some nice mixins for tags, ratings, relations
[zzz-floof.git] / floof / controllers / art.py
index 7ca1285..bb3bc19 100644 (file)
@@ -8,7 +8,12 @@ from floof.lib.base import BaseController, render
 log = logging.getLogger(__name__)
 
 import elixir
-from floof.model.art import Art, Rating
+from floof.model.users import User
+from floof.model import Art, Rating, UserRelation
+from floof.model.comments import Discussion
+
+from sqlalchemy.exceptions import IntegrityError
+
 
 class ArtController(BaseController):
     def __before__(self, id=None):
@@ -17,33 +22,45 @@ class ArtController(BaseController):
         if id:
             c.art = h.get_object_or_404(Art, id=id)
 
-    # def index():
-    #     c.artwork = Art.query.order_by(Art.id.desc()).all()
-    #     return render
-
     def new(self):
         """ New Art! """
         return render("/art/new.mako")
 
     # TODO: login required
     def create(self):
-        Art(uploaded_by=c.user, **request.params)
-        elixir.session.commit()
-        redirect_to(controller="main", action="index")
+        # if 'file' not in request.params or not request.params['file']:
+        #     return "Validation Error: Needs a File"
+
+
+        c.art = Art(uploader=c.user, **request.params)
+        c.art.discussion = Discussion(count=0)
+        
+
+        artist = User.get_by(name=request.params['artist'])
+        if not artist:
+            return "Validation Error: Artist not found"
+
+        relation = UserRelation(user=artist, kind="by", creator=c.user, art=c.art)
+
+        try:
+            elixir.session.commit()
+            redirect(url('show_art', id=c.art.id))
+        except IntegrityError:
+            # hurr, there must be a better way to do this but I am lazy right now
+            hash = c.art.hash
+            elixir.session.rollback()
+            duplicate_art = Art.get_by(hash=hash)
+            h.flash("We already have that one.")
+            redirect(url('show_art', id=duplicate_art.id))
+
 
     def show(self, id):
         # c.art = h.get_object_or_404(Art, id=id)
         if c.user:
             c.your_score = c.art.user_score(c.user)
         return render("/art/show.mako")
-        
-    # # TODO: login required
-    # def tag(self, id):
-    #     # c.art = h.get_object_or_404(Art, id=id)
-    #     c.art.add_tags(request.params["tags"], c.user)
-    #     elixir.session.commit()
-    #     redirect_to('show_art', id=c.art.id)
-    # 
+
+
     # TODO: login required
     def rate(self, id):
         # c.art = h.get_object_or_404(Art, id=id)
@@ -52,8 +69,8 @@ class ArtController(BaseController):
             score = int(score)
         else:
             score = Rating.reverse_options.get(score)
-        
+
         c.art.rate(score, c.user)
         elixir.session.commit()
-            
-        redirect(url('art', id=c.art.id))
+
+        redirect(url('show_art', id=c.art.id))