Support for custom URLs

master
Nick Sergeant 2012-05-03 22:19:05 -04:00
parent 4f2f783870
commit 82d5cb3c4d
6 changed files with 142 additions and 26 deletions

View File

@ -0,0 +1,122 @@
# 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 'Favorite.modified'
db.alter_column('snipts_favorite', 'modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True))
# Changing field 'Favorite.created'
db.alter_column('snipts_favorite', 'created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True))
# Adding field 'Snipt.custom_slug'
db.add_column('snipts_snipt', 'custom_slug', self.gf('django.db.models.fields.SlugField')(db_index=True, default='', max_length=255, blank=True), keep_default=False)
# Changing field 'Snipt.created'
db.alter_column('snipts_snipt', 'created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True))
# Changing field 'Snipt.modified'
db.alter_column('snipts_snipt', 'modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True))
def backwards(self, orm):
# Changing field 'Favorite.modified'
db.alter_column('snipts_favorite', 'modified', self.gf('django.db.models.fields.DateTimeField')())
# Changing field 'Favorite.created'
db.alter_column('snipts_favorite', 'created', self.gf('django.db.models.fields.DateTimeField')())
# Deleting field 'Snipt.custom_slug'
db.delete_column('snipts_snipt', 'custom_slug')
# Changing field 'Snipt.created'
db.alter_column('snipts_snipt', 'created', self.gf('django.db.models.fields.DateTimeField')())
# Changing field 'Snipt.modified'
db.alter_column('snipts_snipt', 'modified', self.gf('django.db.models.fields.DateTimeField')())
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.favorite': {
'Meta': {'object_name': 'Favorite'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'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', [], {'auto_now_add': 'True', 'blank': 'True'}),
'custom_slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}),
'embedded': ('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

@ -25,6 +25,7 @@ class Snipt(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, blank=True)
custom_slug = models.SlugField(max_length=255, blank=True)
tags = TaggableManager()
lexer = models.CharField(max_length=50)
@ -92,6 +93,10 @@ class Snipt(models.Model):
return self.title
def get_absolute_url(self):
if self.custom_slug:
return '/{}/'.format(self.custom_slug)
if self.public:
return '/{}/{}/'.format(self.user.username, self.slug)
else:

View File

@ -4,11 +4,11 @@ from snipts import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^public/$', views.list_public, name='list-public'),
url(r'^public/tag/(?P<tag_slug>[^/]+)/$', views.list_public, name='list-public-tag'),
url(r'^embed/(?P<snipt_key>[^/]+)/$', views.embed, name='embed'),
url(r'^(?P<username>[^/]+)/$', views.list_user, name='list-user'),
url(r'^(?P<username>[^/]+)/tag/(?P<tag_slug>[^/]+)/$', views.list_user, name='list-user-tag'),
url(r'^(?P<username>[^/]+)/(?P<snipt_slug>[^/]+)/$', views.detail, name='detail'),
url(r'^$', views.home, name='home'),
url(r'^public/$', views.list_public, name='list-public'),
url(r'^public/tag/(?P<tag_slug>[^/]+)/$', views.list_public, name='list-public-tag'),
url(r'^embed/(?P<snipt_key>[^/]+)/$', views.embed, name='embed'),
url(r'^(?P<username_or_custom_slug>[^/]+)/$', views.list_user, name='list-user'),
url(r'^(?P<username>[^/]+)/tag/(?P<tag_slug>[^/]+)/$', views.list_user_tag, name='list-user-tag'),
url(r'^(?P<username>[^/]+)/(?P<snipt_slug>[^/]+)/$', views.detail, name='detail'),
)

View File

@ -4,6 +4,7 @@ from django.http import Http404, HttpResponseRedirect
from django.contrib.auth.models import User
from django.template import RequestContext
from annoying.decorators import render_to
from annoying.functions import get_object_or_None
from snipts.models import Favorite, Snipt
from django.db.models import Count
from django.conf import settings
@ -53,9 +54,14 @@ def list_public(request, tag_slug=None):
return context
@render_to('snipts/list-user.html')
def list_user(request, username, tag_slug=None):
def list_user(request, username_or_custom_slug, tag_slug=None):
user = get_object_or_None(User, username=username_or_custom_slug)
if user is None:
snipt = get_object_or_404(Snipt, custom_slug=username_or_custom_slug)
return detail(request, snipt.user, snipt.slug)
user = get_object_or_404(User, username=username)
tags = Tag.objects
snipts = Snipt.objects
@ -150,7 +156,6 @@ def rss(request, context):
mimetype="application/rss+xml"
)
def search(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None):
query = ''
results = EmptySearchQuerySet()

View File

@ -1,14 +0,0 @@
{% extends "base.html" %}
{% block page-title %}API - {{ block.super }}{% endblock %}
{% block body-class %}{{ block.super }} api static{% endblock %}
{% block sub-header %}{% endblock %}
{% block content %}
<div class="static-box">
<div class="alert alert-info alert-alone">
This is where the API documentation will eventually live.
</div>
</div>
{% endblock %}

View File

@ -32,8 +32,6 @@ urlpatterns = patterns('',
url(r'^404/$', direct_to_template, {'template': '404.html'}),
url(r'^500/$', direct_to_template, {'template': '500.html'}),
url(r'^api/$', direct_to_template, {'template': 'api.html'}),
url(r'^api/', include(public_api.urls)),
url(r'^api/', include(private_api.urls)),