Skip to content
Browse files

Modifications to both the client side and server to support:

* Pushing objects with relationships to non-Nudge configured models
* removed the entire process_item method, instead we're leaning on Django's deserialization functions.
  • Loading branch information...
1 parent 91cb0cb commit 5a8cd5dddaa365f42801d9d018da4d80d2a979c0 @rosskarchner committed Jun 8, 2012
Showing with 32 additions and 37 deletions.
  1. +19 −5 src/nudge/client.py
  2. +13 −32 src/nudge/server.py
View
24 src/nudge/client.py
@@ -1,4 +1,5 @@
import datetime, hashlib, json, os, pickle, urllib, urllib2
+from urlparse import urljoin
from Crypto.Cipher import AES
@@ -8,6 +9,8 @@
from nudge.exceptions import *
from utils import related_objects
+
+
"""
client.py
@@ -43,17 +46,26 @@ def serialize_batch(key, batch):
related= related_objects(obj)
if related:
objects_exploded=related + objects_exploded
+
#append the original list to the exploded list
- objects_exploded=[obj for obj in objects_exploded if obj in version_lookup]+version_lookup.keys()
+ objects_exploded=objects_exploded+version_lookup.keys()
objects_imploded=[]
#filter out duplicates and related items that aren't part of this push
+
for obj in objects_exploded:
- if obj not in objects_imploded and obj in version_lookup:
+ if obj not in objects_imploded:
objects_imploded.append(obj)
# put the batch back together. Now, related objects are pushed in a sensible order!
+
+ unversioned=[]
for obj in objects_imploded:
- versions.append(version_lookup[obj].version)
- batch_items = serializers.serialize("json", versions)
+
+ if obj in version_lookup:
+ versions.append(version_lookup[obj].version)
+ else:
+ unversioned.append(obj)
+
+ batch_items = serializers.serialize("json", unversioned + versions)
b_plaintext = pickle.dumps({ 'id':batch.id, 'title':batch.title, 'items':batch_items })
return encrypt_batch(key, b_plaintext)
@@ -62,8 +74,10 @@ def send_command(target, data):
"""
sends a nudge api command
"""
- url = "%s/nudge-api/%s/" % (SETTINGS.remote_address, target)
+ url= urljoin(SETTINGS.remote_address, '/nudge-api/'+ target+'/')
+
req = urllib2.Request(url, urllib.urlencode(data))
+
response = urllib2.urlopen(req)
return response
View
45 src/nudge/server.py
@@ -10,20 +10,14 @@
from nudge.models import Setting
from django.contrib.contenttypes.models import ContentType
+from reversion.models import Version
"""
server.py
commands received from nudge client
"""
-
-
-def get_model(model_str):
- """returns model object based on string provided by batch item"""
- app_name = model_str.split('.')[0]
- model_name = model_str.split('.')[1]
- return ContentType.objects.get_by_natural_key(app_name, model_name).model_class()
def valid_batch(batch_info):
"""returns whether a batch format is valid"""
@@ -37,37 +31,24 @@ def decrypt(key, ciphertext, iv):
plaintext = decobj.decrypt(ciphertext)
return plaintext
-def process_item(item):
- """
- examines an item in a batch, determines if it should be added, updated or
- deleted and performs the command
- """
-
- item_content = json.loads(item['fields']['serialized_data'])[0]
- model_obj = get_model(item_content['model'])
- id = item_content['pk']
- fields = convert_keys_to_string(item_content['fields'])
-
- if item['fields']['type'] < 2:
- # Add or Update
- fields=caster(fields, model_obj)
- new_item = model_obj(pk=id, **fields)
- new_item.save()
- return True
- else:
- # Delete
- del_item = model_obj.objects.get(pk=id)
- del_item.delete()
- return True
-
def process_batch(key, batch_info, iv):
"""
loops through items in a batch and processes them
"""
+
batch_info = pickle.loads(decrypt(key, batch_info, iv.decode('hex')))
+
if valid_batch(batch_info):
- items = json.loads(batch_info['items'])
+ items = serializers.deserialize("json", batch_info['items'])
success = True
+
for item in items:
- success = success and process_item(item)
+ item.save()
+ if type(item.object) == Version:
+ version=item.object
+ if version.type == 2:
+ version.object.delete()
+ else:
+ item.object.revert()
+
return success

0 comments on commit 5a8cd5d

Please sign in to comment.
Something went wrong with that request. Please try again.