def put(self, request, *args, **kwargs): ad_id = int(kwargs.get('pk')) ad = ProductAd.objects.get(id=ad_id) serializer = AdSerializer(ad, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, *args, **kwargs): ad_id = int(kwargs.get("pk")) ad = ProductAd.objects.get(id=ad_id) serializer = AdSerializer(ad, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, username, format=None): if request.user.username != username: return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = AdSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=request.user) # helpers.delete_ad(serializer.data.get('id'), 60) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def _really_delete(pk): """Delete an ad by primary key and send a push notification.""" notify_data = {} try: ad = ProductAd.objects.get(pk=pk) notify_data.update({'ad_id': ad.id}) notify_data.update({'ad_owner': str(ad.owner)}) serializer = AdSerializer(ad) if not did_someone_bid(pk): ad.delete() notify_data.update({'type': 'ad_expired'}) send_push_by_subscribed_categories(notify_data, serializer.data.get('category')) else: # Sell to the highest bidder highest_bid = get_highest_bid(pk) notify_data.update({'type': 'sold_to_highest_bidder'}) notify_data.update({'price': str(highest_bid.bid)}) notify_data.update({'sold_to': highest_bid.bidder_name()}) ad.sold = True ad.sold_to = highest_bid.bidder_name() ad.save() send_push_by_subscribed_categories(notify_data, serializer.data.get('category')) except ProductAd.DoesNotExist: # Just do nothing if the ad was already deleted. pass
def post(self, request, username, format=None): if request.user.username != username: return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = AdSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=request.user) new_ad_id = serializer.data.get("id") notify_data = {} notify_data.update({"type": "new_ad_posted"}) notify_data.update({"ad_id": new_ad_id}) notify_data.update({"ad_owner": serializer.data.get("owner")}) helpers.send_push_by_subscribed_categories(notify_data, serializer.data.get("category")) # Set an alarm to delete the ad after 24Hours helpers.delete_ad(new_ad_id, delay=TWENTY_FOUR_HOURS) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, username, format=None): if request.user.username != username: return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = AdSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=request.user) new_ad_id = serializer.data.get('id') notify_data = {} notify_data.update({'type': 'new_ad_posted'}) notify_data.update({'ad_id': new_ad_id}) notify_data.update({'ad_owner': serializer.data.get('owner')}) helpers.send_push_by_subscribed_categories( notify_data, serializer.data.get('category')) # Set an alarm to delete the ad after 24Hours helpers.delete_ad(new_ad_id, delay=TWENTY_FOUR_HOURS) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, *args, **kwargs): ad = helpers.get_ad_by_primary_key(self, request, kwargs.get('pk')) serializer = AdSerializer(ad) return Response(serializer.data)
def get(self, request, username, pk, format=None): ad = helpers.get_ad_by_primary_key(self, request, pk) serializer = AdSerializer(ad) return Response(serializer.data)