Less hardcoding
authorCharles Connell <charles@connells.org>
Thu, 23 Jan 2014 00:48:36 +0000 (19:48 -0500)
committerCharles Connell <charles@connells.org>
Thu, 23 Jan 2014 15:44:36 +0000 (10:44 -0500)
karmaworld/apps/users/views.py
karmaworld/templates/user_profile.html

index e2511e528b129aa51016575bca9f341ad1f7f670..7bd02f621cfc3ebe65b9e686e565428546f3e4d4 100644 (file)
@@ -7,7 +7,7 @@ from django.contrib.auth.models import User
 from django.views.generic import TemplateView
 from django.views.generic.list import MultipleObjectMixin
 from karmaworld.apps.notes.models import Note
-from karmaworld.apps.users.models import NoteKarmaEvent, CourseKarmaEvent, GenericKarmaEvent
+from karmaworld.apps.users.models import ALL_KARMA_EVENT_CLASSES
 
 
 class ProfileView(TemplateView, MultipleObjectMixin):
@@ -36,11 +36,14 @@ class ProfileView(TemplateView, MultipleObjectMixin):
 
     def get_context_data(self, **kwargs):
         notes = [('note', o) for o in Note.objects.filter(user=self.request.user)]
-        generic_karma_events = [('generic_karma_events', o) for o in GenericKarmaEvent.objects.filter(user=self.request.user)]
-        note_karma_events = [('note_karma_event', o) for o in NoteKarmaEvent.objects.filter(user=self.request.user)]
-        course_karma_events = [('course_karma_event', o) for o in CourseKarmaEvent.objects.filter(user=self.request.user)]
-
-        result_list = sorted(chain(notes, generic_karma_events, note_karma_events, course_karma_events),
+        all_events = []
+        for cls in ALL_KARMA_EVENT_CLASSES:
+            all_events.append(
+                [(cls.__name__, o) for o in cls.objects.filter(user=self.request.user)]
+            )
+        all_events = chain.from_iterable(all_events)
+
+        result_list = sorted(chain(notes, all_events),
                              cmp=ProfileView.compareProfileItems,
                              key=lambda o: o[1],
                              reverse=True)
index dab9388016106bd368d45c2cd3b2ef2c40b5165f..7d4bd528ed1aff63444968f8bb19f27ae3cf3038 100644 (file)
@@ -87,7 +87,7 @@
                 {% endwith %}
               {% endif %}
 
-              {% if 'karma_event' in item.0 %}
+              {% if 'KarmaEvent' in item.0 %}
                 {% with event=item.1 %}
                   <div class="row">
                     <div class="small-12 columns activity_timestamp">
                       {% endif %}
                       {{ event.get_message }}
                     </div>
-                    {% if item.0 == 'note_karma_event' %}
+                    {% if item.0 == 'NoteKarmaEvent' %}
                       <div class="small-12 columns activity_details_action">
                         <a href="{{ event.note.get_absolute_url }}" class="activity_target">{{ event.note.name }}</a>
                       </div>
                         <a href="{{ event.note.course.get_absolute_url }}" class="course_link">{{ event.note.course.name }} // {{ event.note.course.school.name }}</a>
                       </div>
                     {% endif %}
-                    {% if item.0 == 'course_karma_event' %}
+                    {% if item.0 == 'CourseKarmaEvent' %}
                       <div class="small-12 columns activity_details_action">
                         <a href="{{ event.course.get_absolute_url }}" class="activity_target">{{ event.course.name }}</a>
                       </div>