-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example site plus base application
git-svn-id: http://django-paste.googlecode.com/svn/trunk/dpaste@5 1e28be48-2e37-0410-b882-833e1ed5e216
- Loading branch information
1 parent
8c681d3
commit 59ce6ca
Showing
12 changed files
with
563 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from django import newforms as forms | ||
from django.conf import settings | ||
from dpaste.models import Snippet | ||
from dpaste.highlight import LEXER_LIST_ALL, LEXER_LIST, LEXER_DEFAULT | ||
|
||
#=============================================================================== | ||
# Snippet Form and Handling | ||
#=============================================================================== | ||
|
||
class SnippetForm(forms.ModelForm): | ||
|
||
lexer = forms.ChoiceField( | ||
choices=LEXER_LIST, | ||
initial=LEXER_DEFAULT, | ||
) | ||
|
||
def __init__(self, request, *args, **kwargs): | ||
super(SnippetForm, self).__init__(*args, **kwargs) | ||
self.request = request | ||
if self.request.session.get('userprefs') and \ | ||
self.request.session['userprefs'].get('display_all_lexer', False): | ||
self.fields['lexer'].choices = LEXER_LIST_ALL | ||
self.fields['lexer'].help_text = u'Youre displaying the whole bunch of lexers!' | ||
self.fields['author'].initial = self.request.session['userprefs'].get('default_name', '') | ||
|
||
def save(self, parent=None, *args, **kwargs): | ||
|
||
# Set parent snippet | ||
if parent: | ||
print dir(self.instance) | ||
self.instance.parent = parent | ||
|
||
# Save snippet in the db | ||
super(SnippetForm, self).save(*args, **kwargs) | ||
|
||
# Add the snippet to the user session list | ||
if self.request.session.get('snippet_list', False): | ||
if len(self.request.session['snippet_list']) >= getattr(settings, 'MAX_SNIPPETS_PER_USER', 10): | ||
self.request.session['snippet_list'].pop(0) | ||
self.request.session['snippet_list'] += [self.instance.pk] | ||
else: | ||
self.request.session['snippet_list'] = [self.instance.pk] | ||
|
||
return self.request, self.instance | ||
|
||
class Meta: | ||
model = Snippet | ||
fields = ( | ||
'title', | ||
'content', | ||
'author', | ||
'lexer', | ||
) | ||
|
||
|
||
#=============================================================================== | ||
# User Settings | ||
#=============================================================================== | ||
|
||
USERPREFS_FONT_CHOICES = [(None, 'Default')] + [ | ||
(i, i) for i in sorted(( | ||
'Monaco', | ||
'Bitstream Vera Sans Mono', | ||
'Courier New', | ||
'Consolas', | ||
)) | ||
] | ||
|
||
USERPREFS_SIZES = [(None, 'Default')] + [(i, '%dpx' % i) for i in range(5, 25)] | ||
|
||
class UserSettingsForm(forms.Form): | ||
|
||
default_name = forms.CharField(required=False) | ||
display_all_lexer = forms.BooleanField( | ||
required=False, | ||
widget=forms.CheckboxInput | ||
) | ||
font_family = forms.ChoiceField(required=False, choices=USERPREFS_FONT_CHOICES) | ||
font_size = forms.ChoiceField(required=False, choices=USERPREFS_SIZES) | ||
line_height = forms.ChoiceField(required=False, choices=USERPREFS_SIZES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from pygments.lexers import get_all_lexers, get_lexer_by_name | ||
from pygments.styles import get_all_styles | ||
from pygments.formatters import HtmlFormatter | ||
from pygments import highlight | ||
|
||
LEXER_LIST_ALL = sorted([(i[1][0], i[0]) for i in get_all_lexers()]) | ||
LEXER_LIST = ( | ||
('apacheconf', 'ApacheConf'), | ||
('as', 'ActionScript'), | ||
('bash', 'Bash'), | ||
('bbcode', 'BBCode'), | ||
('c', 'C'), | ||
('cpp', 'C++'), | ||
('csharp', 'C#'), | ||
('css', 'CSS'), | ||
('diff', 'Diff'), | ||
('django', 'Django/Jinja'), | ||
('erlang', 'Erlang'), | ||
('html', 'HTML'), | ||
('ini', 'INI'), | ||
('irc', 'IRC logs'), | ||
('java', 'Java'), | ||
('js', 'JavaScript'), | ||
('jsp', 'Java Server Page'), | ||
('lua', 'Lua'), | ||
('make', 'Makefile'), | ||
('perl', 'Perl'), | ||
('php', 'PHP'), | ||
('pot', 'Gettext Catalog'), | ||
('pycon', 'Python console session'), | ||
('pytb', 'Python Traceback'), | ||
('python', 'Python'), | ||
('python3', 'Python 3'), | ||
('rb', 'Ruby'), | ||
('rst', 'reStructuredText'), | ||
('smarty', 'Smarty'), | ||
('sql', 'SQL'), | ||
('text', 'Text only'), | ||
('xml', 'XML'), | ||
('yaml', 'YAML') | ||
) | ||
LEXER_DEFAULT = 'text' | ||
|
||
|
||
class NakedHtmlFormatter(HtmlFormatter): | ||
def wrap(self, source, outfile): | ||
return self._wrap_code(source) | ||
def _wrap_code(self, source): | ||
for i, t in source: | ||
yield i, t | ||
|
||
def pygmentize(code_string, lexer_name='text'): | ||
return highlight(code_string, get_lexer_by_name(lexer_name), NakedHtmlFormatter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import difflib | ||
|
||
from django.db import models | ||
from django.db.models import permalink | ||
import mptt | ||
|
||
from dpaste.highlight import LEXER_DEFAULT, pygmentize | ||
|
||
|
||
class Snippet(models.Model): | ||
""" | ||
Stores the snippets. | ||
Some Doctests: | ||
============== | ||
>>> from dpaste.models import Snippet | ||
>>> p = Snippet(title=u'foobar', author=u'[email protected]', lexer=u'text', content=u'foo') | ||
>>> p.title | ||
u'foobar' | ||
>>> p.content_highlighted | ||
'' | ||
# Highlighted content is processed on save | ||
>>> p.save() | ||
# Now there is highlighted content (well, it's not really highlighted due | ||
# the simple text lexer. | ||
>>> p.content_highlighted | ||
u'foo\\n' | ||
Relationships between snippets | ||
------------------------------ | ||
>>> q = Snippet(title=u'answer to foobar', lexer=u'text', content=u'bar') | ||
>>> q.parent = p | ||
>>> q.save() | ||
# There is now a relationship between ``q`` and ``p`` | ||
>>> q.get_root() | ||
<Snippet: Snippet #1> | ||
""" | ||
title = models.CharField(max_length=120, blank=True) | ||
author = models.CharField(max_length=30, blank=True) | ||
content = models.TextField() | ||
content_highlighted = models.TextField(blank=True) | ||
lexer = models.CharField(max_length=30, default=LEXER_DEFAULT) | ||
published = models.DateTimeField(auto_now_add=True) | ||
parent = models.ForeignKey('self', null=True, blank=True, related_name='children') | ||
|
||
class Meta: | ||
ordering = ('-published',) | ||
|
||
def get_linecount(self): | ||
return len(self.content.splitlines()) | ||
|
||
def save(self): | ||
self.content_highlighted = pygmentize(self.content, self.lexer) | ||
super(Snippet, self).save() | ||
|
||
@permalink | ||
def get_absolute_url(self): | ||
return ('snippet_details', (self.pk,)) | ||
|
||
def __unicode__(self): | ||
return 'Snippet #%s' % self.pk | ||
|
||
mptt.register(Snippet, order_insertion_by=['content']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
{% extends "base.html" %} | ||
{% load mptt_tags %} | ||
|
||
{% block extrahead %} | ||
{% if request.session.userprefs %} | ||
<style type="text/css" media="all"> | ||
.code{ | ||
font-family: {{ request.session.userprefs.font_family }} !important; | ||
font-size: {{ request.session.userprefs.font_size }}px !important; | ||
line-height: {{ request.session.userprefs.line_height }}px !important; | ||
} | ||
</style> | ||
{% endif %} | ||
{% endblock %} | ||
|
||
{% block title %}Snippet #{{ snippet.pk }}{% endblock %} | ||
{% block headline %} | ||
<h1> | ||
Snippet #{{ snippet.pk }} | ||
{% if snippet.parent_id %} | ||
(Copy of <a href="{{ snippet.parent.get_absolute_url }}">snippet #{{ snippet.parent.id }}</a>) | ||
{% endif %} | ||
<span class="date">{{ snippet.published|date:_("DATETIME_FORMAT") }} (UTC)</span> | ||
</h1> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div id="diff" style="display:none;">diff</div> | ||
|
||
<div class="accordion"> | ||
{# --------------------------- Snippet Details --------------------------- #} | ||
<h2> | ||
{% if snippet.title %}{{ snippet.title }}{% else %} Snippet #{{ snippet.id }}{% endif %} | ||
<span>{% if snippet.author %}by {{ snippet.author }}{% endif %}</span> | ||
</h2> | ||
<div class="container"> | ||
<div class="snippet"> | ||
<table> | ||
<tr> | ||
<th><pre class="code">{% for l in lines %}<a href="#l{{ forloop.counter }}" id="l{{ forloop.counter }}">{{ forloop.counter }}</a>{% endfor %}</pre></th> | ||
<td><pre class="code">{{ snippet.content_highlighted|safe }}</pre></td> | ||
</tr> | ||
</table> | ||
</div> | ||
</div> | ||
|
||
{# --------------------------- Snippet Answer --------------------------- #} | ||
<h2>Write an answer →</h2> | ||
<div class="container" style="display: none;"> | ||
<form method="post" action"." class="snippetform"> | ||
<ol> | ||
{{ snippet_form.as_ul }} | ||
<li class="submit"> | ||
<input type="submit" value="Paste it"/> | ||
</li> | ||
</ol> | ||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} | ||
|
||
|
||
|
||
{% block sidebar %} | ||
{# --------------------------- History Tree --------------------------- #} | ||
<h2>History</h2> | ||
|
||
<form method="get" id="diffform" action="{% url snippet_diff %}"> | ||
<div class="tree"> | ||
{% for tree_item,structure in tree|tree_info %} | ||
{% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %} | ||
<div> | ||
<span class="diff"> | ||
<input type="radio" name="a" value="{{ tree_item.id }}" {% ifequal tree_item.id snippet.parent_id %}checked="checked"{% endifequal %}/> | ||
<input type="radio" name="b" value="{{ tree_item.id }}" {% ifequal snippet tree_item %}checked="checked"{% endifequal %}/> | ||
</span> | ||
{% ifequal snippet tree_item %} | ||
<strong>{{ tree_item }}</strong> | ||
{% else %} | ||
<a href="{{ tree_item.get_absolute_url }}">{{ tree_item }}</a> | ||
{% endifequal %} | ||
</div> | ||
{% for level in structure.closed_levels %}</li></ul>{% endfor %} | ||
{% endfor %} | ||
<div class="submit"> | ||
<input type="submit" value="Compare"/> | ||
</div> | ||
</div> | ||
</form> | ||
|
||
<h2>Options</h2> | ||
<p><a href="{% url snippet_details_raw snippet.id %}">View raw</a></p> | ||
{% endblock %} | ||
|
||
{% block script_footer %} | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> | ||
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.min.js"></script> | ||
<script type="text/javascript"> | ||
jQuery(document).ready(function(){ | ||
$("div.accordion").accordion({ | ||
autoHeight: false, | ||
header: 'h2', | ||
animation: 'bounceslide', | ||
duration: 2000, | ||
}); | ||
|
||
$("form#diffform").submit(function() { | ||
$.get("{% url snippet_diff %}", { | ||
a: $("input[name=a]:checked").val(), | ||
b: $("input[name=b]:checked").val() | ||
}, function(data){ | ||
$('#diff').html(data).slideDown('fast'); | ||
}); | ||
return false; | ||
}); | ||
}); | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ snippet.content|safe }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h2> | ||
<span style="float:right;">(<a href="#" onclick="$('#diff').slideUp('fast'); return false;">Close</a>)</span> | ||
Diff between | ||
<a href="{{ fileA.get_absolute_url }}">Snippet #{{ fileA.id }}</a> | ||
and | ||
<a href="{{ fileB.get_absolute_url }}">Snippet #{{ fileB.id }}</a> | ||
</h2> | ||
|
||
<pre class="code">{{ difftext|safe }}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Snippet #{{ snippet.pk }}{% endblock %} | ||
{% block headline %} | ||
<h1>Your latest {{ snippets_max }} snippets</h1> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
{% if snippet_list %} | ||
{% for snippet in snippet_list %} | ||
<h2> | ||
<a href="{{ snippet.get_absolute_url }}">Snippet #{{ snippet.pk }}</a> | ||
~ {{ snippet.published|date:_("DATETIME_FORMAT") }} | ||
</h2> | ||
<p style="color: #555; margin: 8px 0 20px 0;">{{ snippet.content|truncatewords:40 }}</p> | ||
{% endfor %} | ||
{% else %} | ||
<p>No snippets available.</p> | ||
{% endif %} | ||
|
||
|
||
<div class="hint"> | ||
<p><strong>Remember:</strong> Your settings are stored in a session using a cookie.</p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}New snippet{% endblock %} | ||
{% block headline %}<h1>Paste a new snippet</h1>{% endblock %} | ||
|
||
{% block content %} | ||
<h2>New snippet</h2> | ||
<form method="post" action"." class="snippetform"> | ||
<ol> | ||
{{ snippet_form.as_ul }} | ||
<li class="submit"> | ||
<input type="submit" value="Paste it"/> | ||
</li> | ||
</ol> | ||
</form> | ||
{% endblock %} | ||
|
||
|
||
{% block sidebar %} | ||
<h2>What is this?</h2> | ||
|
||
<p>dpaste.de is a code pastebin inspired by <a href="http://dpaste.com/">dpaste.com</a>.</p> | ||
|
||
<h2>News</h2> | ||
|
||
<p>User settings are working correctly now.</p> | ||
|
||
{% endblock %} |
Oops, something went wrong.