def create(request): """ Expect a post """ user = _auth(request) if user: data = json.loads(request.POST.get('data', '{}')) data['status'] = 'published' form = SnippetForm(data) if not form.is_valid(): return HttpResponse('VALIDATION') try: lexer_obj = guess_lexer(data['body']) for lex in LEXERS.itervalues(): if lexer_obj.name == lex[1]: lexer = lex[2][0].lower() break except ClassNotFound: lexer = u'text' try: snippet = Snippet( author = user, title = data['title'], description = data['description'], body=data['body'], tags=data['tags'], lexer=lexer, via=data['via'], privacy = data['privacy'], status = data['status'] ) snippet.save() return HttpResponse('SUCCESS') except: return HttpResponse('ERROR') else: return HttpResponse('NOT_AUTHORIZED')
def save(request): if not request.method == 'POST' or not 'title' in request.POST: return HttpResponseForbidden() snippet_title = request.POST['title'] if not snippet_title: return HttpResponseForbidden() try: # load the snippet snippet = Snippet.objects.get(title=snippet_title) if request.user.id != snippet.author.id: return HttpResponseForbidden() except Snippet.DoesNotExist: # create new snippet snippet = Snippet(author=request.user) if not title_available(snippet_title): return HttpResponseForbidden() response_dict = {} if request.is_ajax(): form = SnippetForm(instance=snippet, data=request.POST) if form.is_valid(): snippet.code = request.POST['code'] snippet.language = Language.objects.get(name=request.POST['language']) snippet = form.save() response_dict.update({'success': True}) else: response_dict.update({'errors': form.errors}) return JsonResponse(response_dict)
def _process(request, id=None): """ Create/Update snippet """ if id is not None:#Update snippet = get_object_or_404(Snippet, pk=id) form = SnippetForm(instance=snippet) if request.user.is_staff or request.user.id != snippet.author_id: request.session['flash'] = ['Access denied', 'error']; return HttpResponseRedirect('/accounts/profile/') if 'delete' in request.POST: snippet.delete() request.session['flash'] = ['#%s deleted successfuly' % id, 'sucess'] return HttpResponseRedirect('/accounts/profile/') else:#Create snippet = None form = SnippetForm() if request.method == 'POST': form = SnippetForm(request.POST)#Bounding form to the POST data if not form.is_valid(): # redirect to form with errors return render_to_response('django_snippify/process.html', {'form': form }, context_instance=build_context(request)) formData = form.save(commit = False) formData.pk = id if 'preview' in request.POST: data = {} data['title'] = formData.title; data['preview_body'] = highlight(formData.body, get_lexer_by_name(formData.lexer), HtmlFormatter(cssclass = 'source')) data['lines'] = range(1, formData.body.count('\n') + 2) data['form'] = form data['snippet'] = formData return render_to_response('djnago_snippify/process.html', data, context_instance=build_context(request)) else:#save formData.author = request.user if not formData.lexer: try: lexer = guess_lexer(formData.body) for lex in LEXERS.itervalues(): if lexer.name == lex[1]: formData.lexer = lex[2][0].lower() except ClassNotFound: formData.lexer = 'text' formData.save() if snippet is not None and snippet.body != formData.body: try: last_version = SnippetVersion.objects.order_by('-version').filter(snippet = snippet).all()[0] new_version = SnippetVersion(snippet = snippet, version = last_version.version + 1, body = snippet.body) new_version.save() except: create_version = SnippetVersion(snippet = snippet, version = 1, body = snippet.body) create_version.save() request.session['flash'] = ['#%s %s successfuly' % (formData.pk, 'update' if id is not None else 'created'), 'sucess']; return HttpResponseRedirect('/accounts/profile/') else: return render_to_response('django_snippify/process.html', {'form': form, 'snippet': snippet }, context_instance=build_context(request))