Created
April 8, 2012 19:07
-
-
Save un33k/2339309 to your computer and use it in GitHub Desktop.
bookmark class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| from django.contrib.auth.models import User | |
| from django.utils.translation import ugettext_lazy as _ | |
| from django.contrib.contenttypes.models import ContentType | |
| from django.contrib.contenttypes import generic | |
| class BookmarkQuerySet(models.query.QuerySet): | |
| def get_or_create(self, **kwargs): | |
| new_kwargs = {} | |
| content_object = kwargs.get('content_object') | |
| new_kwargs['content_type'] = ContentType.objects.get_for_model(type(content_object)) | |
| new_kwargs['object_id'] = content_object.pk | |
| new_kwargs['user'] = kwargs.get('user') | |
| bookmark, created = super(BookmarkQuerySet, self).get_or_create(**new_kwargs) | |
| if created: | |
| try: | |
| bookmark.content_object.set_bookmark(increment=True) | |
| except: | |
| pass | |
| return bookmark | |
| class BookmarkManager(models.Manager): | |
| def get_query_set(self): | |
| return BookmarkQuerySet(self.model).order_by('-updated_at') | |
| def get_all_bookmarks_for_this_model_for_any_user(self, model): | |
| """ Returns all bookmarks of a given model for any user """ | |
| content_type = ContentType.objects.get_for_model(model) | |
| queryset = self.get_query_set().filter(content_type=content_type) | |
| return queryset | |
| def get_all_bookmarks_for_this_model_for_this_user(self, user, model): | |
| """ Returns all bookmarks of a model type for a given user """ | |
| queryset = self.get_all_bookmarks_for_this_model_for_any_user(model).filter(user=user) | |
| return queryset | |
| def get_all_bookmarks_for_this_object_type_for_any_user(self, obj): | |
| """ Returns all bookmarks of a given object by its type for any user""" | |
| content_type = ContentType.objects.get_for_model(type(obj)) | |
| queryset = self.get_query_set().filter(content_type=content_type) | |
| return queryset | |
| def get_all_bookmarks_for_this_object_type_for_this_user(self, obj): | |
| """ Returns all bookmarks of a given object by its type for a given user """ | |
| queryset = self.get_all_bookmarks_for_this_object_type_for_any_user(model).filter(user=user) | |
| return queryset | |
| def get_all_bookmarks_for_this_exact_object_for_any_user(self, obj): | |
| """ Returns all bookmarks of an specific object for any user """ | |
| content_type = ContentType.objects.get_for_model(type(obj)) | |
| queryset = self.get_query_set().filter(content_type=content_type, object_id=obj.pk) | |
| return queryset | |
| def get_the_bookmark_for_this_object_for_this_user(self, user, obj): | |
| """ Returns a single bookmark of a given object for a given user """ | |
| content_type = ContentType.objects.get_for_model(type(obj)) | |
| queryset = self.get_query_set().get(content_type=content_type, object_id=obj.pk, user=user) | |
| return queryset | |
| def get_all_bookmarks_for_this_user(self, user): | |
| """ Returns all bookmarks of all models & types for this user """ | |
| return self.get_query_set().filter(user=user) | |
| @classmethod | |
| def delete_bookmark(cls, user, obj): | |
| try: | |
| bm = Bookmark.objects.get_the_bookmark_for_this_object_for_this_user(user, obj) | |
| except: | |
| pass | |
| else: | |
| try: | |
| bm.content_object.set_bookmark(increment=False) | |
| except: | |
| pass | |
| bm.delete() | |
| return | |
| class Bookmark(models.Model): | |
| """ Bookmark objects for this user """ | |
| user = models.ForeignKey(User, related_name="%(class)s", null=False) | |
| content_type = models.ForeignKey(ContentType) | |
| object_id = models.PositiveIntegerField() | |
| content_object = generic.GenericForeignKey('content_type', 'object_id') | |
| created_at = models.DateTimeField(auto_now_add=True) | |
| updated_at = models.DateTimeField(auto_now=True) | |
| objects = BookmarkManager() | |
| class Meta: | |
| verbose_name = _('bookmark') | |
| verbose_name_plural = _('bookmarks') | |
| unique_together = (('user', 'content_type', 'object_id'),) | |
| def __unicode__(self): | |
| return u'%s-[%s]' % (self.user, self.content_object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment