2012-01-30 19:20:31 -08:00
|
|
|
from taggit.utils import edit_string_for_tags, parse_tags
|
2012-01-29 17:48:17 -08:00
|
|
|
from tastypie.authentication import ApiKeyAuthentication
|
2012-04-09 08:29:44 -07:00
|
|
|
from tastypie.authorization import Authorization
|
2012-05-27 11:51:21 -07:00
|
|
|
from django.template.defaultfilters import date
|
2011-12-22 20:36:06 -08:00
|
|
|
from tastypie.resources import ModelResource
|
2011-10-01 16:09:59 -07:00
|
|
|
from django.contrib.auth.models import User
|
2012-05-27 11:51:21 -07:00
|
|
|
from tastypie.validation import Validation
|
2012-01-29 17:48:17 -08:00
|
|
|
from tastypie.models import create_api_key
|
2012-04-12 19:04:37 -07:00
|
|
|
from snipts.models import Favorite, Snipt
|
2012-05-09 14:18:09 -07:00
|
|
|
from haystack.query import SearchQuerySet
|
2011-10-02 15:57:41 -07:00
|
|
|
from tastypie.cache import SimpleCache
|
2012-01-29 20:49:14 -08:00
|
|
|
from tastypie.fields import ListField
|
2011-10-02 13:07:47 -07:00
|
|
|
from taggit.models import Tag
|
2012-01-29 17:48:17 -08:00
|
|
|
from django.db import models
|
|
|
|
from tastypie import fields
|
|
|
|
|
2012-05-27 11:51:21 -07:00
|
|
|
import datetime, hashlib, time
|
|
|
|
|
|
|
|
import parsedatetime.parsedatetime as pdt
|
|
|
|
import parsedatetime.parsedatetime_consts as pdc
|
2012-04-13 10:13:19 -07:00
|
|
|
|
2012-01-29 17:48:17 -08:00
|
|
|
models.signals.post_save.connect(create_api_key, sender=User)
|
2011-10-01 10:23:05 -07:00
|
|
|
|
2011-10-02 15:38:20 -07:00
|
|
|
|
2012-04-12 19:04:37 -07:00
|
|
|
class FavoriteValidation(Validation):
|
|
|
|
def is_valid(self, bundle, request=None):
|
|
|
|
errors = {}
|
|
|
|
snipt = bundle.data['snipt']
|
|
|
|
|
|
|
|
if Favorite.objects.filter(user=request.user, snipt=snipt).count():
|
|
|
|
errors['duplicate'] = 'User has already favorited this snipt.'
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
2011-10-01 16:09:59 -07:00
|
|
|
class PublicUserResource(ModelResource):
|
|
|
|
class Meta:
|
2011-10-02 13:07:47 -07:00
|
|
|
queryset = User.objects.all()
|
2011-10-01 16:09:59 -07:00
|
|
|
resource_name = 'user'
|
2012-05-07 13:15:20 -07:00
|
|
|
fields = ['id', 'username',]
|
2011-10-02 13:16:58 -07:00
|
|
|
include_absolute_url = True
|
2011-10-03 17:42:00 -07:00
|
|
|
allowed_methods = ['get']
|
2012-08-27 09:01:37 -07:00
|
|
|
filtering = { 'username': 'exact', }
|
|
|
|
max_limit = 200
|
2011-10-02 15:57:41 -07:00
|
|
|
cache = SimpleCache()
|
2011-10-01 16:09:59 -07:00
|
|
|
|
2011-10-03 17:42:00 -07:00
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['snipts'] = '/api/public/snipt/?user=%d' % bundle.obj.id
|
|
|
|
return bundle
|
2011-10-01 16:09:59 -07:00
|
|
|
|
|
|
|
class PublicTagResource(ModelResource):
|
|
|
|
class Meta:
|
2011-10-03 17:42:00 -07:00
|
|
|
queryset = Tag.objects.filter(snipt__public=True)
|
2012-01-29 17:48:17 -08:00
|
|
|
queryset = queryset.annotate(count=models.Count('taggit_taggeditem_items__id'))
|
2012-01-16 15:47:09 -08:00
|
|
|
queryset = queryset.order_by('-count', 'name')
|
2011-10-01 16:09:59 -07:00
|
|
|
resource_name = 'tag'
|
2012-05-04 16:11:35 -07:00
|
|
|
fields = ['id', 'name',]
|
2011-10-03 17:42:00 -07:00
|
|
|
allowed_methods = ['get']
|
2012-08-27 09:01:37 -07:00
|
|
|
max_limit = 200
|
2011-10-02 15:57:41 -07:00
|
|
|
cache = SimpleCache()
|
2011-10-02 15:38:20 -07:00
|
|
|
|
2012-07-30 09:51:02 -07:00
|
|
|
def build_filters(self, filters=None):
|
|
|
|
if filters is None:
|
|
|
|
filters = {}
|
|
|
|
|
|
|
|
orm_filters = super(PublicTagResource, self).build_filters(filters)
|
|
|
|
|
|
|
|
if 'q' in filters:
|
|
|
|
orm_filters['slug'] = filters['q']
|
|
|
|
|
|
|
|
return orm_filters
|
|
|
|
|
2011-10-02 15:38:20 -07:00
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['absolute_url'] = '/public/tag/%s/' % bundle.obj.slug
|
2011-10-10 21:13:18 -07:00
|
|
|
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
|
2011-10-03 17:42:00 -07:00
|
|
|
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(
|
|
|
|
snipt__public=True).count()
|
2011-10-02 15:38:20 -07:00
|
|
|
return bundle
|
2011-10-01 16:09:59 -07:00
|
|
|
|
|
|
|
class PublicSniptResource(ModelResource):
|
2011-10-03 17:42:00 -07:00
|
|
|
user = fields.ForeignKey(PublicUserResource, 'user', full=True)
|
|
|
|
tags = fields.ToManyField(PublicTagResource, 'tags', related_name='tag', full=True)
|
2011-10-01 10:23:05 -07:00
|
|
|
|
|
|
|
class Meta:
|
2011-10-02 13:07:47 -07:00
|
|
|
queryset = Snipt.objects.filter(public=True).order_by('-created')
|
2011-10-01 10:23:05 -07:00
|
|
|
resource_name = 'snipt'
|
2012-04-08 17:16:02 -07:00
|
|
|
fields = ['id', 'title', 'slug', 'lexer', 'code', 'line_count',
|
2012-01-29 20:49:14 -08:00
|
|
|
'stylized', 'created', 'modified',]
|
2011-10-02 13:16:58 -07:00
|
|
|
include_absolute_url = True
|
2011-10-03 17:42:00 -07:00
|
|
|
allowed_methods = ['get']
|
|
|
|
filtering = { 'user': 'exact', }
|
2012-02-20 19:53:34 -08:00
|
|
|
ordering = ['created', 'modified',]
|
2012-02-20 20:04:49 -08:00
|
|
|
max_limit = 200
|
2011-10-02 15:57:41 -07:00
|
|
|
cache = SimpleCache()
|
2011-10-01 16:09:59 -07:00
|
|
|
|
|
|
|
def dehydrate(self, bundle):
|
2011-10-03 17:42:00 -07:00
|
|
|
bundle.data['embed_url'] = bundle.obj.get_embed_url()
|
2012-06-23 18:47:52 -07:00
|
|
|
bundle.data['full_absolute_url'] = bundle.obj.get_full_absolute_url()
|
2011-10-01 16:09:59 -07:00
|
|
|
return bundle
|
2011-10-02 15:38:20 -07:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2012-05-09 14:18:09 -07:00
|
|
|
if 'q' in filters:
|
|
|
|
sqs = SearchQuerySet().auto_query(filters['q'])
|
|
|
|
orm_filters['pk__in'] = [i.pk for i in sqs]
|
|
|
|
|
2011-10-02 15:38:20 -07:00
|
|
|
return orm_filters
|
2011-12-22 20:36:06 -08:00
|
|
|
|
|
|
|
|
2011-12-25 13:00:41 -08:00
|
|
|
class PrivateUserResource(ModelResource):
|
|
|
|
class Meta:
|
|
|
|
queryset = User.objects.all()
|
|
|
|
resource_name = 'user'
|
2012-05-07 13:15:20 -07:00
|
|
|
fields = ['id', 'username', 'email',]
|
2011-12-25 13:00:41 -08:00
|
|
|
include_absolute_url = True
|
|
|
|
allowed_methods = ['get']
|
|
|
|
list_allowed_methods = []
|
2012-01-29 17:48:17 -08:00
|
|
|
authentication = ApiKeyAuthentication()
|
2012-04-09 08:29:44 -07:00
|
|
|
authorization = Authorization()
|
2012-04-05 14:33:47 -07:00
|
|
|
always_return_data = True
|
2012-08-27 09:01:37 -07:00
|
|
|
max_limit = 200
|
2011-12-25 13:00:41 -08:00
|
|
|
cache = SimpleCache()
|
|
|
|
|
|
|
|
def apply_authorization_limits(self, request, object_list):
|
|
|
|
return object_list.filter(username=request.user.username)
|
|
|
|
|
2012-04-13 10:13:19 -07:00
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['email_md5'] = hashlib.md5(bundle.obj.email.lower()).hexdigest()
|
2012-07-31 12:35:12 -07:00
|
|
|
bundle.data['profile'] = {
|
|
|
|
'is_pro': bundle.obj.profile.is_pro
|
|
|
|
}
|
2012-04-13 10:13:19 -07:00
|
|
|
return bundle
|
|
|
|
|
2011-12-25 13:00:41 -08:00
|
|
|
class PrivateTagResource(ModelResource):
|
|
|
|
class Meta:
|
|
|
|
queryset = Tag.objects.all()
|
|
|
|
resource_name = 'tag'
|
2012-05-04 16:11:35 -07:00
|
|
|
fields = ['id', 'name',]
|
2011-12-25 13:00:41 -08:00
|
|
|
allowed_methods = ['get']
|
2012-01-29 17:48:17 -08:00
|
|
|
authentication = ApiKeyAuthentication()
|
2012-04-09 08:29:44 -07:00
|
|
|
authorization = Authorization()
|
2012-04-05 14:33:47 -07:00
|
|
|
always_return_data = True
|
2012-08-27 09:01:37 -07:00
|
|
|
max_limit = 200
|
2011-12-25 13:00:41 -08:00
|
|
|
cache = SimpleCache()
|
|
|
|
|
2012-07-30 09:51:02 -07:00
|
|
|
def build_filters(self, filters=None):
|
|
|
|
if filters is None:
|
|
|
|
filters = {}
|
|
|
|
|
2012-07-30 11:40:55 -07:00
|
|
|
orm_filters = super(PrivateTagResource, self).build_filters(filters)
|
2012-07-30 09:51:02 -07:00
|
|
|
|
|
|
|
if 'q' in filters:
|
|
|
|
orm_filters['slug'] = filters['q']
|
|
|
|
|
|
|
|
return orm_filters
|
2011-12-25 13:00:41 -08:00
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['absolute_url'] = '/%s/tag/%s/' % (bundle.request.user.username,
|
|
|
|
bundle.obj.slug)
|
|
|
|
bundle.data['snipts'] = '/api/private/snipt/?tag=%d' % bundle.obj.id
|
|
|
|
|
|
|
|
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(
|
2011-12-28 18:50:28 -08:00
|
|
|
snipt__user=bundle.request.user
|
|
|
|
).count()
|
|
|
|
|
2011-12-25 13:00:41 -08:00
|
|
|
return bundle
|
|
|
|
|
|
|
|
def apply_authorization_limits(self, request, object_list):
|
2011-12-28 18:50:28 -08:00
|
|
|
object_list = object_list.filter(snipt__user=request.user)
|
2012-01-29 17:48:17 -08:00
|
|
|
object_list = object_list.annotate(count=models.Count('taggit_taggeditem_items__id'))
|
2012-01-16 15:47:09 -08:00
|
|
|
object_list = object_list.order_by('-count', 'name')
|
2011-12-28 18:50:28 -08:00
|
|
|
return object_list
|
2011-12-25 13:00:41 -08:00
|
|
|
|
2011-12-22 20:36:06 -08:00
|
|
|
class PrivateSniptResource(ModelResource):
|
2011-12-25 13:00:41 -08:00
|
|
|
user = fields.ForeignKey(PrivateUserResource, 'user', full=True)
|
2012-01-29 20:49:14 -08:00
|
|
|
tags = fields.ToManyField(PrivateTagResource, 'tags', related_name='tag', full=True)
|
|
|
|
tags_list = ListField()
|
2011-12-25 13:00:41 -08:00
|
|
|
|
2011-12-22 20:36:06 -08:00
|
|
|
class Meta:
|
2011-12-24 12:08:49 -08:00
|
|
|
queryset = Snipt.objects.all().order_by('-created')
|
2011-12-22 20:36:06 -08:00
|
|
|
resource_name = 'snipt'
|
2012-05-24 19:20:06 -07:00
|
|
|
fields = ['id', 'title', 'slug', 'lexer', 'code', 'line_count', 'stylized',
|
2012-05-27 11:51:21 -07:00
|
|
|
'key', 'public', 'blog_post', 'created', 'modified', 'publish_date',]
|
2012-01-30 19:20:31 -08:00
|
|
|
validation = Validation()
|
2011-12-22 20:36:06 -08:00
|
|
|
include_absolute_url = True
|
2012-01-30 19:20:31 -08:00
|
|
|
detail_allowed_methods = ['get', 'patch', 'put', 'delete']
|
2012-01-29 17:48:17 -08:00
|
|
|
list_allowed_methods = ['get', 'post']
|
|
|
|
authentication = ApiKeyAuthentication()
|
2012-04-09 08:29:44 -07:00
|
|
|
authorization = Authorization()
|
2012-02-20 19:53:34 -08:00
|
|
|
ordering = ['created', 'modified',]
|
2012-04-05 14:33:47 -07:00
|
|
|
always_return_data = True
|
2012-08-27 09:01:37 -07:00
|
|
|
max_limit = 200
|
2012-01-29 19:04:52 -08:00
|
|
|
cache = SimpleCache()
|
2011-12-22 20:36:06 -08:00
|
|
|
|
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['embed_url'] = bundle.obj.get_embed_url()
|
2012-01-30 19:20:31 -08:00
|
|
|
bundle.data['tags_list'] = edit_string_for_tags(bundle.obj.tags.all())
|
2012-06-23 18:47:52 -07:00
|
|
|
bundle.data['full_absolute_url'] = bundle.obj.get_full_absolute_url()
|
2012-05-27 11:51:21 -07:00
|
|
|
|
2012-09-17 10:26:54 -07:00
|
|
|
if bundle.data['user'].data['profile']['is_pro']:
|
|
|
|
bundle.data['views'] = bundle.obj.views
|
|
|
|
bundle.data['favs'] = bundle.obj.favs()
|
|
|
|
|
2012-05-27 11:51:21 -07:00
|
|
|
if bundle.data['publish_date']:
|
|
|
|
bundle.data['publish_date'] = date(bundle.data['publish_date'], 'M d, Y \\a\\t h:i A')
|
|
|
|
|
2011-12-22 20:36:06 -08:00
|
|
|
return bundle
|
|
|
|
|
2011-12-24 12:08:49 -08:00
|
|
|
def obj_create(self, bundle, request=None, **kwargs):
|
2012-01-30 19:20:31 -08:00
|
|
|
bundle.data['tags_list'] = bundle.data.get('tags')
|
2012-01-29 20:49:14 -08:00
|
|
|
bundle.data['tags'] = ''
|
2012-05-24 19:20:06 -07:00
|
|
|
|
2012-05-29 07:49:42 -07:00
|
|
|
if 'blog_post' in bundle.data:
|
|
|
|
bundle = self._clean_publish_date(bundle)
|
2012-05-27 11:51:21 -07:00
|
|
|
|
2011-12-24 12:08:49 -08:00
|
|
|
return super(PrivateSniptResource, self).obj_create(bundle, request,
|
2012-04-09 08:29:44 -07:00
|
|
|
user=request.user, **kwargs)
|
2011-12-22 20:36:06 -08:00
|
|
|
|
2012-01-30 19:20:31 -08:00
|
|
|
def obj_update(self, bundle, request=None, **kwargs):
|
|
|
|
bundle.data['user'] = request.user
|
2012-07-08 20:55:18 -07:00
|
|
|
|
2012-07-04 08:03:09 -07:00
|
|
|
if type(bundle.data['tags']) in (str, unicode):
|
2012-01-30 19:20:31 -08:00
|
|
|
bundle.data['tags_list'] = bundle.data['tags']
|
|
|
|
else:
|
|
|
|
bundle.data['tags_list'] = ''
|
|
|
|
bundle.data['tags'] = ''
|
2012-05-24 19:20:06 -07:00
|
|
|
|
2012-05-29 07:49:42 -07:00
|
|
|
if 'blog_post' in bundle.data:
|
|
|
|
bundle = self._clean_publish_date(bundle)
|
2012-05-27 11:51:21 -07:00
|
|
|
|
2012-04-09 08:29:44 -07:00
|
|
|
return super(PrivateSniptResource, self).obj_update(bundle, request,
|
|
|
|
user=request.user, **kwargs)
|
2012-01-30 19:20:31 -08:00
|
|
|
|
2012-05-27 11:51:21 -07:00
|
|
|
def _clean_publish_date(self, bundle):
|
2012-07-05 18:22:45 -07:00
|
|
|
if bundle.data['blog_post'] and 'publish_date' not in bundle.data:
|
|
|
|
bundle.data['publish_date'] = datetime.datetime.now()
|
|
|
|
elif bundle.data['publish_date'] == '':
|
2012-05-27 11:51:21 -07:00
|
|
|
bundle.data['publish_date'] = datetime.datetime.now()
|
|
|
|
elif bundle.data['blog_post']:
|
|
|
|
c = pdc.Constants()
|
|
|
|
p = pdt.Calendar(c)
|
|
|
|
publish_date, result = p.parse(bundle.data['publish_date'])
|
|
|
|
|
|
|
|
if result != 0:
|
|
|
|
publish_date = time.strftime("%Y-%m-%d %H:%M:%S", publish_date)
|
|
|
|
else:
|
|
|
|
publish_date = datetime.datetime.now()
|
|
|
|
|
|
|
|
bundle.data['publish_date'] = publish_date
|
|
|
|
elif not bundle.data['blog_post']:
|
|
|
|
bundle.data['publish_date'] = None
|
|
|
|
|
|
|
|
return bundle
|
|
|
|
|
2011-12-25 13:00:41 -08:00
|
|
|
def build_filters(self, filters=None):
|
|
|
|
if filters is None:
|
|
|
|
filters = {}
|
|
|
|
|
|
|
|
orm_filters = super(PrivateSniptResource, 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]
|
|
|
|
|
2012-05-09 14:18:09 -07:00
|
|
|
if 'q' in filters:
|
|
|
|
sqs = SearchQuerySet().auto_query(filters['q'])
|
|
|
|
orm_filters['pk__in'] = [i.pk for i in sqs]
|
|
|
|
|
2011-12-25 13:00:41 -08:00
|
|
|
return orm_filters
|
|
|
|
|
2011-12-24 12:08:49 -08:00
|
|
|
def apply_authorization_limits(self, request, object_list):
|
|
|
|
return object_list.filter(user=request.user)
|
2012-01-29 20:49:14 -08:00
|
|
|
|
|
|
|
def save_m2m(self, bundle):
|
|
|
|
tags = bundle.data.get('tags_list', [])
|
2012-01-30 19:20:31 -08:00
|
|
|
if tags != '':
|
|
|
|
bundle.obj.tags.set(*parse_tags(tags))
|
2012-07-08 20:55:18 -07:00
|
|
|
else:
|
|
|
|
bundle.obj.tags.set()
|
2012-07-04 08:03:09 -07:00
|
|
|
|
2012-04-12 19:04:37 -07:00
|
|
|
class PrivateFavoriteResource(ModelResource):
|
|
|
|
user = fields.ForeignKey(PrivateUserResource, 'user', full=True)
|
|
|
|
snipt = fields.ForeignKey(PrivateSniptResource, 'snipt', full=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
queryset = Favorite.objects.all().order_by('-created')
|
|
|
|
resource_name = 'favorite'
|
|
|
|
fields = ['id',]
|
|
|
|
validation = FavoriteValidation()
|
|
|
|
detail_allowed_methods = ['get', 'post', 'delete']
|
|
|
|
list_allowed_methods = ['get', 'post',]
|
|
|
|
authentication = ApiKeyAuthentication()
|
|
|
|
authorization = Authorization()
|
|
|
|
ordering = ['created',]
|
|
|
|
always_return_data = True
|
2012-08-27 09:01:37 -07:00
|
|
|
max_limit = 200
|
2012-04-12 19:04:37 -07:00
|
|
|
cache = SimpleCache()
|
|
|
|
|
2012-11-23 05:53:42 -08:00
|
|
|
def dehydrate(self, bundle):
|
|
|
|
bundle.data['snipt'] = '/api/public/snipt/{}/'.format(
|
|
|
|
bundle.obj.snipt.pk)
|
|
|
|
return bundle
|
|
|
|
|
2012-04-12 19:04:37 -07:00
|
|
|
def obj_create(self, bundle, request=None, **kwargs):
|
|
|
|
bundle.data['user'] = request.user
|
|
|
|
bundle.data['snipt'] = Snipt.objects.get(pk=bundle.data['snipt'])
|
|
|
|
return super(PrivateFavoriteResource, self).obj_create(bundle, request,
|
|
|
|
user=request.user, **kwargs)
|
|
|
|
|
|
|
|
def apply_authorization_limits(self, request, object_list):
|
|
|
|
return object_list.filter(user=request.user)
|