# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2013 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy.sql import text
from pybossa.cache import cache, memoize, ONE_HOUR
from pybossa.core import db
import pybossa.model as model


@cache(key_prefix="categories_all", timeout=ONE_HOUR)
def get_all():
    """Return all categories"""
    return db.session.query(model.Category).all()


@cache(key_prefix="categories_used", timeout=ONE_HOUR)
def get_used():
    """Return categories only used by apps"""
    sql = text('''
               SELECT category.* FROM category, app
               WHERE app.category_id=category.id GROUP BY category.id
               ''')
    results = db.engine.execute(sql)
    categories = []
    for row in results:
        category = dict(id=row.id, name=row.name, short_name=row.short_name,
                        description=row.description)
        categories.append(category)
    return categories


def reset():
    """Clean the cache"""
    cache.delete('categories_all')
    cache.delete('categories_used')

@@ -806,25 +806,30 @@
 he, 
-memoize
+delete_cached
 , ONE_
-HOUR
+DAY
 %0Afro
@@ -925,36 +925,35 @@
 l%22, timeout=ONE_
-HOUR
+DAY
 )%0Adef get_all():
@@ -1090,12 +1090,11 @@
 ONE_
-HOUR
+DAY
 )%0Ade
@@ -1624,36 +1624,37 @@
 ache%22%22%22%0A    
-cache.
 delete
+_cached
 ('categories
@@ -1668,20 +1668,21 @@
     
-cache.
 delete
+_cached
 ('ca

Use new cache