diff options
author | Priit Laes <plaes@plaes.org> | 2010-08-08 09:20:00 +0300 |
---|---|---|
committer | Priit Laes <plaes@plaes.org> | 2010-08-08 09:20:00 +0300 |
commit | 19e67b3239f2621096fd770d1aca46c4ec2c3463 (patch) | |
tree | 2451a752bf0a046b8e5406a0fc1c0ebd022d042d | |
parent | Move registration warning box below header (diff) | |
download | gsoc2010-grumpy-19e67b3239f2621096fd770d1aca46c4ec2c3463.tar.gz gsoc2010-grumpy-19e67b3239f2621096fd770d1aca46c4ec2c3463.tar.bz2 gsoc2010-grumpy-19e67b3239f2621096fd770d1aca46c4ec2c3463.zip |
Implemented user's favorite packages
-rw-r--r-- | grumpy/models.py | 27 | ||||
-rw-r--r-- | grumpy/templates/browse_cat.html | 50 | ||||
-rw-r--r-- | grumpy/webapp.py | 14 |
3 files changed, 66 insertions, 25 deletions
diff --git a/grumpy/models.py b/grumpy/models.py index 241afde..49b58ee 100644 --- a/grumpy/models.py +++ b/grumpy/models.py @@ -30,6 +30,10 @@ package_herds = db.Table('package_herds', db.Model.metadata, db.Column('package_id', db.Integer, db.ForeignKey('packages.id')), db.Column('herd_id', db.Integer, db.ForeignKey('herds.id')) ) +package_users = db.Table('package_users', db.Model.metadata, + db.Column('package_id', db.Integer, db.ForeignKey('packages.id')), + db.Column('user_id', db.Integer, db.ForeignKey('users.id')) + ) class Developer(db.Model): """Represents developers in the system""" @@ -106,27 +110,6 @@ class Ebuild(db.Model): def __repr__(self): return '<%s> - %s' % (self.__class__.__name__, self.cpv) -class Favorite(db.Model): - """Represents packages that user can add in their watch list.""" - - __tablename__ = 'favorites' - __table_args__ = ( - db.UniqueConstraint('package_id', 'user_id'), - {}) - - id = db.Column('id', db.Integer, primary_key=True) - _package = db.Column('package_id', db.Integer, db.ForeignKey('packages.id')) - _user = db.Column('user_id', db.Integer, db.ForeignKey('users.id')) - - package = db.relationship("Package") - user = db.relationship("User", backref=db.backref('favorites')) - - def __init__(self, package): - self.package = package - - def __repr__(self): - return '<%s> - %s' % (self.__class__.__name__, self.package.cp) - class Herd(db.Model): """Represents herds in the system""" @@ -166,6 +149,8 @@ class Package(db.Model): ebuilds = db.relationship(Ebuild, backref='package', \ cascade='all, delete-orphan', \ collection_class=column_mapped_collection(Ebuild.cpv)) + favorites = db.relationship("User", secondary=package_users, + backref='favorites') def __init__(self, ebuild_src, mtime=time.time()): self.key = ebuild_src.key diff --git a/grumpy/templates/browse_cat.html b/grumpy/templates/browse_cat.html index 6de7e15..d66a5a2 100644 --- a/grumpy/templates/browse_cat.html +++ b/grumpy/templates/browse_cat.html @@ -1,14 +1,58 @@ +{% set add_fav = '[ Add to favorites ]' -%} +{% set del_fav = '[ Remove from favorites ]' -%} +{% macro render_package_extras(package, user) -%} + {% if package.qaissues %} + {{ package.qaissues | length }} issue(s)</span> + {% endif %} + {% if user %} + <a id="href_{{package.pkg | e}}" + href="#" onClick="javascript:favorite_toggle('{{ package.key | e }}'); return false;"> + {% if package in user.favorites %} + {{ del_fav }} + {% else %} + {{ add_fav }} + {% endif %} + </a> + {% endif %} +{% endmacro -%} {% extends "layout.html" %} {% block title %}Browsing category "{{ cat | e }}"{% endblock %} +{% block script %} +{{ importer.load_js('jquery.min') }} +<script type='text/javascript'> +function favorite_toggle(pkg) { + $.ajax({ + data: { 'pkg' : pkg}, + url: "{{ url_for('api_favorite_edit') }}", + dataType: 'json', + type: 'POST', + error: function(req, xxx, error) { + alert('Unknown error occurred.'); + }, + success: function(data) { + if (data.success == false) + alert('Unknown error occurred'); + else if (data.success == true) { + elem = $('#href_' + pkg.split('/')[1]) + if (data.status == true) + // Favorite added + elem.text('{{ del_fav }}') + else + // Favorite removed + elem.text('{{ add_fav }}') + } + } + }); +} +</script> +{% endblock %} {% block body %} <h3>Browsing category "{{ cat | e }} - {{ pkgs | length }} package(s):</h3> <ul> <li><a href="{{ url_for('index') }}">..</a></li> {% for pkg in pkgs %} <li><a href="{{ url_for('browse_pkg', cat=cat, pkg=pkg.pkg)}}">{{ pkg.pkg | e }}</a> - {% if pkg.qaissues %} - {{ pkg.qaissues | length }} issue(s)</span> - {% endif %} + {{ render_package_extras(pkg, g.user) }} </li> {% endfor %} <ul> diff --git a/grumpy/webapp.py b/grumpy/webapp.py index 800bf06..200edff 100644 --- a/grumpy/webapp.py +++ b/grumpy/webapp.py @@ -130,7 +130,7 @@ def confirm_account(email): flash(u'Invalid email: "%s"' % email) return redirect(url_for('index')) if user.regtoken is None: - flash(u'Account been already confirmed') + flash(u'Account has been already confirmed') return redirect(url_for('index')) if user.regtoken != request.args.get('token'): flash(u'Invalid token specified') @@ -147,6 +147,18 @@ def api_generate_api_key(): db.session.commit() return jsonify(dict(apikey=g.user.apitoken)) +@app.route('/_api/1.0/favorite/', methods=['POST']) +def api_favorite_edit(): + pkg = Package.query.filter_by(key=request.form.get('pkg')).first() + if not pkg: + return jsonify(dict(success=False)) + if pkg in g.user.favorites: + pkg.favorites.remove(g.user) + else: + pkg.favorites.append(g.user) + db.session.commit() + return jsonify(dict(success=True, status=(pkg in g.user.favorites))) + @app.route('/_api/1.0/tinderbox/') @requires_auth_basic def tinderbox_api(): |