[snowy] Add a NoteManager to consolidate note permissions
- From: Brad Taylor <btaylor src gnome org>
- To: svn-commits-list gnome org
- Subject: [snowy] Add a NoteManager to consolidate note permissions
- Date: Sun, 31 May 2009 22:23:46 -0400 (EDT)
commit 59e10b7e5879462dd8fb7724e170bcfb8e9f6584
Author: Brad Taylor <brad getcoded net>
Date: Sun May 31 22:22:29 2009 -0400
Add a NoteManager to consolidate note permissions
---
api/handlers.py | 27 ++++++++++++---------------
notes/managers.py | 26 ++++++++++++++++++++++++++
notes/models.py | 4 ++++
notes/templates/notes/note_detail.html | 4 ++--
notes/views.py | 16 +++++++---------
5 files changed, 51 insertions(+), 26 deletions(-)
diff --git a/api/handlers.py b/api/handlers.py
index ca3a931..efc746c 100644
--- a/api/handlers.py
+++ b/api/handlers.py
@@ -73,16 +73,13 @@ class NotesHandler(BaseHandler):
@catch_and_return(ObjectDoesNotExist, rc.NOT_HERE)
def read(self, request, username):
- user = User.objects.get(username=username)
- notes = Note.objects.filter(author=user)
-
- if request.user != user:
- notes = notes.filter(permissions=1) # Public only
+ author = User.objects.get(username=username)
+ notes = Note.objects.user_viewable(request.user, author)
if request.GET.has_key('since'):
notes = notes.filter(last_sync_rev__gt=int(request.GET['since']))
- response = {'latest-sync-revision': user.get_profile().latest_sync_rev}
+ response = {'latest-sync-revision': author.get_profile().latest_sync_rev}
if request.GET.has_key('include_notes'):
response['notes'] = [describe_note(n) for n in notes]
else:
@@ -96,14 +93,14 @@ class NotesHandler(BaseHandler):
def clean_date(date):
return parser.parse(date).astimezone(pytz.timezone(settings.TIME_ZONE))
- user = User.objects.get(username=username)
- if request.user != user:
+ author = User.objects.get(username=username)
+ if request.user != author:
return rc.FORBIDDEN
update = json.loads(request.raw_post_data)
changes = update['note-changes']
- current_sync_rev = user.get_profile().latest_sync_rev
+ current_sync_rev = author.get_profile().latest_sync_rev
new_sync_rev = current_sync_rev + 1
if update.has_key('latest-sync-revision'):
@@ -114,7 +111,7 @@ class NotesHandler(BaseHandler):
return rc.BAD_REQUEST
for c in changes:
- note, created = Note.objects.get_or_create(author=user,
+ note, created = Note.objects.get_or_create(author=author,
guid=c['guid'])
if c.has_key('command') and c['command'] == 'delete':
@@ -135,21 +132,21 @@ class NotesHandler(BaseHandler):
if c.has_key('tags'):
note.tags.clear()
for tag_name in c['tags']:
- tag, created = NoteTag.objects.get_or_create(author=user,
+ tag, created = NoteTag.objects.get_or_create(author=author,
name=tag_name)
note.tags.add(tag)
note.last_sync_rev = new_sync_rev
note.save()
- profile = user.get_profile()
+ profile = author.get_profile()
if len(changes) > 0:
profile.latest_sync_rev = new_sync_rev
profile.save()
return {
'latest-sync-revision': profile.latest_sync_rev,
- 'notes': [simple_describe_note(n) for n in Note.objects.filter(author=user)]
+ 'notes': [simple_describe_note(n) for n in Note.objects.filter(author=author)]
}
@@ -160,9 +157,9 @@ class NoteHandler(BaseHandler):
@catch_and_return(ObjectDoesNotExist, rc.NOT_HERE)
def read(self, request, username, note_id, slug):
- user = User.objects.get(username=username)
+ author = User.objects.get(username=username)
note = Note.objects.get(pk=note_id, slug=slug)
- if request.user != user and note.permissions == 0:
+ if request.user != author and note.permissions == 0:
return rc.FORBIDDEN
return {'note': [describe_note(note)]}
diff --git a/notes/managers.py b/notes/managers.py
new file mode 100644
index 0000000..cfc4722
--- /dev/null
+++ b/notes/managers.py
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2009 Brad Taylor <brad getcoded net>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+from django.db import models
+
+class NoteManager(models.Manager):
+ def user_viewable(self, request_user, author):
+ notes = self.filter(author=author)
+ if request_user != author:
+ # Public notes only
+ notes.filter(permissions=1)
+ return notes
diff --git a/notes/models.py b/notes/models.py
index 027e50e..8aa48e0 100644
--- a/notes/models.py
+++ b/notes/models.py
@@ -23,6 +23,8 @@ from django.db import models
from autoslug.fields import AutoSlugField
+from snowy.notes.managers import NoteManager
+
class Note(models.Model):
NOTE_PERMISSIONS = (
(0, 'Private'), (1, 'Public'),
@@ -51,6 +53,8 @@ class Note(models.Model):
last_sync_rev = models.IntegerField(default=-1)
+ objects = NoteManager()
+
class Meta:
get_latest_by = 'user_modified'
unique_together = (('author', 'title'), ('author', 'guid'), )
diff --git a/notes/templates/notes/note_detail.html b/notes/templates/notes/note_detail.html
index cb1ec37..67c2681 100644
--- a/notes/templates/notes/note_detail.html
+++ b/notes/templates/notes/note_detail.html
@@ -13,7 +13,7 @@
{% block sidebar %}
{{ block.super }}
-{% user_notes_list request user as all_notes %}
+{% user_notes_list request author as all_notes %}
<div id="sidebar-note-list">
<ul>
{% for n in all_notes %}
@@ -29,7 +29,7 @@
<div id="sidebar-notebook-list">
<h3>{% trans "Notebooks" %}</h3>
<ul>
-{% user_notebook_list request user as all_notebooks %}
+{% user_notebook_list request author as all_notebooks %}
{% for n in all_notebooks %}
<li class="notebook-item"><a href="#">{{ n.get_name_for_display }}</a></li>
{% endfor %}
diff --git a/notes/views.py b/notes/views.py
index 04f98cf..68fb5a2 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -26,27 +26,25 @@ from snowy import settings
def note_index(request, username,
template_name='note/note_index.html'):
- user = get_object_or_404(User, username=username)
+ author = get_object_or_404(User, username=username)
# TODO: retrieve the last open note from the user
- last_modified = Note.objects.filter(author=user) \
+ last_modified = Note.objects.user_viewable(request.user, author) \
.order_by('-user_modified')
- if request.user != user:
- last_modified = last_modified.filter(permissions=1)
if last_modified.count() > 0:
return HttpResponseRedirect(last_modified[0].get_absolute_url())
# TODO: Instruction page to tell user to either sync or create a new note
return render_to_response(template_name,
- {'user': user},
+ {'author': author},
context_instance=RequestContext(request))
def note_detail(request, username, note_id, slug='',
template_name='notes/note_detail.html'):
- user = get_object_or_404(User, username=username)
- note = get_object_or_404(Note, pk=note_id, author=user)
+ author = get_object_or_404(User, username=username)
+ note = get_object_or_404(Note, pk=note_id, author=author)
- if request.user != user and note.permissions == 0:
+ if request.user != author and note.permissions == 0:
return HttpResponseForbidden()
if note.slug != slug:
@@ -76,5 +74,5 @@ def note_detail(request, username, note_id, slug='',
return render_to_response(template_name,
{'title': note.title,
'note': note, 'body': body,
- 'request': request, 'user': user},
+ 'request': request, 'author': author},
context_instance=RequestContext(request))
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]