예제 #1
0
    def post(self, request):
        # TODO: clean the code and use class based views.
        """View used by the crawler to upload watches.
        Testing using the crawler is easier.
        """
        # check user permissions
        if not request.user.has_perm('main.add_watch'):
            return HttpResponse(status=403)

        data = json.loads(request.data['json'])
        data['brand'] = super_clean_str(data['brand'])
        extra = AddWatchView.get_extra(data)

        # Upload the watch only if there is a brand for it in the database
        try:
            data['brand'] = Brand.objects.get(cleaned_name=data['brand'])
        except Brand.DoesNotExist:
            return HttpResponse(status=409)

        watch = AddWatchView.get_or_create_watch(data, request.FILES["image"])

        if extra['price']:
            AddWatchView.save_price(request, watch, extra)

        return HttpResponse(status=200)
예제 #2
0
    def get_extra(data):
        extra_data = {}
        extra_data['sale_url'] = data['sale_url']
        extra_data['store'] = super_clean_str(data['store'])
        extra_data['price'] = super_clean_price(data['price'])
        extra_data['currency'] = data['currency']
        del data['sale_url']
        del data['store']
        del data['price']
        del data['currency']

        return extra_data
예제 #3
0
    def get_or_create_watch(data, image):
        # TODO: Clear the danger
        try:
            watch = Watch.objects.get(
                cleaned_reference=super_clean_str(data['reference']))
        except Watch.DoesNotExist:
            watch = Watch(
                **data
            )  # !DANGER: Data must contain the same fields as the watch
            watch.image = image
            watch.save()

        return watch
예제 #4
0
    def save(self, *args, **kwargs):
        self.cleaned_reference = super_clean_str(self.reference)

        super(Watch, self).save(*args, **kwargs)
예제 #5
0
 def save(self, *args, **kwargs):
     self.cleaned_name = super_clean_str(self.name)
     super(Brand, self).save(*args, **kwargs)