From b1c65bc6e4549bbbff89a4d18b8be840a5b4ac29 Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Sun, 2 Oct 2011 18:38:20 -0400 Subject: [PATCH] Working ont ags --- requirements.txt | 1 + settings.py | 6 + snipts/api.py | 37 +++++- ...d_comment_modified__chg_field_snipt_mod.py | 115 ++++++++++++++++++ snipts/models.py | 26 ++++ 5 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 snipts/migrations/0014_auto__add_taggedsnipt__chg_field_comment_modified__chg_field_snipt_mod.py diff --git a/requirements.txt b/requirements.txt index ee2e88a..23c0b87 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/settings.py b/settings.py index a89b756..bc65b6c 100644 --- a/settings.py +++ b/settings.py @@ -82,6 +82,12 @@ COMPILER_FORMATS = { GRAPPELLI_ADMIN_TITLE = 'Snipt' +# User absolute URLs + +ABSOLUTE_URL_OVERRIDES = { + 'auth.user': lambda u: "/%s/" % u.username, +} + # Local settings and debug from local_settings import * diff --git a/snipts/api.py b/snipts/api.py index 15704ff..c2dd944 100644 --- a/snipts/api.py +++ b/snipts/api.py @@ -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 diff --git a/snipts/migrations/0014_auto__add_taggedsnipt__chg_field_comment_modified__chg_field_snipt_mod.py b/snipts/migrations/0014_auto__add_taggedsnipt__chg_field_comment_modified__chg_field_snipt_mod.py new file mode 100644 index 0000000..23ce761 --- /dev/null +++ b/snipts/migrations/0014_auto__add_taggedsnipt__chg_field_comment_modified__chg_field_snipt_mod.py @@ -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'] diff --git a/snipts/models.py b/snipts/models.py index 1e5c90c..3134e71 100644 --- a/snipts/models.py +++ b/snipts/models.py @@ -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"""