Nulling things

master
Nick Sergeant 2012-01-29 23:49:14 -05:00
parent c6aea74f78
commit c5cfdd584a
6 changed files with 142 additions and 48 deletions

View File

@ -1,27 +1,12 @@
from django.contrib import admin
from snipts.models import Comment, Snipt
class CommentInline(admin.TabularInline):
readonly_fields = ('user', 'snipt',)
model = Comment
extra = 0
allow_add = False
from snipts.models import Snipt
class SniptAdmin(admin.ModelAdmin):
readonly_fields = ('user',)
list_display = ('title', 'slug', 'user', 'lexer', 'public', 'created', 'modified',)
search_fields = ('title', 'user__username', 'lexer', 'id',)
ordering = ('created',)
ordering = ('-created',)
prepopulated_fields = {'slug': ('title',)}
inlines = [CommentInline]
admin.site.register(Snipt, SniptAdmin)
class CommentAdmin(admin.ModelAdmin):
readonly_fields = ('user', 'snipt',)
list_display = ('comment', 'user', 'snipt', 'created', 'modified',)
search_fields = ('comment', 'user__username',)
ordering = ('created',)
admin.site.register(Comment, CommentAdmin)

View File

@ -1,9 +1,13 @@
from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import DjangoAuthorization
from tastypie.validation import FormValidation
from tastypie.resources import ModelResource
from django.contrib.auth.models import User
from tastypie.models import create_api_key
from tastypie.cache import SimpleCache
from tastypie.fields import ListField
from taggit.utils import parse_tags
from snipts.forms import SniptForm
from snipts.models import Snipt
from taggit.models import Tag
from django.db import models
@ -51,7 +55,7 @@ class PublicSniptResource(ModelResource):
queryset = Snipt.objects.filter(public=True).order_by('-created')
resource_name = 'snipt'
fields = ['title', 'description', 'slug', 'lexer', 'code', 'line_count',
'created', 'modified',]
'stylized', 'created', 'modified',]
include_absolute_url = True
allowed_methods = ['get']
filtering = { 'user': 'exact', }
@ -59,7 +63,6 @@ class PublicSniptResource(ModelResource):
def dehydrate(self, bundle):
bundle.data['embed_url'] = bundle.obj.get_embed_url()
bundle.data['stylized'] = bundle.obj.get_stylized()
return bundle
def build_filters(self, filters=None):
@ -120,12 +123,15 @@ class PrivateTagResource(ModelResource):
class PrivateSniptResource(ModelResource):
user = fields.ForeignKey(PrivateUserResource, 'user', full=True)
tags = fields.ToManyField(PrivateTagResource, 'tags', related_name='tag', full=True)
tags_list = ListField()
class Meta:
queryset = Snipt.objects.all().order_by('-created')
resource_name = 'snipt'
fields = ['title', 'description', 'slug', 'lexer', 'code', 'line_count',
'key', 'public', 'created', 'modified',]
'stylized', 'key', 'public', 'created', 'modified',]
validation = FormValidation(form_class=SniptForm)
include_absolute_url = True
detail_allowed_methods = ['get', 'put', 'delete']
list_allowed_methods = ['get', 'post']
@ -135,10 +141,11 @@ class PrivateSniptResource(ModelResource):
def dehydrate(self, bundle):
bundle.data['embed_url'] = bundle.obj.get_embed_url()
bundle.data['stylized'] = bundle.obj.get_stylized()
return bundle
def obj_create(self, bundle, request=None, **kwargs):
bundle.data['tags_list'] = bundle.data['tags']
bundle.data['tags'] = ''
return super(PrivateSniptResource, self).obj_create(bundle, request,
user=request.user)
@ -157,3 +164,7 @@ class PrivateSniptResource(ModelResource):
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
def save_m2m(self, bundle):
tags = bundle.data.get('tags_list', [])
bundle.obj.tags.set(*parse_tags(tags))

6
snipt/snipts/forms.py Normal file
View File

@ -0,0 +1,6 @@
from django.forms import ModelForm
from snipts.models import Snipt
class SniptForm(ModelForm):
class Meta:
model = Snipt

View File

@ -0,0 +1,101 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Changing field 'Snipt.stylized'
db.alter_column('snipts_snipt', 'stylized', self.gf('django.db.models.fields.TextField')(null=True))
# Changing field 'Snipt.user'
db.alter_column('snipts_snipt', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True))
# Changing field 'Snipt.key'
db.alter_column('snipts_snipt', 'key', self.gf('django.db.models.fields.CharField')(max_length=100, null=True))
def backwards(self, orm):
# Changing field 'Snipt.stylized'
db.alter_column('snipts_snipt', 'stylized', self.gf('django.db.models.fields.TextField')(default=''))
# Changing field 'Snipt.user'
db.alter_column('snipts_snipt', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(default=3, to=orm['auth.User']))
# Changing field 'Snipt.key'
db.alter_column('snipts_snipt', 'key', self.gf('django.db.models.fields.CharField')(default='', max_length=100))
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'snipts.snipt': {
'Meta': {'object_name': 'Snipt'},
'code': ('django.db.models.fields.TextField', [], {}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'key': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
'lexer': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'line_count': ('django.db.models.fields.IntegerField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'public': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}),
'stylized': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
},
'taggit.tag': {
'Meta': {'object_name': 'Tag'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100', 'db_index': 'True'})
},
'taggit.taggeditem': {
'Meta': {'object_name': 'TaggedItem'},
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'taggit_taggeditem_tagged_items'", 'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'object_id': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}),
'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'taggit_taggeditem_items'", 'to': "orm['taggit.Tag']"})
}
}
complete_apps = ['snipts']

View File

@ -10,13 +10,15 @@ from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
import md5
site = Site.objects.all()[0]
class Snipt(models.Model):
"""An individual Snipt."""
user = models.ForeignKey(User)
user = models.ForeignKey(User, blank=True, null=True)
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
@ -25,19 +27,28 @@ class Snipt(models.Model):
lexer = models.CharField(max_length=50)
code = models.TextField()
stylized = models.TextField()
stylized = models.TextField(blank=True, null=True)
line_count = models.IntegerField(blank=True, null=True, default=None)
key = models.CharField(max_length=100)
key = models.CharField(max_length=100, blank=True, null=True)
public = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
def save(self, *args, **kwargs):
if not self.key:
self.key = md5.new(self.slug).hexdigest()
if not self.slug:
self.slug = slugify(self.title)[:50]
self.stylized = highlight(self.code,
get_lexer_by_name(self.lexer, encoding='UTF-8'),
HtmlFormatter())
self.line_count = len(self.code.split('\n'))
return super(Snipt, self).save(*args, **kwargs)
def __unicode__(self):
@ -56,26 +67,6 @@ class Snipt(models.Model):
root = 'http://snipt.net'
return "%s/%s/%s/" % (root, self.user.username, self.slug)
#TODO This needs to be deprecated - render stylized version on save
def get_stylized(self):
if self.stylized == '':
self.stylized = highlight(self.code,
get_lexer_by_name(self.lexer, encoding='UTF-8'),
HtmlFormatter())
self.save()
return self.stylized
else:
return self.stylized
#TODO This needs to be deprecated - render line count on save
def get_line_count(self):
if not self.line_count:
self.line_count = len(self.code.split('\n'))
self.save()
return self.line_count
else:
return self.line_count
def get_embed_url(self):
return 'http%s://%s/embed/%s/' % ('s' if settings.USE_HTTPS else '',
site.domain,

View File

@ -1,6 +1,6 @@
{% load humanize %}
<article class="snipt {% if not snipt.public %}private-snipt{% endif %} {% if snipt.get_line_count > 8 and not detail %}expandable{% endif %}">
<article class="snipt {% if not snipt.public %}private-snipt{% endif %} {% if snipt.line_count > 8 and not detail %}expandable{% endif %}">
<div class="number">#{{ snipt.id }}</div>
<div class="group">
<div class="container">
@ -9,12 +9,12 @@
<h1><a href="{{ snipt.get_absolute_url }}">{{ snipt.title }}</a></h1>
</header>
<section class="code {% if request.GET.style %}{{ request.GET.style }}{% else %}autumn{% endif %}">
{{ snipt.get_stylized|safe }}
{% if snipt.get_line_count > 8 and not detail %}
{{ snipt.stylized|safe }}
{% if snipt.line_count > 8 and not detail %}
<a href="#" class="expand">
<span class="expand">Expand</span>
<span class="collapse">Collapse</span>
<span class="lines">({{ snipt.get_line_count }} lines)</span>
<span class="lines">({{ snipt.line_count }} lines)</span>
</a>
{% endif %}
<div class="raw">{{ snipt.code }}</div>