[snowy] Add UserProfile model, ignore first line of content
- From: Brad Taylor <btaylor src gnome org>
- To: svn-commits-list gnome org
- Subject: [snowy] Add UserProfile model, ignore first line of content
- Date: Mon, 18 May 2009 15:37:56 -0400 (EDT)
commit 5725b7811d516184bce1ab8ea7f5714486329dac
Author: Brad Taylor <brad getcoded net>
Date: Mon May 18 15:36:43 2009 -0400
Add UserProfile model, ignore first line of content
---
api/handlers.py | 23 ++++++++++++-----------
notes/admin.py | 4 +++-
notes/models.py | 17 ++++++++++++++---
settings.py | 2 ++
4 files changed, 31 insertions(+), 15 deletions(-)
diff --git a/api/handlers.py b/api/handlers.py
index 7456f8a..0f69aa4 100644
--- a/api/handlers.py
+++ b/api/handlers.py
@@ -60,7 +60,7 @@ class UserHandler(AnonymousBaseHandler):
'api-ref': reverse('note_api_index', kwargs=reverse_args),
'href': reverse('note_index', kwargs=reverse_args),
},
- 'latest-sync-revision' : get_latest_sync_rev(user)
+ 'latest-sync-revision' : user.get_profile().latest_sync_rev,
# TODO: friends
}
@@ -79,7 +79,7 @@ class NotesHandler(BaseHandler):
if request.GET.has_key('since'):
notes = notes.filter(last_sync_rev__gt=int(request.GET['since']))
- response = {'latest-sync-revision': get_latest_sync_rev(user)}
+ response = {'latest-sync-revision': user.get_profile().latest_sync_rev}
if request.GET.has_key('include_notes'):
response['notes'] = [describe_note(n) for n in notes]
else:
@@ -93,13 +93,16 @@ class NotesHandler(BaseHandler):
def clean_date(date):
return parser.parse(date).astimezone(pytz.timezone(settings.TIME_ZONE))
+ def clean_content(content):
+ return content.split('\n', 1)[-1]
+
user = User.objects.get(username=username)
if request.user != user:
return rc.FORBIDDEN
update = json.loads(request.raw_post_data)
- current_sync_rev = get_latest_sync_rev(user)
+ current_sync_rev = user.get_profile().latest_sync_rev
new_sync_rev = current_sync_rev + 1
if update.has_key('latest-sync-revision'):
@@ -118,7 +121,7 @@ class NotesHandler(BaseHandler):
continue
if c.has_key('title'): note.title = c['title']
- if c.has_key('note-content'): note.content = c['note-content']
+ if c.has_key('note-content'): note.content = clean_content(c['note-content'])
if c.has_key('note-content-version'): note.content_version = c['note-content-version']
if c.has_key('last-change-date'): note.user_modified = clean_date(c['last-change-date'])
if c.has_key('last-metadata-change-date'):
@@ -133,10 +136,14 @@ class NotesHandler(BaseHandler):
# TODO: tags
note.save()
+ profile = user.get_profile()
+ profile.latest_sync_rev = new_sync_rev
+ profile.save()
+
# TODO: Would like to be able to return this. Why does it break the update?
# Is it related to how the transaction is handled?
# Must we commit manually first?
-# return {'latest-sync-revision': get_latest_sync_rev(user),
+# return {'latest-sync-revision': new_sync_rev,
# 'notes': [simple_describe_note(n) for n in notes]}
# http://domain/api/1.0/user/notes/id
@@ -181,9 +188,3 @@ def simple_describe_note(note):
},
'title': note.title
}
-
-def get_latest_sync_rev(user):
- max_rev = Note.objects.filter(author=user) \
- .aggregate(Max('last_sync_rev')) \
- ['last_sync_rev__max']
- return max_rev if max_rev != None else -1
diff --git a/notes/admin.py b/notes/admin.py
index 1900fe5..ef15c68 100644
--- a/notes/admin.py
+++ b/notes/admin.py
@@ -17,7 +17,7 @@
from django.contrib import admin
from reversion.admin import VersionAdmin
-from snowy.notes.models import Note
+from snowy.notes.models import *
class NoteAdmin(VersionAdmin):
list_display = ('created', 'author', 'title')
@@ -25,3 +25,5 @@ class NoteAdmin(VersionAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Note, NoteAdmin)
+admin.site.register(NoteTag)
+admin.site.register(UserProfile)
diff --git a/notes/models.py b/notes/models.py
index 0f5f80d..1d496e9 100644
--- a/notes/models.py
+++ b/notes/models.py
@@ -17,6 +17,7 @@
from django.db import models
from django.contrib.auth.models import User
+from django.db.models.signals import post_save
class Note(models.Model):
NOTE_PERMISSIONS = (
@@ -83,6 +84,16 @@ class NoteTag(models.Model):
return self.name
-class VersionMetadata(models.Model):
- revision = models.ForeignKey('reversion.Revision')
- version = models.IntegerField()
+class UserProfile(models.Model):
+ user = models.ForeignKey(User, unique=True)
+ latest_sync_rev = models.IntegerField(default=-1)
+
+def _create_profile(sender, instance, created, **kwargs):
+ """
+ Create a UserProfile object in response to a new User being created.
+ """
+ if not created: return
+ UserProfile.objects.create(user=instance)
+
+post_save.connect(_create_profile, sender=User,
+ dispatch_uid='django.contrib.auth.models.User')
diff --git a/settings.py b/settings.py
index 5564603..e0a9be7 100644
--- a/settings.py
+++ b/settings.py
@@ -105,6 +105,8 @@ INSTALLED_APPS = (
ACCOUNT_ACTIVATION_DAYS = 30
+AUTH_PROFILE_MODULE = 'notes.UserProfile'
+
# local_settings.py can be used to override environment-specific settings
# like database and email that differ between development and production.
try:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]