Ejemplo n.º 1
0
    def obj_create(self, bundle, request=None, **kwargs):
        """
        Handle POST requests to the resource. If the data validates, create a
        new Review from bundle data.
        """
        form = ReviewForm(bundle.data)

        if not form.is_valid():
            raise self.form_errors(form)

        app = self.get_app(bundle.data['app'])

        # Return 409 if the user has already reviewed this app.
        if self._meta.queryset.filter(addon=app, user=request.user).exists():
            raise ImmediateHttpResponse(response=http.HttpConflict())

        # Return 403 if the user is attempting to review their own app:
        if app.has_author(request.user):
            raise ImmediateHttpResponse(response=http.HttpForbidden())

        # Return 403 if not a free app and the user hasn't purchased it.
        if app.is_premium() and not app.is_purchased(request.amo_user):
            raise ImmediateHttpResponse(response=http.HttpForbidden())

        bundle.obj = Review.objects.create(
            **self._review_data(request, app, form))

        amo.log(amo.LOG.ADD_REVIEW, app, bundle.obj)
        log.debug('[Review:%s] Created by user %s ' %
                  (bundle.obj.id, request.user.id))
        record_action('new-review', request, {'app-id': app.id})

        return bundle
    def obj_create(self, bundle, **kwargs):
        """
        A new end point for login the user using the django login system
        """
        logger.debug('UserResource.obj_create')

        data = bundle.data
        email = data['email'].lower()

        # try to geet the user by email
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            # the user with this email does not exist
            # create a new user
            user = User.objects.create(email=email,
                                       first_name=data['first_name'],
                                       last_name=data['last_name'])
            user.set_password(data['password'])
            user.save()

        user = authenticate(email=email, password=data['password'])

        try:
            login(bundle.request, user)
        except:
            response = http.HttpConflict(
                json.dumps("This email is already registered"),
                content_type=bundle.request.META['CONTENT_TYPE'])
            raise ImmediateHttpResponse(response=response)

        bundle.obj = user
        return bundle
Ejemplo n.º 3
0
    def obj_create(self, bundle, **kwargs):
        try:
            return super(WakeUpMixinResource,
                         self).obj_create(bundle, **kwargs)
        except NotUniqueError:
            unique_fields = [
                field_name for field_name, field in
                self._meta.object_class._fields.iteritems() if field.unique
            ]

            # If the header is set, try to wake up the document
            # Moving to the update process
            if bundle.request.META.get('HTTP_X_WAKEUP'):
                filter_kwargs = {}
                for field in unique_fields:
                    filter_kwargs[field] = bundle.data.get(field)
                bundle.obj = self.get_object_list(
                    bundle.request).filter(**filter_kwargs)[0]
                bundle.obj.status = bundle.request.META.get('HTTP_X_WAKEUP')
                return self.obj_update(bundle, **kwargs)

            # If the header is not set, raise an explicative error message
            if len(unique_fields) == 1:
                err_msg = 'A document with this {0} already exists. It can be waked up thanks to the X-WakeUp header (if in a DELETED/INACTIVE status)  or you can set different values'.format(
                    *unique_fields)
            else:
                err_msg = 'A document with these {0} already exists. It can be waked up thanks to the X-WakeUp header (if in a DELETED/INACTIVE status)  or you can set different values'.format(
                    ', '.join(unique_fields))
            raise ZombiePostConflict(http.HttpConflict(err_msg))
Ejemplo n.º 4
0
 def post_list(self, request, review_id=None, **kwargs):
     if ReviewFlag.objects.filter(review_id=review_id,
                                  user=request.amo_user).exists():
         return http.HttpConflict()
     return MarketplaceModelResource.post_list(self,
                                               request,
                                               review_id=review_id,
                                               **kwargs)
Ejemplo n.º 5
0
class ZombiePostConflict(ImmediateHttpResponse):
    _response = http.HttpConflict(
        'This document already exists. It can be waked up thanks to the X-WakeUp header'
    )

    def __init__(self, response=None):
        if response:
            self._response = response
Ejemplo n.º 6
0
 def obj_create(self, bundle, **kwargs):
     pin = Pin.objects.get(pk=kwargs['pk'])
     likes = Like.objects.filter(pin=pin, user=bundle.request.user)
     user = User.objects.filter(id=bundle.request.user.id)[0]
     if likes.exists():
         raise ImmediateHttpResponse(
             http.HttpConflict(
                 "You are not allowed to like this Pin multiple times."))
     return super(LikeResource, self).obj_create(bundle, pin=pin, user=user)
Ejemplo n.º 7
0
 def obj_create(self, bundle, **kwargs):
     bundle.obj = bom.User(_id=bundle.data["email"])
     bundle.obj.set_password(bundle.data["password"])
     try:
         self._client.create_user(bundle.obj)
     except mongodb.MongoDriver.UserExistsError as e:
         raise exceptions.ImmediateHttpResponse(
             response=http.HttpConflict(e))
     return bundle
Ejemplo n.º 8
0
 def obj_delete_list(self, bundle, **kwargs):
     pin = Pin.objects.get(pk=kwargs['pk'])
     likes = Like.objects.filter(pin=pin, user=bundle.request.user)
     if len(likes) != 1:
         raise ImmediateHttpResponse(
             http.HttpConflict(
                 "Multiple likes exist from you on this Pin, something went wrong."
             ))
     return super(LikeResource,
                  self).obj_delete_list(bundle,
                                        user=bundle.request.user,
                                        pin=pin,
                                        **kwargs)