From 9377f772e4c52b6a6ac8df340313a9b8621563cb Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 20 Feb 2013 11:29:29 +0000 Subject: [PATCH] add addpdf button --- pandora/text/models.py | 74 ++++++++++++++++++++++++++++++++- pandora/text/views.py | 79 +++--------------------------------- static/js/pandora/folders.js | 24 ++++++----- static/js/pandora/utils.js | 5 ++- 4 files changed, 96 insertions(+), 86 deletions(-) diff --git a/pandora/text/models.py b/pandora/text/models.py index dbf02047..02a8a804 100644 --- a/pandora/text/models.py +++ b/pandora/text/models.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division, with_statement -from datetime import datetime import os +import re import subprocess +from datetime import datetime from glob import glob from django.db import models @@ -69,6 +70,77 @@ class Text(models.Model): return True return False + def edit(self, data, user): + for key in data: + if key == 'status': + value = data[key] + if value not in self._status: + value = self._status[0] + if value == 'private': + for user in self.subscribed_users.all(): + self.subscribed_users.remove(user) + qs = Position.objects.filter(user=user, + section='section', text=self) + if qs.count() > 1: + pos = qs[0] + pos.section = 'personal' + pos.save() + elif value == 'featured': + if user.get_profile().capability('canEditFeaturedselfs'): + pos, created = Position.objects.get_or_create(text=self, user=user, + section='featured') + if created: + qs = Position.objects.filter(user=user, section='featured') + pos.position = qs.aggregate(Max('position'))['position__max'] + 1 + pos.save() + Position.objects.filter(text=self).exclude(id=pos.id).delete() + else: + value = self.status + elif self.status == 'featured' and value == 'public': + Position.objects.filter(text=self).delete() + pos, created = Position.objects.get_or_create(text=self, + user=self.user,section='personal') + qs = Position.objects.filter(user=self.user, + section='personal') + pos.position = qs.aggregate(Max('position'))['position__max'] + 1 + pos.save() + for u in self.subscribed_users.all(): + pos, created = Position.objects.get_or_create(text=self, user=u, + section='public') + qs = Position.objects.filter(user=u, section='public') + pos.position = qs.aggregate(Max('position'))['position__max'] + 1 + pos.save() + + self.status = value + elif key == 'name': + data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip() + name = data['name'] + if not name: + name = "Untitled" + num = 1 + while Text.objects.filter(name=name, user=self.user).exclude(id=self.id).count()>0: + num += 1 + name = data['name'] + ' [%d]' % num + self.name = name + elif key == 'description': + self.description = ox.sanitize_html(data['description']) + elif key == 'text': + self.text = ox.sanitize_html(data['text']) + + if 'position' in data: + pos, created = Position.objects.get_or_create(text=text, user=user) + pos.position = data['position'] + pos.section = 'featured' + if self.status == 'private': + pos.section = 'personal' + pos.save() + if 'type' in data: + self.type = data['type'] == 'pdf' and 'pdf' or 'html' + if 'posterFrames' in data: + self.poster_frames = tuple(data['posterFrames']) + self.update_icon() + self.save() + def json(self, keys=None, user=None): if not keys: keys=['id', 'name', 'user', 'status', 'subscribed', 'posterFrames', 'description', 'text', 'type', 'links'] diff --git a/pandora/text/views.py b/pandora/text/views.py index 33e1b105..ea941bd0 100644 --- a/pandora/text/views.py +++ b/pandora/text/views.py @@ -39,7 +39,7 @@ def addText(request): } ''' data = json.loads(request.POST['data']) - data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip() + data['name'] = re.sub(' \[\d+\]$', '', data.get('name', 'Untitled')).strip() name = data['name'] if not name: name = "Untitled" @@ -51,6 +51,10 @@ def addText(request): name = data['name'] + ' [%d]' % num text.save() + del data['name'] + if data: + text.edit(data, request.user) + if text.status == 'featured': pos, created = models.Position.objects.get_or_create(text=text, user=request.user, section='featured') @@ -132,78 +136,7 @@ def editText(request): else: text = qs[0] if text.editable(request.user): - for key in data: - if key == 'status': - value = data[key] - if value not in text._status: - value = text._status[0] - if value == 'private': - for user in text.subscribed_users.all(): - text.subscribed_users.remove(user) - qs = models.Position.objects.filter(user=request.user, - section='section', text=text) - if qs.count() > 1: - pos = qs[0] - pos.section = 'personal' - pos.save() - elif value == 'featured': - if request.user.get_profile().capability('canEditFeaturedTexts'): - pos, created = models.Position.objects.get_or_create(text=text, user=request.user, - section='featured') - if created: - qs = models.Position.objects.filter(user=request.user, section='featured') - pos.position = qs.aggregate(Max('position'))['position__max'] + 1 - pos.save() - models.Position.objects.filter(text=text).exclude(id=pos.id).delete() - else: - value = text.status - elif text.status == 'featured' and value == 'public': - models.Position.objects.filter(text=text).delete() - pos, created = models.Position.objects.get_or_create(text=text, - user=text.user,section='personal') - qs = models.Position.objects.filter(user=text.user, - section='personal') - pos.position = qs.aggregate(Max('position'))['position__max'] + 1 - pos.save() - for u in text.subscribed_users.all(): - pos, created = models.Position.objects.get_or_create(text=text, user=u, - section='public') - qs = models.Position.objects.filter(user=u, section='public') - pos.position = qs.aggregate(Max('position'))['position__max'] + 1 - pos.save() - - text.status = value - elif key == 'name': - data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip() - name = data['name'] - if not name: - name = "Untitled" - num = 1 - while models.Text.objects.filter(name=name, user=text.user).exclude(id=text.id).count()>0: - num += 1 - name = data['name'] + ' [%d]' % num - text.name = name - elif key == 'description': - text.description = ox.sanitize_html(data['description']) - elif key == 'text': - text.text = ox.sanitize_html(data['text']) - - if 'position' in data: - pos, created = models.Position.objects.get_or_create(text=text, user=request.user) - pos.position = data['position'] - pos.section = 'featured' - if text.status == 'private': - pos.section = 'personal' - pos.save() - if 'type' in data: - if data['type'] == 'pdf': - text.type = 'pdf' - else: - text.type = 'html' - if 'posterFrames' in data: - text.poster_frames = tuple(data['posterFrames']) - text.update_icon() - text.save() + text.edit(data, request.user) response['data'] = text.json(user=request.user) else: response = json_response(status=403, text='permission denied') diff --git a/static/js/pandora/folders.js b/static/js/pandora/folders.js index 284df4d3..a59ac390 100644 --- a/static/js/pandora/folders.js +++ b/static/js/pandora/folders.js @@ -82,35 +82,39 @@ pandora.ui.folders = function() { } }) ]; - } else { + } else if(ui.section == 'texts') { extras = [ pandora.$ui.personalListsMenu = Ox.MenuButton({ items: [ - { id: 'new' + folderItem.toLowerCase(), title: 'New ' + folderItem }, + { id: 'newtext', title: 'New Text' }, + { id: 'newpdf', title: 'New PDF' }, {}, - { id: 'delete' + folderItem.toLowerCase(), title: 'Delete Selected ' + folderItem + '...', disabled: !ui[ui.section.slice(0,-1)] } + { id: 'deletetext', title: 'Delete Selected Text...', disabled: !ui.text } ], title: 'edit', - tooltip: 'Manage Personal ' + folderItems, + tooltip: 'Manage Personal Texts', type: 'image' }) .bindEvent({ click: function(data) { var $list = pandora.$ui.folderList[folder.id]; - // fixme: duplicated - if (data.id == 'new' + folderItem.toLowerCase()) { - pandora['add' + folderItem](); - } else if (data.id == 'delete' + folderItem.toLowerCase()) { + if (data.id == 'newtext') { + pandora.addText({type: 'text'}); + } else if (data.id == 'newpdf') { + pandora.addText({type: 'pdf'}); + } else if (data.id == 'deletetext') { pandora.ui.deleteListDialog().open(); } } }) - .bindEvent('pandora_' + ui.section.slice(0,-1), function(data) { + .bindEvent('pandora_text', function(data) { pandora.$ui.personalListsMenu[ data.value && data.value.length ? 'enableItem' : 'disableItem' - ]('delete' + folderItem.toLowerCase()); + ]('deletetext'); }) ]; + } else { + extras = []; } } } else if (folder.id == 'favorite') { diff --git a/static/js/pandora/utils.js b/static/js/pandora/utils.js index 692647d2..ffff484c 100644 --- a/static/js/pandora/utils.js +++ b/static/js/pandora/utils.js @@ -147,9 +147,10 @@ pandora.addList = function() { }).reloadList(); } }; -pandora.addText = function() { +pandora.addText = function(options) { var $folderList = pandora.$ui.folderList.personal; - pandora.api.addText({name: 'Untitled'}, function(result) { + options = options || {}; + pandora.api.addText(options, function(result) { reloadFolder(result.data.id); }); function reloadFolder(newId) {