From d45173b0da9a8e2439864f73362521a7bdfd0fd4 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 17 Jul 2013 13:02:21 +0000 Subject: [PATCH] fix position of edits onload(like lists and texts) --- pandora/edit/models.py | 2 +- pandora/user/models.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pandora/edit/models.py b/pandora/edit/models.py index b22608cbf..6383db296 100644 --- a/pandora/edit/models.py +++ b/pandora/edit/models.py @@ -32,7 +32,7 @@ class Edit(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) - user = models.ForeignKey(User) + user = models.ForeignKey(User, related_name='edits') name = models.CharField(max_length=255) status = models.CharField(max_length=20, default='private') diff --git a/pandora/user/models.py b/pandora/user/models.py index f9e3e4cdf..a0a7b7b6b 100644 --- a/pandora/user/models.py +++ b/pandora/user/models.py @@ -15,6 +15,7 @@ from ox.utils import json from itemlist.models import List, Position import text +import edit import managers import tasks @@ -285,6 +286,27 @@ def get_ui(user_ui, user=None): ids.append(t.get_id()) return ids + def add_edits(edits, section): + P = edit.models.Position + ids = [] + for t in edits: + qs = P.objects.filter(section=section) + if section == 'featured': + try: + pos = P.objects.get(edit=t, section=section) + created = False + except P.DoesNotExist: + pos = P(edit=t, section=section, user=t.user) + pos.save() + created = True + else: + pos, created = P.objects.get_or_create(edit=t, user=user, section=section) + qs = qs.filter(user=user) + if created: + pos.position = qs.aggregate(Max('position'))['position__max'] + 1 + pos.save() + ids.append(t.get_id()) + return ids ids = [''] if user: @@ -299,6 +321,10 @@ def get_ui(user_ui, user=None): tids += add_texts(user.texts.exclude(status="featured"), 'personal') tids += add_texts(user.subscribed_texts.filter(status='public'), 'public') tids += add_texts(text.models.Text.objects.filter(status='featured'), 'featured') + if user: + tids += add_edits(user.edits.exclude(status="featured"), 'personal') + tids += add_edits(user.subscribed_edits.filter(status='public'), 'public') + tids += add_edits(edit.models.Edit.objects.filter(status='featured'), 'featured') return ui def init_user(user, request=None):