the beginnings of user-art relations. You can add relations, and it makes sure they...
authorNick Retallack <nickretallack@gmail.com>
Wed, 28 Oct 2009 08:22:55 +0000 (01:22 -0700)
committerNick Retallack <nickretallack@gmail.com>
Wed, 28 Oct 2009 08:22:55 +0000 (01:22 -0700)
floof/config/routing.py
floof/controllers/relation.py [new file with mode: 0644]
floof/model/art.py
floof/templates/art/show.mako
floof/tests/functional/test_relation.py [new file with mode: 0644]

index 645d138..32451e9 100644 (file)
@@ -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 (file)
index 0000000..4139da8
--- /dev/null
@@ -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'
index 8b328d8..e9d4e44 100644 (file)
@@ -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):
index cf713dc..45ebcb9 100644 (file)
@@ -32,6 +32,20 @@ ${h.submit('score', text)}
 ${h.end_form()}
 % endif
 
+<h2>Relations</h2>
+<ul>
+% for relation in c.art.user_relations:
+<li>${relation.kind}: ${relation.user}
+% endfor
+</ul>
+
+<h2>Add Relations</h2>
+${h.form (h.url("create_relation", kind="by", art_id=c.art.id))}
+By: ${h.text('username')}
+${h.submit('add','Add')}
+${h.end_form()}
+
+
 <img class="full" src="${c.art.get_path()}">
 
 ${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 (file)
index 0000000..b651acf
--- /dev/null
@@ -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...