def mutate_and_get_payload(cls, *args, **kwargs): u = get_current_user() if u.is_superuser: try: image_instance = get_object(get_image_model(), kwargs['id']) if image_instance: image_data = kwargs.get('image') updated_image = update_create_instance( image_instance, image_data, exception=['id', 'tags']) tag_slugs = image_data.get('tag_slugs', '') if tag_slugs is not None: tag_slugs = [ t.strip() for t in tag_slugs.split(',') if t.strip() ] tags = list(Tag.objects.filter(slug__in=tag_slugs)) tag_slugs = [ t for t in tag_slugs if t not in [e.slug for e in tags] ] updated_image.tags.set(*(tags + tag_slugs)) if updated_image.pk: return cls(updated_image=updated_image, tags=Tag.objects.all()) except ValidationError as e: errors = get_errors(e) except Exception as e: errors = ['Error: {}'.format(e)] else: errors = ['image not found'] return cls(updated_image=None, tags=None, errors=errors)
def get_tags(request): obj = get_object(request.REQUEST.get("id", None)) tags = Tag.objects.filter(user=request.user, content_type=ContentType.objects.get_for_model(obj), object_id=obj.id).values_list("name", flat=True) return dict(tags=list(tags))
def set_tags(request): obj = get_object(request.REQUEST.get("id", None)) form = TagsForm(request.REQUEST, instance=obj, user=request.user) if form.is_valid(): form.save() return dict(status="success") else: return dict(errors=form._errors)
def get_rating(request): obj = get_object(request.REQUEST.get("id", None)) try: rating = Rating.objects.get( content_type=ContentType.objects.get_for_model(obj), object_id=obj.id, user=request.user ).value except Rating.DoesNotExist: rating = None return dict(rating=rating)
def get_review(request): obj = get_object(request.REQUEST.get("id", None)) try: review = Review.objects.get(content_type=ContentType.objects.get_for_model(obj), object_id=obj.id, user=request.user).text except Review.DoesNotExist: review = None return dict(review=review)
def set_review(request): obj = get_object(request.REQUEST.get("id", None)) content_type = ContentType.objects.get_for_model(obj) review = get_object_or_None(Review, content_type=content_type, object_id=obj.id, user=request.user, ) or Review( content_type=content_type, object_id=obj.id, user=request.user, ) form = ReviewForm(request.REQUEST, instance=review) if form.is_valid(): form.save() return dict(status="success") else: return dict(errors=form._errors)