Italo Maia avatar Italo Maia committed fa7cf25

Added some new fancy save methods

Comments (0)

Files changed (1)

 # -*- coding:utf-8 -*-
 
-from django import forms
 from app_settings import *
 from models import Reply, Thread, Profile, Message, Rating
 
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
 THREAD_META_FIELDS = ['title']
 
 if THREAD_ALLOW_IMAGE:
 
 
 class MessageForm(forms.ModelForm):
+
+    def __init__(self, *args, **kw):
+        super(MessageForm, self).__init__(*args, **kw)
+        self.fields['message'].required = True
+
+    def save(self, author, recipient, commit=True):
+        message = super(MessageForm, self).save(commit=False)
+        message.author = author
+        message.to = recipient
+
+        if commit:
+            message.save()
+
+        return message
+
     class Meta:
         model = Message
         fields = ['title', 'message']
     Form used to create a reply
     """
 
-    def save(self, author, forum, thread, **kw):
+    class Meta:
+        model = Reply
+        fields = ['message']
+
+    def __init__(self, *args, **kw):
+        super(ReplyForm, self).__init__(*args, **kw)
+        self.fields['message'].required = True
+
+    def save(self, author, forum, thread, commit=True):
         reply = super(ReplyForm, self).save(commit=False)
         reply.author = author
         reply.forum = forum
         reply.thread = thread
-        reply.save()
+
+        if commit:
+            reply.save()
+
         return reply
 
-    class Meta:
-        model = Reply
-        fields = ['message']
-
 
 class ThreadForm(forms.ModelForm):
-    def save(self, author, forum, **kw):
+
+    def save(self, author, forum, commit=True):
         thread = super(ThreadForm, self).save(commit=False)
         thread.author = author
         thread.forum = forum
-        thread.save()
+
+        if commit:
+            thread.save()
+
         return thread
 
 
         exclude = ['flag']
 
 
-class EditProfileForm(forms.ModelForm):
-    class Meta:
-        model = Profile
-        fields = ['avatar']
-
-
 class ProfileForm(forms.ModelForm):
-    signature = forms.CharField(widget=forms.Textarea, required=False)
+    message = forms.CharField(label=_('Signature'), widget=forms.Textarea, required=False)
 
     class Meta:
         model = Profile
+        fields = ['gender', 'nickname', 'avatar', 'message']
         exclude = ['user', 'flag', 'reputation', 'threads_counter', 'replies_counter']
 
+    def save(self, user, commit=True):
+        profile = super(ProfileForm, self).save(commit=False)
+        profile.user = user
+
+        if commit:
+            profile.save()
+
+        return profile
+
 
 class RateForm(forms.ModelForm):
     def save(self, author, reply, **kw):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.