Show "Created by" read-only field in Document Edit view. Update size of TinyMCE widget in Document Edit/Create views. Change user property name to founder in Document model and update files according to this change (now founder is used in Group and Document models). Remove not used variables and imports in views/site/docs/edit.py. Refactor EditForm and CreateForm in views/site/docs/edit.py so that EditForm inherits from CreateForm and just extends it.
authorPawel Solyga <Pawel.Solyga@gmail.com>
Wed, 15 Oct 2008 14:06:33 +0000
changeset 338 0d78f41dde9b
parent 337 cb7d22b1e7d8
child 339 b9be44e09530
Show "Created by" read-only field in Document Edit view. Update size of TinyMCE widget in Document Edit/Create views. Change user property name to founder in Document model and update files according to this change (now founder is used in Group and Document models). Remove not used variables and imports in views/site/docs/edit.py. Refactor EditForm and CreateForm in views/site/docs/edit.py so that EditForm inherits from CreateForm and just extends it. Patch by: Pawel Solyga Review by: to-be-reviewed
app/soc/models/document.py
app/soc/views/site/docs/edit.py
app/soc/views/site/home.py
--- a/app/soc/models/document.py	Wed Oct 15 13:59:52 2008 +0000
+++ b/app/soc/models/document.py	Wed Oct 15 14:06:33 2008 +0000
@@ -56,8 +56,11 @@
   #: view them (which may be anyone, for example, with the site front page).
   content = db.TextProperty(verbose_name=ugettext_lazy('Content'))
   
-  #: User who created this document.
   #: TODO(pawel.solyga): replace this with WorkAuthors relation
-  user = db.ReferenceProperty(reference_class=soc.models.user.User,
-                              required=True, collection_name='documents')
+  #: Required many:1 relationship indicating the founding User of the
+  #: Document (this relationship is needed to keep track of lifetime document
+  #: creation limits, used to prevent spamming, etc.).
+  founder = db.ReferenceProperty(reference_class=soc.models.user.User,
+                                 required=True, collection_name="documents",
+                                 verbose_name=ugettext_lazy('Created by'))
 
--- a/app/soc/views/site/docs/edit.py	Wed Oct 15 13:59:52 2008 +0000
+++ b/app/soc/views/site/docs/edit.py	Wed Oct 15 14:06:33 2008 +0000
@@ -32,7 +32,6 @@
 from soc.logic import out_of_band
 from soc.logic import path_link_name
 from soc.logic.models import document
-from soc.logic.site import id_user
 
 from soc.views import helper
 from soc.views import simple
@@ -47,30 +46,6 @@
 import soc.views.out_of_band
 
 
-class EditForm(helper.forms.DbModelForm):
-  """Django form displayed when Developer edits a Document.
-  """
-  doc_key_name = forms.fields.CharField(widget=forms.HiddenInput)
-  content = forms.fields.CharField(widget=helper.widgets.TinyMCE())
-  
-  class Meta:
-    model = soc.models.document.Document
-    
-    #: list of model fields which will *not* be gathered by the form
-    exclude = ['inheritance_line', 'user', 'created', 'modified']
- 
-  def clean_partial_path(self):
-    partial_path = self.cleaned_data.get('partial_path')
-    # TODO(tlarsen): combine path and link_name and check for uniqueness
-    return partial_path
-
-  def clean_link_name(self):
-    link_name = self.cleaned_data.get('link_name')
-    # TODO(tlarsen): combine path and link_name and check for uniqueness
-    return link_name
-
-
-DEF_SITE_DOCS_EDIT_TMPL = 'soc/site/docs/edit.html'
 DEF_CREATE_NEW_DOC_MSG = ' You can create a new document by visiting the' \
                          ' <a href="/site/docs/edit">Create ' \
                          'a New Document</a> page.'
@@ -100,7 +75,7 @@
   properties['short_name'] = form.cleaned_data.get('short_name')
   properties['abstract'] = form.cleaned_data.get('abstract')
   properties['content'] = form.cleaned_data.get('content')
-  properties['user'] = models.user.logic.getFromFields(email=email)
+  properties['founder'] = models.user.logic.getFromFields(email=email)
   properties['is_featured'] = form.cleaned_data.get('is_featured')
 
   doc = document.logic.updateOrCreateFromFields(properties,
@@ -109,6 +84,87 @@
   return doc
 
 
+class CreateForm(helper.forms.DbModelForm):
+  """Django form displayed when Developer creates a Document.
+  """
+  content = forms.fields.CharField(widget=helper.widgets.TinyMCE(
+      attrs={'rows':10, 'cols':40}))
+
+  class Meta:
+    model = soc.models.document.Document
+
+    #: list of model fields which will *not* be gathered by the form
+    exclude = ['inheritance_line', 'founder', 'created', 'modified']
+
+  def clean_partial_path(self):
+    partial_path = self.cleaned_data.get('partial_path')
+    # TODO(tlarsen): combine path and link_name and check for uniqueness
+    return partial_path
+
+  def clean_link_name(self):
+    link_name = self.cleaned_data.get('link_name')
+    # TODO(tlarsen): combine path and link_name and check for uniqueness
+    return link_name
+
+
+DEF_SITE_DOCS_CREATE_TMPL = 'soc/site/docs/edit.html'
+
+def create(request, template=DEF_SITE_DOCS_CREATE_TMPL):
+  """View for a Developer to create a new Document entity.
+
+  Args:
+    request: the standard django request object
+    template: the "sibling" template (or a search list of such templates)
+      from which to construct the public.html template name (or names)
+
+  Returns:
+    A subclass of django.http.HttpResponse which either contains the form to
+    be filled out, or a redirect to the correct view in the interface.
+  """
+
+  try:
+    access.checkIsDeveloper(request)
+  except  soc.views.out_of_band.AccessViolationResponse, alt_response:
+    return alt_response.response()
+
+  # create default template context for use with any templates
+  context = helper.responses.getUniversalContext(request)
+
+  if request.method == 'POST':
+    form = CreateForm(request.POST)
+
+    if form.is_valid():
+      doc = getDocForForm(form)
+
+      if not doc:
+        return http.HttpResponseRedirect('/')
+
+      new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
+
+      # redirect to new /site/docs/edit/new_path?s=0
+      # (causes 'Profile saved' message to be displayed)
+      return helper.responses.redirectToChangedSuffix(
+          request, None, new_path,
+          params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
+  else: # method == 'GET':
+    # no link name specified, so start with an empty form
+    form = CreateForm()
+
+  context['form'] = form
+
+  return helper.responses.respond(request, template, context)
+
+
+DEF_SITE_DOCS_EDIT_TMPL = 'soc/site/docs/edit.html'
+
+class EditForm(CreateForm):
+  """Django form displayed when Developer edits a Document.
+  """
+  doc_key_name = forms.fields.CharField(widget=forms.HiddenInput)
+  created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
+                                      required=False)
+
+
 def edit(request, partial_path=None, link_name=None,
          template=DEF_SITE_DOCS_EDIT_TMPL):
   """View for a Developer to modify the properties of a Document Model entity.
@@ -134,7 +190,6 @@
 
 # create default template context for use with any templates
   context = helper.responses.getUniversalContext(request)
-  logged_in_id = users.get_current_user()
 
   doc = None  # assume that no Document entity will be found
 
@@ -184,12 +239,14 @@
                 request, profile.SUBMIT_MSG_PARAM_NAME,
                 values=SUBMIT_MESSAGES))
 
-        # populate form with the existing User entity
+        # populate form with the existing Document entity
+        founder_link_name = doc.founder.link_name
         form = EditForm(initial={'doc_key_name': doc.key().name(),
             'title': doc.title, 'partial_path': doc.partial_path,
             'link_name': doc.link_name, 'short_name': doc.short_name,
             'abstract': doc.abstract, 'content': doc.content,
-            'user': doc.user, 'is_featured': doc.is_featured})       
+            'founder': doc.founder, 'is_featured': doc.is_featured,
+            'created_by': founder_link_name})       
       else:
         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
           # redirect to aggressively remove 'Profile saved' query parameter
@@ -212,77 +269,6 @@
   return helper.responses.respond(request, template, context)
 
 
-class CreateForm(helper.forms.DbModelForm):
-  """Django form displayed when Developer creates a Document.
-  """
-  content = forms.fields.CharField(widget=helper.widgets.TinyMCE())
-  
-  class Meta:
-    model = soc.models.document.Document
-    
-    #: list of model fields which will *not* be gathered by the form
-    exclude = ['inheritance_line', 'user', 'created', 'modified']
- 
-  def clean_partial_path(self):
-    partial_path = self.cleaned_data.get('partial_path')
-    # TODO(tlarsen): combine path and link_name and check for uniqueness
-    return partial_path
-
-  def clean_link_name(self):
-    link_name = self.cleaned_data.get('link_name')
-    # TODO(tlarsen): combine path and link_name and check for uniqueness
-    return link_name
-
-
-DEF_SITE_DOCS_CREATE_TMPL = 'soc/site/docs/edit.html'
-
-def create(request, template=DEF_SITE_DOCS_CREATE_TMPL):
-  """View for a Developer to create a new Document entity.
-
-  Args:
-    request: the standard django request object
-    template: the "sibling" template (or a search list of such templates)
-      from which to construct the public.html template name (or names)
-
-  Returns:
-    A subclass of django.http.HttpResponse which either contains the form to
-    be filled out, or a redirect to the correct view in the interface.
-  """
-
-  try:
-    access.checkIsDeveloper(request)
-  except  soc.views.out_of_band.AccessViolationResponse, alt_response:
-    return alt_response.response()
-
-  # create default template context for use with any templates
-  context = helper.responses.getUniversalContext(request)
-  logged_in_id = users.get_current_user()
-
-  if request.method == 'POST':
-    form = CreateForm(request.POST)
-
-    if form.is_valid():
-      doc = getDocForForm(form)
-
-      if not doc:
-        return http.HttpResponseRedirect('/')
-
-      new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
-        
-      # redirect to new /site/docs/edit/new_path?s=0
-      # (causes 'Profile saved' message to be displayed)
-      return helper.responses.redirectToChangedSuffix(
-          request, None, new_path,
-          params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
-  else: # method == 'GET':
-    # no link name specified, so start with an empty form
-    form = CreateForm()
-
-  context['form'] = form
-
-  return helper.responses.respond(request, template, context)
-
-
 def delete(request, partial_path=None, link_name=None,
            template=DEF_SITE_DOCS_EDIT_TMPL):
   """Request handler for a Developer to delete Document Model entity.
--- a/app/soc/views/site/home.py	Wed Oct 15 13:59:52 2008 +0000
+++ b/app/soc/views/site/home.py	Wed Oct 15 14:06:33 2008 +0000
@@ -54,7 +54,8 @@
 
 
 class DocumentForm(helper.forms.DbModelForm):
-  content = forms.fields.CharField(widget=helper.widgets.TinyMCE())
+  content = forms.fields.CharField(widget=helper.widgets.TinyMCE(
+      attrs={'rows':10, 'cols':40}))
 
   class Meta:
     """Inner Meta class that defines some behavior for the form.
@@ -64,7 +65,7 @@
     
     #: list of model fields which will *not* be gathered by the form
     exclude = ['partial_path', 'link_name',
-               'user', 'modified', 'created', 'inheritance_line']
+               'founder', 'modified', 'created', 'inheritance_line']
 
 
 class SiteSettingsForm(helper.forms.DbModelForm):
@@ -161,7 +162,7 @@
       link_name = DEF_SITE_HOME_DOC_LINK_NAME
       partial_path=DEF_SITE_SETTINGS_PATH
       logged_in_id = users.get_current_user()
-      user = models.user.logic.getFromFields(email=logged_in_id.email())
+      founder = models.user.logic.getFromFields(email=logged_in_id.email())
 
       properties = {
         'title': document_form.cleaned_data.get('title'),
@@ -171,7 +172,7 @@
         'link_name': link_name,
         'partial_path': partial_path,
         'id': logged_in_id,
-        'user': user,
+        'founder': founder,
       }
 
       site_doc = document.logic.updateOrCreateFromFields(