def obj_create(self, bundle, **kwargs): """ Posts a new favorite """ try: try: user = User.objects.get(id=bundle.data["user_id"]) company = Company.objects.get(id=bundle.data["company_id"]) except: user = User.objects.get(id=bundle.data["user"]) company = Company.objects.get(id=bundle.data["company"]) print "1" if bundle.data['unfavorite']: print "2" a = Favorite.objects.filter(user=user).filter(company=company) a.delete() else: print "3" old_favorite = Favorite.objects.filter(user=user, company=company) if len(old_favorite) > 0: print "4" bundle.obj = old_favorite[0] else: print "5" new_favorite = Favorite(user=user, company=company) new_favorite.save() bundle.obj = new_favorite return bundle except Exception, e: print e
def add_favorite(request): to_return = {'msg' : u'Sin datos del POST' } to_return['success'] = False item_id = request.POST['item_id'] if request.method == "POST": post = request.POST.copy() if post.has_key('item_id'): if request.user.is_authenticated(): # Do something for authenticated users. user = request.user print item_id item = get_object_or_404(Item, pk = item_id) item_favorite = Favorite(item = item, user = user) item_favorite.save() to_return['success'] = True else: #product_favorite = 'Usuario no Logueado redireccionar a la forma login' to_return['msg'] = u"Usuario no logueado." #return HttpResponseRedirect('/') else : to_return['msg'] = u"No se proporciono el id para agregar a favoritos." else : to_return['msg'] = u"No es un POST." #return HttpResponseRedirect() #return HttpResponse(product_favorite) serialized = simplejson.dumps(to_return) return HttpResponse(serialized, mimetype="application/json")
def favorite(request): serialized = FavoriteSerializer(data=request.data) if serialized.is_valid(): access_token = serialized.validated_data['access_token'] reddit_id = serialized.validated_data['reddit_id'] tag_name = serialized.validated_data['tag_name'] token = Token.objects.get(key=access_token) user = User.objects.get(id=token.user_id) redditpost = RedditPost.objects.get(reddit_id=reddit_id) favorite = Favorite(user=user, access_token=access_token, reddit_id=reddit_id, permalink=redditpost.permalink, url=redditpost.url, author=redditpost.url, tag_name=tag_name) favorite.save() return Response("Favorite saved!", status=status.HTTP_201_CREATED) else: return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
def obj_create(self, bundle, **kwargs): """ Post favorites an event for the user if 'delete' is in param, then we delete that post instead """ try: if bundle.data.get('delete',False): try: print bundle.data event_id = bundle.data['event'] user_id = bundle.data['user'] event = Event.objects.get(id=event_id) user = User.objects.get(id=user_id) old_favorite = Favorite.objects.get(event=event, user=user) old_favorite.delete() # return some random Favorite object bundle.obj = Favorite.objects.all()[0] except Exception as e: print e raise ImmediateHttpResponse(HttpForbidden("Unable to unfavorite event. Please try again.")) return bundle else: try: print bundle.data event_id = bundle.data['event'] user_id = bundle.data['user'] event = Event.objects.get(id=event_id) user = User.objects.get(id=user_id) if len(Favorite.objects.filter(event=event, user=user)) == 0: new_favorite = Favorite(event=event, user=user) new_favorite.save() else: new_favorite = Favorite.objects.filter(event=event, user=user)[0] bundle.obj = new_favorite except Exception as e: print e raise ImmediateHttpResponse(HttpForbidden("Invalid POST bundle.")) return bundle except Exception as e: print e raise ImmediateHttpResponse(HttpForbidden("Unable to perform request. Please try again."))