Example #1
0
def featured(app_id=None):
    """List featured apps of PyBossa"""
    n_published = cached_apps.n_published()
    if request.method == 'GET':
        apps, n_published = cached_apps.get_published(page=1, per_page=n_published)
        return render_template('/admin/applications.html', apps=apps)
    if request.method == 'POST':
        cached_apps.reset()
        f = model.Featured()
        f.app_id = app_id
        # Check if the app is already in this table
        tmp = db.session.query(model.Featured)\
                .filter(model.Featured.app_id == app_id)\
                .first()
        if (tmp is None):
            db.session.add(f)
            db.session.commit()
            return json.dumps(f.dictize())
        else:
            return json.dumps({'error': 'App.id %s already in Featured table'
                               % app_id})
    if request.method == 'DELETE':
        cached_apps.reset()
        f = db.session.query(model.Featured)\
              .filter(model.Featured.app_id == app_id)\
              .first()
        if (f):
            db.session.delete(f)
            db.session.commit()
            return "", 204
        else:
            return json.dumps({'error': 'App.id %s is not in Featured table'
                               % app_id})
Example #2
0
def featured(app_id=None):
    """List featured apps of PyBossa"""
    try:
        categories = cached_cat.get_all()

        if request.method == 'GET':
            apps = {}
            for c in categories:
                n_apps = cached_apps.n_count(category=c.short_name)
                apps[c.short_name], n_apps = cached_apps.get(category=c.short_name,
                                                             page=1,
                                                             per_page=n_apps)
            return render_template('/admin/applications.html', apps=apps,
                                   categories=categories)
        elif app_id:
            if request.method == 'POST':
                cached_apps.reset()
                f = model.Featured()
                f.app_id = app_id
                app = db.session.query(model.App).get(app_id)
                require.app.update(app)
                # Check if the app is already in this table
                tmp = db.session.query(model.Featured)\
                        .filter(model.Featured.app_id == app_id)\
                        .first()
                if (tmp is None):
                    db.session.add(f)
                    db.session.commit()
                    return json.dumps(f.dictize())
                else:
                    msg = "App.id %s alreay in Featured table" % app_id
                    return format_error(msg, 415)
            if request.method == 'DELETE':
                cached_apps.reset()
                f = db.session.query(model.Featured)\
                      .filter(model.Featured.app_id == app_id)\
                      .first()
                if (f):
                    db.session.delete(f)
                    db.session.commit()
                    return "", 204
                else:
                    msg = 'App.id %s is not in Featured table' % app_id
                    return format_error(msg, 404)
        else:
            msg = ('App.id is missing for %s action in featured method' %
                   request.method)
            return format_error(msg, 415)
    except HTTPException:
        return abort(403)
    except Exception as e:
        current_app.logger.error(e)
        return abort(500)