[snowy] Refactoring, implement PUT



commit 00859875a967512976e4bc8b1f4638075fde8cd4
Author: Brad Taylor <brad getcoded net>
Date:   Fri May 15 15:59:06 2009 -0400

    Refactoring, implement PUT
---
 INSTALL         |    2 +-
 README          |    8 +++++++
 api/handlers.py |   56 +++++++++++++++++++++++++++++++++++++++++++++++-------
 api/urls.py     |    4 +-
 notes/models.py |    6 ++--
 5 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/INSTALL b/INSTALL
index 66ca008..93861ef 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,6 +1,6 @@
 Running Snowy From Your Git Checkout
 --
-0. Install python-libxslt, python-libxml2, and python-sqlite
+0. Install python-libxslt, python-libxml2, python-pytz, and python-sqlite
    (package names somewhat different from distro to distro).
 
 1. Install django from SVN:
diff --git a/README b/README
index 3f8a819..b92e067 100644
--- a/README
+++ b/README
@@ -1,3 +1,11 @@
 Snowy is Tomboy's best friend online.
 
 Snowy is a web-based viewer for your Tomboy notes written in Django.
+
+Requirements
+=================
+ * Python 2.6
+   - python-libxslt
+   - python-libxml2
+   - python-pytz
+   - python-sqlite
diff --git a/api/handlers.py b/api/handlers.py
index 0f95ae0..59ec80e 100644
--- a/api/handlers.py
+++ b/api/handlers.py
@@ -22,7 +22,13 @@ from django.contrib.auth.models import User
 from piston.handler import AnonymousBaseHandler, BaseHandler
 from piston.utils import rc, HttpStatusCode
 
+from datetime import datetime
+from dateutil import parser
+
 from snowy.notes.models import Note
+from snowy import settings
+
+import json, pytz
 
 class catch_and_return(object):
     def __init__(self, err, response):
@@ -37,6 +43,7 @@ class catch_and_return(object):
                 return self.response
         return wrapper
 
+# http://domain/api/1.0/user
 class UserHandler(AnonymousBaseHandler):
     allow_methods = ('GET',)
 
@@ -51,18 +58,23 @@ class UserHandler(AnonymousBaseHandler):
                 'api-ref': reverse('note_api_index', kwargs=reverse_args),
                 'href': reverse('note_index', kwargs=reverse_args),
             },
+            # TODO: friends
         }
 
-class ListNoteRefsHandler(BaseHandler):
+# http://domain/api/1.0/user/notes
+class NotesHandler(BaseHandler):
     allow_methods = ('GET',)
 
+    # TODO: Handle since param
+    # TODO: Permissions
     @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.GET.has_key('include_notes'):
-            return {'notes': [describe_note(n) for n in Note.objects.filter(author=user)] }
+            return {'notes': [describe_note(n) for n in notes] }
         else:
-            return {'note-refs': [{
+            return {'notes': [{
                     'guid': n.guid,
                     'ref': {
                         'api-ref': reverse('note_api_detail', kwargs={
@@ -74,9 +86,34 @@ class ListNoteRefsHandler(BaseHandler):
                     },
                     'title': n.title,
                 }
-                for n in Note.objects.filter(author=user)
+                for n in notes
             ]}
 
+    # TODO: Permissions
+    # TODO: Transactions
+    @catch_and_return(ObjectDoesNotExist, rc.NOT_HERE)
+    @catch_and_return(KeyError, rc.BAD_REQUEST)
+    def update(self, request, username):
+        def clean_date(date):
+            return parser.parse(date).astimezone(pytz.timezone(settings.TIME_ZONE))
+
+        user = User.objects.get(username=username)
+        changes = json.loads(request.raw_post_data)['note-changes']
+        for c in changes:
+            note, created = Note.objects.get_or_create(author=user, guid=c['guid'])
+            if c.has_key('title'): note.title = c['title']
+            if c.has_key('note-content'): note.content = c['note-content']
+            if c.has_key('last-change-date'): note.user_modified = clean_date(c['last-change-date'])
+            if c.has_key('last-metadata-change-date'):
+                note.modified = clean_date(c['last-metadata-change-date'])
+            else:
+                note.modified = datetime.now()
+            if c.has_key('create-date'): note.created = clean_date(c['create-date'])
+            if c.has_key('open-on-startup'): note.open_on_startup = (c['open-on-startup'] == 'true')
+
+            note.save()
+
+# http://domain/api/1.0/user/notes/id/slug
 class NoteHandler(BaseHandler):
     allow_methods = ('GET',)
     model = Note
@@ -87,15 +124,18 @@ class NoteHandler(BaseHandler):
         note = Note.objects.get(pk=note_id, slug=slug)
         return {'note': [describe_note(note)]}
 
-
 def describe_note(note):
+    def local_iso(date):
+        return date.replace(tzinfo=pytz.timezone(settings.TIME_ZONE)) \
+                   .isoformat()
+
     return {
         'guid': note.guid,
         'title': note.title,
         'note-content': note.content,
-        'last-change-date': note.user_modified,
-        'last-metadata-change-date': note.modified,
-        'create-date': note.created,
+        'last-change-date': local_iso(note.user_modified),
+        'last-metadata-change-date': local_iso(note.modified),
+        'create-date': local_iso(note.created),
         'open-on-startup': note.open_on_startup,
         'tags': [t.name for t in note.tags.all()],
     }
diff --git a/api/urls.py b/api/urls.py
index 3c4c098..6a3427b 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -21,12 +21,12 @@ from piston.resource import Resource
 from snowy.api.handlers import *
 
 user_handler = Resource(UserHandler)
-list_note_refs_handler = Resource(ListNoteRefsHandler)
+notes_handler = Resource(NotesHandler)
 note_handler = Resource(NoteHandler)
 
 urlpatterns = patterns('',
     # 1.0 API methods
     url(r'1.0/(?P<username>\w+)/notes/(?P<note_id>\d+)/(?P<slug>[^/]+)/$', note_handler, name='note_api_detail'),
-    url(r'1.0/(?P<username>\w+)/notes/$', list_note_refs_handler, name='note_api_index'),
+    url(r'1.0/(?P<username>\w+)/notes/$', notes_handler, name='note_api_index'),
     url(r'1.0/(?P<username>\w+)/$', user_handler),
 )
diff --git a/notes/models.py b/notes/models.py
index f2c5f16..d049466 100644
--- a/notes/models.py
+++ b/notes/models.py
@@ -28,13 +28,13 @@ class Note(models.Model):
     author = models.ForeignKey(User)
 
     created = models.DateTimeField(auto_now_add=True)
-    modified = models.DateTimeField(auto_now=True)
+    modified = models.DateTimeField(auto_now_add=True)
     user_modified = models.DateTimeField(auto_now_add=True)
 
-    title = models.CharField(max_length=128)
+    title = models.CharField(max_length=128, blank=True)
     slug = models.SlugField(blank=True)
     content = models.TextField(blank=True)
-    content_version = models.CharField(max_length=10)
+    content_version = models.CharField(max_length=10, blank=True)
 
     tags = models.ManyToManyField('NoteTag', null=True, blank=True)
     permissions = models.IntegerField(choices=NOTE_PERMISSIONS,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]