Skip to content

Instantly share code, notes, and snippets.

@0x46616c6b
Created January 14, 2011 10:36
Show Gist options
  • Save 0x46616c6b/779458 to your computer and use it in GitHub Desktop.
Save 0x46616c6b/779458 to your computer and use it in GitHub Desktop.
pragmatical layout of the manage view. for external application or other output formats (xml, ...) we should use an api.
@login_required(redirect_field_name='redirect')
def manage(request):
if (request.method == 'POST'):
format = 'json'
if ('format' in request.POST):
format = request.POST['format']
if ('submit' in request.POST) and (format == 'json'):
if ('new_entry' in request.POST):
# format: { "text" : "text body", "isPublic" : 0|1 }
entry = json.loads(request.POST['new_entry'])
entry_text = escape(entry.get('text'))
entry_published = datetime.now()
entry_isPublic = int(entry.get('isPublic'))
new_entry = Entry(text=entry_text, published=entry_published, isPublic=entry_isPublic)
new_entry.save()
elif ('new_pin' in request.POST):
# format: { "pin" : "pin value" }
pin = json.loads(request.POST['new_pin'])
new_pin = Config.objects.get(key='pin')
new_pin.value = pin.get('pin')
new_pin.save()
elif ('del_entry' in request.POST):
# format: { "id" : entry_id }
entry = json.loads(request.POST['del_entry'])
entry_id = int(entry.get('id'))
Entry.objects.filter(id=entry_id).delete()
if (request.method == 'GET'):
format = 'json'
if ('format' in request.POST):
format = request.POST['format']
if ('function' in request.GET) and (format == 'json'):
json_serializer = serializers.get_serializer("json")()
if (request.GET['function'] == 'get_all_entries'):
all_entries = Entry.objects.all().order_by('-published')
return HttpResponse(json_serializer.serialize(all_entries, ensure_ascii=False), mimetype="application/json")
elif (request.GET['function'] == 'get_latest_entry'):
latest_entry = Entry.objects.all().order_by('-published')[:1]
return HttpResponse(json_serializer.serialize(latest_entry, ensure_ascii=False), mimetype="application/json")
elif (request.GET['function'] == 'get_pin'):
pin = Config.objects.filter(key='pin')
return HttpResponse(json_serializer.serialize(pin, ensure_ascii=False), mimetype="application/json")
else:
not_defined_message = "This method is not defined!"
return HttpResponse(not_defined_message, mimetype="text/plain")
return render_to_response('manage/index.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment