Mercurial > agora-palash
comparison apps/snippet/views.py @ 102:f872c643b056
Updates to snippet functionality (see details)
Sorry about the large commit, but it was difficult to break it up as a lot
of new functionality was introduced. Most of it is specific to the snippet
feature but there are some other changes as well.
Commit highlights:
* Added the ability to switch the syntax highlighting colour scheme when
viewing a snippet. This is currently done on a per-snippet basis only,
but eventually it will be possible to set a default in your profile to
have all the snippets you view use that colour scheme. There are currently
8 different colour schemes, all of which were taken from the default
pygments stylesheets (some were modified).
* Added a "num_views" field to the Snippet model, with the field being
incremented any time the snippet view is called (raw or regular view).
* Created a simple "explore" view that lists the recently-posted snippets.
Will implement pagination and sorting by other attributes ("popularity",
for example, based on number of views) as well.
* Added a post-save hook to the User model to ensure that a Profile is created
for every user as soon as the User itself is created. This alleviates the
need for a get_profile method that checks if the user has a profile or not
and creates one if necessary. (The code is currently still there, will be
cleaned up soon).
* Added back the wordwrap toggling feature. Currently, if you want to enable
word-wrapping, the line numbers have to be hidden in order to ensure that
the lines and their numbers don't go out of sync. This will be fixed soon.
* History/diff view is back
* And some other minor cosmetic changes.
Note: since some existing models have been changed, you'll likely need to
delete the existing sqlite database before running syncdb. The alternative
is to determine the necessary column changes/additions and run the SQL
query yourself.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Fri, 31 Aug 2012 02:53:22 -0400 |
parents | a8da60d611f7 |
children | 4f6977515048 |
comparison
equal
deleted
inserted
replaced
101:a8da60d611f7 | 102:f872c643b056 |
---|---|
1 import difflib | 1 import difflib |
2 | 2 |
3 from django.shortcuts import render_to_response, \ | 3 from django.shortcuts import render_to_response, \ |
4 get_object_or_404, get_list_or_404 | 4 get_object_or_404, get_list_or_404, render |
5 from django.template.context \ | 5 from django.template.context \ |
6 import RequestContext | 6 import RequestContext |
7 from django.http \ | 7 from django.http \ |
8 import HttpResponseRedirect, HttpResponseBadRequest, \ | 8 import HttpResponseRedirect, HttpResponseBadRequest, \ |
9 HttpResponse, HttpResponseForbidden | 9 HttpResponse, HttpResponseForbidden |
19 LEXER_LIST | 19 LEXER_LIST |
20 from agora.apps.pygments_style.models import PygmentsStyle | 20 from agora.apps.pygments_style.models import PygmentsStyle |
21 | 21 |
22 | 22 |
23 def snippet_explore(request): | 23 def snippet_explore(request): |
24 pass | 24 context = { |
25 'recent_snippets': Snippet.objects.all()[:20] | |
26 } | |
27 | |
28 return render(request, 'snippet/explore.html', context) | |
25 | 29 |
26 | 30 |
27 def snippet_new(request, template_name='snippet/snippet_new.djhtml'): | 31 def snippet_new(request, template_name='snippet/snippet_new.djhtml'): |
28 | 32 |
29 if request.method == "POST": | 33 if request.method == "POST": |
57 def snippet_details(request, snippet_id, | 61 def snippet_details(request, snippet_id, |
58 template_name='snippet/snippet_details.djhtml', | 62 template_name='snippet/snippet_details.djhtml', |
59 is_raw=False): | 63 is_raw=False): |
60 | 64 |
61 snippet = get_object_or_404(Snippet, secret_id=snippet_id) | 65 snippet = get_object_or_404(Snippet, secret_id=snippet_id) |
66 snippet.num_views += 1 | |
67 snippet.save() | |
62 | 68 |
63 tree = snippet.get_root() | 69 tree = snippet.get_root() |
64 tree = tree.get_descendants(include_self=True) | 70 tree = tree.get_descendants(include_self=True) |
65 | 71 |
66 new_snippet_initial = { | 72 new_snippet_initial = { |
67 'content': snippet.content, | 73 'content': snippet.content, |
74 'lexer': snippet.lexer, | |
68 } | 75 } |
69 | 76 |
70 if request.method == "POST": | 77 if request.method == "POST": |
71 snippet_form = SnippetForm(data=request.POST, | 78 snippet_form = SnippetForm(data=request.POST, |
72 request=request, | 79 request=request, |
73 initial=new_snippet_initial) | 80 initial=new_snippet_initial) |
74 if snippet_form.is_valid(): | 81 if snippet_form.is_valid(): |
75 request, new_snippet = snippet_form.save(parent=snippet) | 82 request, new_snippet = snippet_form.save(parent=snippet) |
76 return HttpResponseRedirect(new_snippet.get_absolute_url()) | 83 return HttpResponseRedirect(new_snippet.get_absolute_url()) |
77 else: | 84 else: |
78 snippet_form = SnippetForm(initial=new_snippet_initial, request=request) | 85 snippet_form = SnippetForm(initial=new_snippet_initial, |
86 request=request) | |
87 | |
88 if request.user.is_authenticated(): | |
89 default_pygments_style = request.user.get_profile().pygments_style | |
90 else: | |
91 default_pygments_style = PygmentsStyle.objects.get(pk=1) | |
79 | 92 |
80 template_context = { | 93 template_context = { |
81 'snippet_form': snippet_form, | 94 'snippet_form': snippet_form, |
82 'snippet': snippet, | 95 'snippet': snippet, |
83 'lines': range(snippet.get_linecount()), | 96 'lines': range(snippet.get_linecount()), |
84 'tree': tree, | 97 'tree': tree, |
85 'language': dict(LEXER_LIST)[snippet.lexer], | 98 'language': dict(LEXER_LIST)[snippet.lexer], |
99 'pygments_styles': PygmentsStyle.objects.all(), | |
100 'default_style': default_pygments_style, | |
101 'no_descendants': len(tree) == 1, | |
86 } | 102 } |
87 | 103 |
88 response = render_to_response( | 104 response = render_to_response( |
89 template_name, | 105 template_name, |
90 template_context, | 106 template_context, |