From 87a2efafa7d4079664caf67aa9fdac79ac50aac6 Mon Sep 17 00:00:00 2001 From: jessykate Date: Sun, 14 Nov 2010 00:12:07 -0500 Subject: [PATCH] save the tag cloud upon initial generation, in the backend, and return the cloud id --- api/handlers.py | 31 +++++++--- frontend/urls.py | 1 - frontend/views.py | 75 +++++++++++++----------- templates/frontend/tagcloud_display.html | 2 +- 4 files changed, 64 insertions(+), 45 deletions(-) diff --git a/api/handlers.py b/api/handlers.py index 56db02c..9006256 100644 --- a/api/handlers.py +++ b/api/handlers.py @@ -1,8 +1,11 @@ +from django.conf import settings from piston.handler import BaseHandler from piston.utils import rc import nltk, urllib, urllib2 -import random, math -from lib import html_unescape +import pymongo +import random, math, datetime +from lib import html_unescape, bitly_shorten + try: import json except: @@ -387,15 +390,27 @@ def tag_cloud(dist, id_ = "", class_ = "", width=None, height=None, style += ''' ''' - # assemble the metadata + # assemble the response + oid = pymongo.objectid.ObjectId() + uid = str(oid) + long_url = settings.HOME_PAGE + '/cloud/' + uid + short_url = bitly_shorten(long_url) metadata = { - 'utc_created': datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") + 'utc_created': datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S"), 'total_tags' : words_in_cloud, + 'short_url' : short_url, } - - # assemble and return the response - resp = {'body': body, 'style': style, 'metadata': metadata} - return resp + record = {'_id': oid, + 'body': body, + 'style': style, + 'metadata': metadata} + + # save to the database + con = pymongo.Connection() + collection = con.wordapi.tagclouds + collection.insert(record) + + return record class UrlTokenHandler(GeneralHandler): allowed_methods = ('GET',) diff --git a/frontend/urls.py b/frontend/urls.py index 77a80d8..3481a2e 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -10,7 +10,6 @@ url(r'^api', direct_to_template, {'template': 'frontend/api.html'}), url(r'^cloud/(?P\w+)', cloud), - url(r'^document/save$', save), url(r'^topic/new$', new_topic), url(r'^document/new$', new_document), url(r'^collecion/new$', new_collection), diff --git a/frontend/views.py b/frontend/views.py index 3464ed3..f734455 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -7,6 +7,7 @@ from frontend.forms import TagCloudForm, NewTopicForm import urllib, httplib2, datetime import pymongo +import pymongo from pymongo.objectid import ObjectId from lib import bitly_shorten try: @@ -73,6 +74,8 @@ def new_document(request): js = json.loads(content) body = js['body'] style = js['style'] + metadata = js['metadata'] + cloud_id = js['_id'] except: return HttpResponse('API Error

'+content) return @@ -92,11 +95,10 @@ def new_document(request): # note that in the template, body and style need to be given the 'safe' # filter so that the markup will be interpreted. otherwise it will be # escaped and displayed as strings. + return HttpResponseRedirect("/cloud/%s" % cloud_id) return render_to_formtemplate(request, 'frontend/tagcloud_display.html', {'body' : body, 'style' : style, 'generator_url' : generator_url, - 'generator_url_display': generator_url_display, - 'tagcloud_json': content}) - + 'generator_url_display': generator_url_display}) else: print request.POST @@ -107,38 +109,6 @@ def new_document(request): 'tagcloud_form' : form }) -def save(request): - if request.method == 'GET': - return HttpResponseRedirect("/") - - # save data and url to db - data = request.POST.get('data') - record = json.loads(data) - record['created'] = datetime.datetime.now() - record['update_type'] = request.POST.get('update') - if request.POST.get('name', None): - record['cloud_name'] = request.POST.get('name') - # record['owner'] = username - # record['permissions'] = public - - # create the unique id for this record - oid = ObjectId() - uid = str(oid) - record['_id'] = oid - # create short link - long_url = settings.HOME_PAGE + '/cloud/' + uid - short_url = bitly_shorten(long_url) - print short_url - record['short_url'] = short_url - con = pymongo.Connection() - collection = con.wordapi.tagclouds - collection.insert(record) - - # redirect to the cloud's page - return HttpResponseRedirect("/cloud/%s" % uid) - - # a dynamic tagcloud saves the source information and rendering preferences - def cloud(request, cloud_id): con = pymongo.Connection() collection = con.wordapi.tagclouds @@ -181,3 +151,38 @@ def topic(request, topic_id): def new_collection(request): pass + + +#def save(request): +# if request.method == 'GET': +# return HttpResponseRedirect("/") +# +# # save data and url to db +# data = request.POST.get('data') +# record = json.loads(data) +# record['created'] = datetime.datetime.now() +# record['update_type'] = request.POST.get('update') +# if request.POST.get('name', None): +# record['cloud_name'] = request.POST.get('name') +# # record['owner'] = username +# # record['permissions'] = public +# +# # create the unique id for this record +# oid = ObjectId() +# uid = str(oid) +# record['_id'] = oid +# # create short link +# long_url = settings.HOME_PAGE + '/cloud/' + uid +# short_url = bitly_shorten(long_url) +# print short_url +# record['short_url'] = short_url +# con = pymongo.Connection() +# collection = con.wordapi.tagclouds +# collection.insert(record) +# +# # redirect to the cloud's page +# return HttpResponseRedirect("/cloud/%s" % uid) +# +# # a dynamic tagcloud saves the source information and rendering preferences + + diff --git a/templates/frontend/tagcloud_display.html b/templates/frontend/tagcloud_display.html index 6f892b7..f55668f 100644 --- a/templates/frontend/tagcloud_display.html +++ b/templates/frontend/tagcloud_display.html @@ -47,7 +47,7 @@

A static cloud stays the same as you see it right now. A dynamic cloud will update every time you visit it.

- +