def ridero_link(self):
return 'https://ridero.eu/%s/books/wl_%s/' % (get_language(), self.slug.replace('-', '_'))
+ def like(self, user):
+ from social.utils import likes, get_set, set_sets
+ if not likes(user, self):
+ tag = get_set(user, '')
+ set_sets(user, self, [tag])
+
+ def unlike(self, user):
+ from social.utils import likes, set_sets
+ if likes(user, self):
+ set_sets(user, self, [])
+
def add_file_fields():
for format_ in Book.formats:
object_id=obj.pk,
tag__in=tags_for_removal).delete()
# Add new tags
- tags_to_add = [tag for tag in updated_tags
- if tag not in current_tags]
+ tags_to_add = [tag for tag in updated_tags if tag not in current_tags]
for tag in tags_to_add:
- if tag not in current_tags:
- self.intermediary_table_model.objects.create(tag=tag, content_object=obj)
+ self.intermediary_table_model.objects.create(tag=tag, content_object=obj)
tags_updated.send(sender=type(obj), instance=obj, affected_tags=tags_to_add + tags_for_removal)
self.intermediary_table_model.objects.filter(
content_type__pk=content_type.pk, object_id=obj.pk, tag=tag).delete()
+ def add_tag(self, obj, tag):
+ """
+ Add tag to an object.
+ """
+ content_type = ContentType.objects.get_for_model(obj)
+ relations = self.intermediary_table_model.objects.filter(
+ content_type__pk=content_type.pk, object_id=obj.pk, tag=tag)
+ if not relations:
+ self.intermediary_table_model.objects.create(tag=tag, content_object=obj)
+
def get_for_object(self, obj):
"""
Create a queryset matching all tags associated with the given
except Tag.DoesNotExist:
tag = Tag.objects.create(
category='set', user=user, name=name, slug=utils.get_random_hash(name), sort_key=name.lower())
+ except Tag.MultipleObjectsReturned:
+ # fix duplicated noname shelf
+ tags = list(Tag.objects.filter(category='set', user=user, name=name))
+ tag = tags[0]
+ for other_tag in tags[1:]:
+ for item in other_tag.items.all():
+ Tag.objects.remove_tag(item, other_tag)
+ Tag.objects.add_tag(item, tag)
+ other_tag.delete()
return tag
from ssify import ssi_included
from social import forms
from .models import Cite
-from social.utils import get_set, likes, set_sets
# ====================
if not request.user.is_authenticated():
return HttpResponseForbidden('Login required.')
book = get_object_or_404(Book, slug=slug)
- if not likes(request.user, book):
- tag = get_set(request.user, '')
- set_sets(request.user, book, [tag])
+
+ book.like(request.user)
if request.is_ajax():
return JsonResponse({"success": True, "msg": "ok", "like": True})
return redirect(book)
+@require_POST
+def unlike_book(request, slug):
+ if not request.user.is_authenticated():
+ return HttpResponseForbidden('Login required.')
+ book = get_object_or_404(Book, slug=slug)
+
+ book.unlike(request.user)
+
+ if request.is_ajax():
+ return JsonResponse({"success": True, "msg": "ok", "like": False})
+ else:
+ return redirect(book)
+
+
@login_required
def my_shelf(request):
return render(request, 'social/my_shelf.html', {
return (obj, request.user), {}
-@require_POST
-def unlike_book(request, slug):
- if not request.user.is_authenticated():
- return HttpResponseForbidden('Login required.')
- book = get_object_or_404(Book, slug=slug)
- if likes(request.user, book):
- set_sets(request.user, book, [])
-
- if request.is_ajax():
- return JsonResponse({"success": True, "msg": "ok", "like": False})
- else:
- return redirect(book)
-
-
@ssi_included
def cite(request, pk, main=False):
cite = get_object_or_404(Cite, pk=pk)