def like(request, content_type, object_id): try: if not request.user.is_authenticated(): raise AccessError mode = request.POST["mode"] content_type = get_object_or_404(ContentType, id=int(content_type)) object_id = int(object_id) try: thelike = Like.objects.get(user=request.user, content_type=content_type, object_id=object_id) except: thelike = Like(user=request.user, content_type=content_type, object_id=object_id) thelike.save() if mode == "like": if not thelike.like: thelike.like = True thelike.dislike = False like_en = True else: thelike.like = False like_en = False dislike_en = False else: if not thelike.dislike: thelike.dislike = True thelike.like = False dislike_en = True else: thelike.dislike = False dislike_en = False like_en = False thelike.save() karma = thelike.content_object.karma() payload = {"success": True, "karma": karma, "liked": like_en, "disliked": dislike_en} except AccessError: payload = {"success": False, "error": "Not allowed"} except: payload = {"success": False} return AjaxLazyAnswer(payload)
def like(request, content_type, object_id): # noinspection PyBroadException try: if not request.user.is_authenticated(): raise AccessError mode = request.POST['mode'] if mode not in ['like', 'dislike']: raise AccessError content_type = ContentType.objects.get_for_id(int(content_type)) object_id = int(object_id) if content_type == ContentType.objects.get_for_model(get_user_model()): if object_id == request.user.pk: raise AccessError else: obj = content_type.get_object_for_this_type(pk=object_id) if obj.user == request.user: raise AccessError # noinspection PyBroadException try: thelike = Like.objects.get(user=request.user, content_type=content_type, object_id=object_id) except: thelike = Like(user=request.user, content_type=content_type, object_id=object_id) thelike.save() if mode == 'like': if thelike.like_dislike is not None: if thelike.like_dislike: thelike.like_dislike = None like_en = False else: thelike.like_dislike = True like_en = True else: thelike.like_dislike = True like_en = True dislike_en = False else: if thelike.like_dislike is not None: if not thelike.like_dislike: thelike.like_dislike = None dislike_en = False else: thelike.like_dislike = False dislike_en = True else: thelike.like_dislike = False dislike_en = True like_en = False thelike.save() karma = thelike.content_object.karma payload = { 'success': True, 'karma': karma, 'liked': like_en, 'disliked': dislike_en } except AccessError as aerr: payload = {'success': False, 'error': 'Not allowed'} except: payload = {'success': False} return ajax_answer_lazy(payload)
def like(request, content_type, object_id): # noinspection PyBroadException try: if not request.user.is_authenticated(): raise AccessError mode = request.POST['mode'] if mode not in ['like', 'dislike']: raise AccessError content_type = ContentType.objects.get_for_id(int(content_type)) object_id = int(object_id) if content_type == ContentType.objects.get_for_model(get_user_model()): if object_id == request.user.pk: raise AccessError else: obj = content_type.get_object_for_this_type(pk=object_id) if obj.user == request.user: raise AccessError # noinspection PyBroadException try: thelike = Like.objects.get(user=request.user, content_type=content_type, object_id=object_id) except: thelike = Like(user=request.user, content_type=content_type, object_id=object_id) thelike.save() if mode == 'like': if thelike.like_dislike is not None: if thelike.like_dislike: thelike.like_dislike = None like_en = False else: thelike.like_dislike = True like_en = True else: thelike.like_dislike = True like_en = True dislike_en = False else: if thelike.like_dislike is not None: if not thelike.like_dislike: thelike.like_dislike = None dislike_en = False else: thelike.like_dislike = False dislike_en = True else: thelike.like_dislike = False dislike_en = True like_en = False thelike.save() karma = thelike.content_object.karma payload = {'success': True, 'karma': karma, 'liked': like_en, 'disliked': dislike_en} except AccessError as aerr: payload = {'success': False, 'error': 'Not allowed'} except: payload = {'success': False} return ajax_answer_lazy(payload)
def like(request, content_type, object_id): try: if not request.user.is_authenticated(): raise AccessError mode = request.POST['mode'] content_type = get_object_or_404(ContentType, id=int(content_type)) object_id = int(object_id) try: thelike = Like.objects.get(user=request.user, content_type=content_type, object_id=object_id) except: thelike = Like(user=request.user, content_type=content_type, object_id=object_id) thelike.save() if mode == 'like': if not thelike.like: thelike.like = True thelike.dislike = False like_en = True else: thelike.like = False like_en = False dislike_en = False else: if not thelike.dislike: thelike.dislike = True thelike.like = False dislike_en = True else: thelike.dislike = False dislike_en = False like_en = False thelike.save() karma = thelike.content_object.karma() payload = {'success': True, 'karma': karma, 'liked': like_en, 'disliked': dislike_en} except AccessError: payload = {'success': False, 'error':'Not allowed'} except: payload = {'success': False} return AjaxLazyAnswer(payload)
def like(request, content_type, object_id): # noinspection PyBroadException try: if not request.user.is_authenticated: raise AccessError mode = request.POST['mode'] if mode not in ['like', 'dislike']: raise AccessError content_type = ContentType.objects.get_for_id(int(content_type)) object_id = int(object_id) if content_type == ContentType.objects.get_for_model(get_user_model()): if object_id == request.user.pk: raise AccessError else: obj = content_type.get_object_for_this_type(pk=object_id) if obj.user == request.user: pass #raise AccessError # noinspection PyBroadException try: thelike = Like.objects.get(user=request.user, content_type=content_type, object_id=object_id) except: thelike = Like(user=request.user, content_type=content_type, object_id=object_id) thelike.save() if mode == 'like': if thelike.status is not None: if thelike.status: thelike.status = None like_en = False else: thelike.status = True like_en = True else: thelike.status = True like_en = True dislike_en = False else: if thelike.status is not None: if not thelike.status: thelike.status = None dislike_en = False else: thelike.status = False dislike_en = True else: thelike.status = False dislike_en = True like_en = False thelike.save() karma = thelike.content_object.karma payload = dict(success=True, karma=karma, liked=like_en, disliked=dislike_en) except AccessError as aerr: payload = dict(success=False, error=_("Not allowed")) except: payload = dict(success=False) return ajax_answer_lazy(payload)