Repository: dotkom/onlineweb4

Issue:
Refactor insane method related to feedback in Event
So there's a method to obtain feedback_users:

<pre>
50     def feedback_users(self):
51         users = []
52         try:
53             if self.attendance_event.attendees.all():
54                 for attendee in self.attendance_event.attendees.filter(attended=True):
55                     users.append(attendee.user)
56             return users
57         except AttendanceEvent.DoesNotExist:
58             return users
</pre>


I'm pretty sure this is the same as doing:

<pre>
def feedback_users(self):
    if self.is_attendance_event:
        return self.attendance_event.attendees.filter(attended=True)
</pre>


There are two reason why this would be better; firstly using the actual method for checking if it's an attendance event, secondly all the extra code makes my eyes bleed for no obvious reason.

EDIT: As pointed out on irc, the top piece of code returns a list of attendee.user, which mine does not.. Enter lambda..

<pre>
def feedback_users(self):
    if self.is_attendance_event:
        return map(lambda a: getattr(a, 'user'), self.attendance_event.attendees.filter(attended=True))
</pre>



Discussion:
This method would very much like a test, as well. I couldn't find a test using it at least.


Resolving patch:
diff --git a/apps/events/models.py b/apps/events/models.py
--- a/apps/events/models.py
+++ b/apps/events/models.py
@@ -52,14 +52,9 @@ class Event(models.Model):
     event_type = models.SmallIntegerField(_(u'type'), choices=TYPE_CHOICES, null=False)
 
     def feedback_users(self):
-        users = []
-        try:
-            if self.attendance_event.attendees.all():
-                for attendee in self.attendance_event.attendees.filter(attended=True):
-                    users.append(attendee.user)
-            return users
-        except AttendanceEvent.DoesNotExist:
-            return users
+        if self.is_attendance_event:
+            return [a.user for a in self.attendance_event.attendees.filter(attended=True)]
+        return []
 
     def feedback_date(self):
         return self.event_end
diff --git a/apps/feedback/models.py b/apps/feedback/models.py
--- a/apps/feedback/models.py
+++ b/apps/feedback/models.py
@@ -111,6 +111,20 @@ def can_answer(self, user):
                 return False
         return True
 
+    def answer_error_message(self, user):
+        if user in self.answered.all():
+            return _(u'Du har allerede svart på skjemaet.')
+
+        if hasattr(self.content_object, "feedback_users"):
+            if self.content_object.feedback_users():
+                if user not in self.content_object.feedback_users():
+                    return _(u'Du har ikke tilgang til å svare på dette skjemaet.')
+            else:
+                return _(u'Skjemaet har ingen brukere som kan svare på skjemaet.')
+
+        return _(u'Ukjent feil.')
+
+
     def not_answered(self):
         if hasattr(self.content_object, "feedback_users"):
             return set(self.content_object.feedback_users()).difference(set(self.answered.all()))
diff --git a/apps/feedback/views.py b/apps/feedback/views.py
--- a/apps/feedback/views.py
+++ b/apps/feedback/views.py
@@ -23,7 +23,7 @@ def feedback(request, applabel, appmodel, object_id, feedback_id):
     fbr = _get_fbr_or_404(applabel, appmodel, object_id, feedback_id)
 
     if not fbr.can_answer(request.user):
-        messages.error(request, _(u"Du kan ikke svare på dette skjemaet."))
+        messages.error(request, fbr.answer_error_message(request.user))
         return redirect("home")
 
     if request.method == "POST":
@@ -40,10 +40,10 @@ def feedback(request, applabel, appmodel, object_id, feedback_id):
             fosa = FieldOfStudyAnswer(feedback_relation = fbr, answer = request.user.field_of_study)
             fosa.save()
 
-            messages.success(request, _(u"Takk for at du svarte"))
+            messages.success(request, _(u"Takk for at du svarte."))
             return redirect("home")
         else:
-            messages.error(request, _(u"Du må svare på alle påkrevde felt"))
+            messages.error(request, _(u"Du må svare på alle påkrevde felt."))
     else:
         answers = create_answer_forms(fbr)
 


Test patch: