From: Nick Retallack Date: Wed, 28 Oct 2009 08:22:55 +0000 (-0700) Subject: the beginnings of user-art relations. You can add relations, and it makes sure they... X-Git-Url: http://git.veekun.com/zzz-floof.git/commitdiff_plain/cb1976ef371904b45d7961212cd87595a9486284?ds=sidebyside;hp=f267b2f2841d407dcdf9c3c0f5406f8c10403ccb the beginnings of user-art relations. You can add relations, and it makes sure they're unique. Confirmation fields are there, but not used yet. --- diff --git a/floof/config/routing.py b/floof/config/routing.py index 645d138..32451e9 100644 --- a/floof/config/routing.py +++ b/floof/config/routing.py @@ -65,6 +65,10 @@ def make_map(): sub.connect('delete_tag', '/art/{art_id}/tag/{id}') sub.connect('create_tag', '/art/{art_id}/tag') + with map.submapper(controller='relation') as sub: + sub.connect('create_relation', '/art/{art_id}/relations/{kind}/create', action="create") + # TODO: conditions: kind = by|for|of|character? + map.resource('tag','tags', controller="tag", parent_resource=dict(member_name='art', collection_name='art')) # Yeah, parent resources are specified kinda dumb-ly. Would be better if you could pass in the diff --git a/floof/controllers/relation.py b/floof/controllers/relation.py new file mode 100644 index 0000000..4139da8 --- /dev/null +++ b/floof/controllers/relation.py @@ -0,0 +1,32 @@ +import logging + +from pylons import request, response, session, tmpl_context as c, h, url +from pylons.controllers.util import abort, redirect + +from floof.lib.base import BaseController, render + +log = logging.getLogger(__name__) + +from floof.model.art import Art, UserRelation +from floof.model.users import User +import elixir + +class RelationController(BaseController): + def create(self, art_id, kind): + art = h.get_object_or_404(Art, id=art_id) + user = h.get_object_or_404(User, name=request.params['username']) + ## TODO: actually, this should act like a form validation. + + prior_relation = UserRelation.get_by(art=art, user=user) + if prior_relation: + abort(404) ## should be a validation error + + relation = UserRelation(user=user, kind=kind, art=art, creator=c.user) + elixir.session.commit() + redirect(url('show_art', id=art_id)) + + def index(self): + # Return a rendered template + #return render('/relation.mako') + # or, return a response + return 'Hello World' diff --git a/floof/model/art.py b/floof/model/art.py index 8b328d8..e9d4e44 100644 --- a/floof/model/art.py +++ b/floof/model/art.py @@ -23,6 +23,9 @@ class Art(Entity): tags = OneToMany('Tag') discussion = ManyToOne('Discussion') + user_relations = OneToMany('UserRelation') + + def set_file(self, file): self.hash = save_file("art", file) self.original_filename = file.filename @@ -102,9 +105,30 @@ Rating.reverse_options = dict (zip(Rating.options.values(), Rating.options.keys( class UserRelation(Entity): - related = ManyToOne("User") + user = ManyToOne("User") art = ManyToOne("Art") - type = Field(String) # by for of + kind = Field(String) # by for of + creator = ManyToOne("User") + confirmed_by_related_user = Field(Boolean) + + # it is useful to record which authority figure on a given artwork + # confirmed the validity of this relation. + confirmed_by_authority = ManyToOne("User") + + def __init__(self, **kwargs): + super(UserRelation, self).__init__(**kwargs) + assert self.user and self.art and self.kind and self.creator + + + if self.creator == self.user: + self.confirmed_by_related_user = True + # TODO: implement authorities + # if self.creator in self.art.authorities + # self.confirmed_by_authority = self.creator + + def __unicode__(self): + return "%s: %s" % (self.kind, self.related_user) + # class CharacterRelation(Entity): diff --git a/floof/templates/art/show.mako b/floof/templates/art/show.mako index cf713dc..45ebcb9 100644 --- a/floof/templates/art/show.mako +++ b/floof/templates/art/show.mako @@ -32,6 +32,20 @@ ${h.submit('score', text)} ${h.end_form()} % endif +

Relations

+ + +

Add Relations

+${h.form (h.url("create_relation", kind="by", art_id=c.art.id))} +By: ${h.text('username')} +${h.submit('add','Add')} +${h.end_form()} + + ${comments.comment_block(c.art.discussion.comments)} diff --git a/floof/tests/functional/test_relation.py b/floof/tests/functional/test_relation.py new file mode 100644 index 0000000..b651acf --- /dev/null +++ b/floof/tests/functional/test_relation.py @@ -0,0 +1,7 @@ +from floof.tests import * + +class TestRelationController(TestController): + + def test_index(self): + response = self.app.get(url(controller='relation', action='index')) + # Test response...