Working ont ags

master
Nick Sergeant 2011-10-02 18:38:20 -04:00
parent 655d84ef70
commit b1c65bc6e4
5 changed files with 180 additions and 5 deletions

View File

@ -7,6 +7,7 @@
-e hg+http://bitbucket.org/dwaiter/django-bcrypt#egg=django-bcrypt
-e hg+ssh://hg@bitbucket.org/nicksergeant/django-registration#egg=django-registration
-e svn+http://django-grappelli.googlecode.com/svn/trunk#egg=django-grappelli
-e hg+https://bitbucket.org/birkenfeld/pygments-main#egg=Pygments
# Deployment
-e git://github.com/bitprophet/fabric.git#egg=fabric

View File

@ -82,6 +82,12 @@ COMPILER_FORMATS = {
GRAPPELLI_ADMIN_TITLE = '<a href="/">Snipt</a>'
# User absolute URLs
ABSOLUTE_URL_OVERRIDES = {
'auth.user': lambda u: "/%s/" % u.username,
}
# Local settings and debug
from local_settings import *

View File

@ -1,9 +1,11 @@
from tastypie.resources import ModelResource
from django.contrib.auth.models import User
from snipts.models import Comment, Snipt
from django.db.models import Count
from tastypie import fields
from taggit.models import Tag
class PublicUserResource(ModelResource):
class Meta:
queryset = User.objects.all()
@ -20,10 +22,16 @@ class PublicCommentSniptResource(ModelResource):
class PublicTagResource(ModelResource):
class Meta:
queryset = Tag.objects.all()
tags = Tag.objects.all()
annotated = tags.annotate(count=Count('taggit_taggeditem_items__id'))
queryset = annotated.order_by('-count')
resource_name = 'tag'
fields = ['name',]
include_absolute_url = True
def dehydrate(self, bundle):
bundle.data['absolute_url'] = '/public/tag/%s/' % bundle.obj.slug
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
return bundle
class PublicCommentResource(ModelResource):
user = fields.ForeignKey(PublicUserResource, 'user')
@ -36,13 +44,14 @@ class PublicCommentResource(ModelResource):
include_absolute_url = True
class PublicSniptResource(ModelResource):
comments = fields.ToManyField(PublicCommentResource, 'comment_set',related_name='comment')
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
related_name='comment')
class Meta:
queryset = Snipt.objects.filter(public=True).order_by('-created')
resource_name = 'snipt'
fields = ['user', 'title', 'slug', 'tags', 'lexer', 'code', 'stylized',
'created', 'modified',]
fields = ['user', 'title', 'slug', 'tags', 'lexer', 'code', 'created',
'modified',]
include_absolute_url = True
def dehydrate(self, bundle):
@ -52,11 +61,29 @@ class PublicSniptResource(ModelResource):
'absolute_url': bundle.obj.user.get_absolute_url(),
}
bundle.data['embed_url'] = bundle.obj.embed_url
bundle.data['stylized'] = bundle.obj.get_stylized()
bundle.data['tags'] = []
for tag in bundle.obj.tags.all():
bundle.data['tags'].append({
'name': tag.name,
'absolute_url': '/public/tag/%s/' % tag.slug,
'resource_uri': '/api/public/tag/%d/' % tag.id,
'snipts': '/api/public/snipt/?tag=%d' % tag.id,
})
return bundle
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(PublicSniptResource, self).build_filters(filters)
if 'tag' in filters:
tag = Tag.objects.get(pk=filters['tag'])
tagged_items = tag.taggit_taggeditem_items.all()
orm_filters['pk__in'] = [i.object_id for i in tagged_items]
return orm_filters

View File

@ -0,0 +1,115 @@
# 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):
# Adding model 'TaggedSnipt'
db.create_table('snipts_taggedsnipt', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('tag', self.gf('django.db.models.fields.related.ForeignKey')(related_name='snipts_taggedsnipt_items', to=orm['taggit.Tag'])),
('content_object', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['snipts.Snipt'])),
('count', self.gf('django.db.models.fields.IntegerField')()),
))
db.send_create_signal('snipts', ['TaggedSnipt'])
# Changing field 'Comment.modified'
db.alter_column('snipts_comment', 'modified', self.gf('django.db.models.fields.DateTimeField')())
# Changing field 'Snipt.modified'
db.alter_column('snipts_snipt', 'modified', self.gf('django.db.models.fields.DateTimeField')())
def backwards(self, orm):
# Deleting model 'TaggedSnipt'
db.delete_table('snipts_taggedsnipt')
# Changing field 'Comment.modified'
db.alter_column('snipts_comment', 'modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True))
# Changing field 'Snipt.modified'
db.alter_column('snipts_snipt', 'modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True))
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.comment': {
'Meta': {'object_name': 'Comment'},
'comment': ('django.db.models.fields.TextField', [], {}),
'created': ('django.db.models.fields.DateTimeField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {}),
'snipt': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['snipts.Snipt']"}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
},
'snipts.snipt': {
'Meta': {'object_name': 'Snipt'},
'code': ('django.db.models.fields.TextField', [], {}),
'created': ('django.db.models.fields.DateTimeField', [], {}),
'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'}),
'lexer': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'modified': ('django.db.models.fields.DateTimeField', [], {}),
'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', [], {}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
},
'snipts.taggedsnipt': {
'Meta': {'object_name': 'TaggedSnipt'},
'content_object': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['snipts.Snipt']"}),
'count': ('django.db.models.fields.IntegerField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'snipts_taggedsnipt_items'", 'to': "orm['taggit.Tag']"})
},
'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'})
}
}
complete_apps = ['snipts']

View File

@ -1,9 +1,18 @@
from django.template.defaultfilters import slugify
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.conf import settings
from django.db import models
from taggit.managers import TaggableManager
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
site = Site.objects.all()[0]
class Snipt(models.Model):
"""An individual Snipt."""
@ -37,6 +46,23 @@ class Snipt(models.Model):
def get_absolute_url(self):
return "/%s/%s/" % (self.user.username, self.slug)
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
@property
def embed_url(self):
return 'http%s://%s/embed/%s/' % ('s' if settings.USE_HTTPS else '',
site.domain,
self.key)
class Comment(models.Model):
"""A comment on a Snipt"""