Skip to content

Commit

Permalink
save the tag cloud upon initial generation, in the backend, and retur…
Browse files Browse the repository at this point in the history
…n the cloud id
  • Loading branch information
jessykate committed Nov 14, 2010
1 parent a498d64 commit 87a2efa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 45 deletions.
31 changes: 23 additions & 8 deletions 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:
Expand Down Expand Up @@ -387,15 +390,27 @@ def tag_cloud(dist, id_ = "", class_ = "", width=None, height=None,
style += '''
</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',)
Expand Down
1 change: 0 additions & 1 deletion frontend/urls.py
Expand Up @@ -10,7 +10,6 @@
url(r'^api', direct_to_template, {'template': 'frontend/api.html'}),

url(r'^cloud/(?P<cloud_id>\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),
Expand Down
75 changes: 40 additions & 35 deletions frontend/views.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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('<b>API Error</b><br><br>'+content)
return
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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


2 changes: 1 addition & 1 deletion templates/frontend/tagcloud_display.html
Expand Up @@ -47,7 +47,7 @@
<p>A static cloud stays the same as you see it right now. A dynamic cloud will
update every time you visit it.</p>
</fieldset>
<input type="hidden" name="data" value="{{ tagcloud_json|escape }}">
<input type="hidden" name="cloud_id" value="{{ cloud_id }}">
</form>
</div>
<div id="tagcloud_wrapper">
Expand Down

0 comments on commit 87a2efa

Please sign in to comment.