Factored out the clean_existing_user method
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 08 Jan 2009 21:23:42 +0000
changeset 788 892877b7db07
parent 787 63d7f170b63c
child 789 e55cad180973
Factored out the clean_existing_user method Patch by: Sverre Rabbelier
app/soc/logic/cleaning.py
app/soc/views/models/notification.py
--- a/app/soc/logic/cleaning.py	Thu Jan 08 21:23:33 2009 +0000
+++ b/app/soc/logic/cleaning.py	Thu Jan 08 21:23:42 2009 +0000
@@ -26,6 +26,7 @@
 from django import forms
 
 from soc.logic import validate
+from soc.logic.models import user as user_logic
 
 
 def clean_new_link_id(logic):
@@ -56,6 +57,26 @@
   return link_id
 
 
+def clean_existing_user(field_name):
+  """Check if the field_name field is a valid user.
+  """
+
+  def wrapped(self):
+    link_id = self.cleaned_data.get(field_name).lower()
+  
+    if not validate.isLinkIdFormatValid(link_id):
+      raise forms.ValidationError("This link ID is in wrong format.")
+  
+    user_entity = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)
+  
+    if not user_entity:
+      # user does not exist
+      raise forms.ValidationError("This user does not exist")
+  
+    return user_entity
+  return wrapped
+
+
 def clean_feed_url(self):
   feed_url = self.cleaned_data.get('feed_url')
 
--- a/app/soc/views/models/notification.py	Thu Jan 08 21:23:33 2009 +0000
+++ b/app/soc/views/models/notification.py	Thu Jan 08 21:23:42 2009 +0000
@@ -27,6 +27,7 @@
 from django import forms
 from django.utils.translation import ugettext_lazy
 
+from soc.logic import cleaning
 from soc.logic import dicts
 from soc.logic import validate
 from soc.models import notification as notification_model
@@ -63,21 +64,7 @@
     # exclude the necessary fields from the form
     exclude = ['link_id', 'scope', 'scope_path', 'from_user', 'unread']
 
-  def clean_to_user(self):
-    """Check if the to_user field has been filled in correctly.
-    """
-    link_id = self.cleaned_data.get('to_user').lower()
-
-    if not validate.isLinkIdFormatValid(link_id):
-      raise forms.ValidationError("This link ID is in wrong format.")
-
-    to_user = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)
-
-    if not to_user:
-      # user does not exist
-      raise forms.ValidationError("This user does not exist")
-
-    return link_id
+  clean_to_user = cleaning.clean_existing_user('to_user')
 
 
 class View(base.View):
@@ -157,13 +144,10 @@
     # get the current user
     current_user = user_logic.logic.getForCurrentAccount()
 
-    to_user = user_logic.logic.getForFields(
-        {'link_id' : fields['to_user']}, unique=True)
-
     fields['link_id'] = '%i' % (time.time())
-    fields['scope'] = to_user
+    fields['scope'] = fields['to_user']
     fields['from_user'] = current_user
-    fields['scope_path'] = fields['to_user']
+    fields['scope_path'] = fields['to_user'].link_id
 
   def _editSeed(self, request, seed):
     """Checks if scope_path is seeded and puts it into to_user.