Exemple #1
0
def add_version(request, product_slug):
    if request.method != "PATCH":
        return JSONResponse({'success': False, 'response': 405, 'detail': 'Invalid method!'})
    else:
        try:
            product = Product.objects.get(slug=product_slug)
        except Product.DoesNotExist:
            return JSONResponse({'success': False, 'response': 404, 'detail': 'Invalid product slug.'})

        if product.version is not None and not request.user.is_authenticated:
            return JSONResponse({'success': False, 'response': 403, 'detail': 'You should login first.'})

        version = Version()
        version.version_code = 0
        version.product = product
        if request.user.is_authenticated:
            version.editor = Profile.get_user_profile(request.user)
        else:
            version.editor = Profile.get_user_profile(get_sentinel_user())

        form = VersionForm(request.data, request.FILES, instance=version)
        if form.is_valid():
            form.save()
            data = {'success': True, 'detail': _(u'Successfully added your update. We will review and apply it asap!')}
        else:
            return JSONResponse({'success': False, 'response': 555, 'detail': dict(form.errors.items())})

    data = pack_data(request, data)
    return JSONResponse(data)
Exemple #2
0
    def save(self, commit=True):
        user = super(SignupForm, self).save(commit)
        user.set_password(self.cleaned_data['password'])
        user.username = user.email.lower()
        user.save()

        profile = Profile(user=user)
        profile.save()

        return user
Exemple #3
0
    def create(self, validated_data):
        password = validated_data.pop('password')
        validated_data['username'] = validated_data['email'].lower()

        user = super().create(validated_data)
        user.set_password(password)
        user.save()

        profile = Profile(user=user)
        profile.save()

        return user
Exemple #4
0
def review_product(request, product_slug):
    if request.method != "POST":
        return JSONResponse({'success': False, 'response': 405, 'detail': 'Invalid method!'})
    elif not request.user.is_authenticated():
        return JSONResponse({'success': False, 'response': 403, 'detail': 'Login required!'})
    else:
        if product_slug.endswith("/"):
            product_slug = product_slug[:-1]

        try:
            product = Product.objects.get(slug=product_slug, status="pub")

            comment = Comment()
            comment.user = Profile.get_user_profile(request.user)
            comment.product = product
            comment_form = CommentForm(request.POST, instance=comment)

            if comment_form.is_valid():
                comment_form.save()
                data = {'success': True}
            else:
                return JSONResponse({'success': False, 'response': 555, 'detail': dict(comment_form.errors.items())})

        except Product.DoesNotExist:
            return JSONResponse({'success': False, 'response': 404, 'detail': 'Invalid product slug!'})

    return JSONResponse(data)
Exemple #5
0
def get_rating(request, product_slug):
    if not request.user.is_authenticated():
        return JSONResponse({'success': False, 'response': 403, 'detail': 'Login required!'})
    else:
        if product_slug.endswith("/"):
            product_slug = product_slug[:-1]

        try:
            product = Product.objects.get(slug=product_slug, status="pub")
            user_profile = Profile.get_user_profile(request.user)
            try:
                rate = Rate.objects.get(user_id=user_profile.id, product_id=product.id,
                                        user_type=user_profile.is_editor)
            except Rate.MultipleObjectsReturned:
                rates = Rate.objects.filter(user_id=user_profile.id, product_id=product.id,
                                            user_type=user_profile.is_editor)
                rate = rates[0]
                for i in range(1, rates.count()):
                    _rate = rates[i]
                    _rate.delete()
            except Rate.DoesNotExist:
                return JSONResponse({'success': True, 'rate': 0, 'p_rate_count': product.p_rate_count()})
            data = {'success': True, 'rate': rate.rate, 'p_rate_count': product.p_rate_count()}

        except Product.DoesNotExist:
            return JSONResponse({'success': False, 'response': 404, 'detail': 'Invalid product slug!'})

    return JSONResponse(data)
Exemple #6
0
def random_product_page(request):
    products = Product.objects.filter(status="pub")

    from random import randint
    random_index = randint(0, products.count())
    product = products[random_index]
    product.hit()

    rate = None
    if request.user.is_authenticated:
        user_profile = Profile.get_user_profile(request.user)
        try:
            rate = Rate.objects.get(user_id=user_profile.id, product_id=product.id, user_type=user_profile.is_editor)
        except Rate.DoesNotExist:
            pass
        except Rate.MultipleObjectsReturned:
            rates = Rate.objects.filter(user_id=user_profile.id, product_id=product.id,
                                        user_type=user_profile.is_editor)
            rate = rates[0]
            for i in range(1, rates.count()):
                _rate = rates[i]
                _rate.delete()

    comments = product.comments.filter(status="pub")

    product_data = ProductSerializer(product).data
    comments_data = CommentSerializer(comments, many=True).data

    data = {'product': product_data, 'comments': comments_data, 'rate': None}

    if rate is not None:
        data['rate'] = rate.rate

    data = pack_data(request, data)
    return JSONResponse(data)
Exemple #7
0
def product_page(request, slug):
    if slug.endswith("/"):
        slug = slug[:-1]

    product = get_object_or_404(Product, slug=slug, status="pub")

    product.hit()

    rate = None
    if request.user.is_authenticated:
        user_profile = Profile.get_user_profile(request.user)
        try:
            rate = Rate.objects.get(user_id=user_profile.id, product_id=product.id, user_type=user_profile.is_editor)
        except Rate.DoesNotExist:
            pass
        except Rate.MultipleObjectsReturned:
            rates = Rate.objects.filter(user_id=user_profile.id, product_id=product.id,
                                        user_type=user_profile.is_editor)
            rate = rates[0]
            for i in range(1, rates.count()):
                _rate = rates[i]
                _rate.delete()

    comments = product.comments.filter(status="pub")

    product_data = ProductSerializer(product).data
    comments_data = CommentSerializer(comments, many=True).data

    data = {'product': product_data, 'comments': comments_data, 'rate': None}

    if rate is not None:
        data['rate'] = rate.rate

    data = pack_data(request, data)
    return JSONResponse(data)
Exemple #8
0
def rate_product(request, product_slug):
    if request.method != "POST":
        return JSONResponse({'success': False, 'response': 405, 'detail': 'Invalid method!'})
    elif not request.user.is_authenticated():
        return JSONResponse({'success': False, 'response': 403, 'detail': 'Login required!'})
    else:
        if product_slug.endswith("/"):
            product_slug = product_slug[:-1]

        try:
            product = Product.objects.get(slug=product_slug, status="pub")
            user_profile = Profile.get_user_profile(request.user)
            try:
                rate = Rate.objects.get(user_id=user_profile.id, product_id=product.id,
                                        user_type=user_profile.is_editor)
            except Rate.MultipleObjectsReturned:
                rates = Rate.objects.filter(user_id=user_profile.id, product_id=product.id,
                                            user_type=user_profile.is_editor)
                rate = rates[0]
                for i in range(1, rates.count()):
                    _rate = rates[i]
                    _rate.delete()
            except Rate.DoesNotExist:
                rate = Rate()
                user_profile = Profile.get_user_profile(request.user)
                rate.user = user_profile
                rate.user_type = user_profile.is_editor
                rate.product = product

            rate_form = RateForm(request.POST, instance=rate)
            if rate_form.is_valid():
                rate_form.save()
                data = {'success': True, 'new_p_rate': product.average_p_rate, 'new_e_rate': product.average_e_rate,
                        'p_rate_count': product.p_rate_count()}

            else:
                return JSONResponse({'success': False, 'response': 555, 'detail': dict(rate_form.errors.items())})

        except Product.DoesNotExist:
            return JSONResponse({'success': False, 'response': 404, 'detail': 'Invalid product slug!'})

    return JSONResponse(data)
Exemple #9
0
def rate_product(request, product_slug):
    if product_slug.endswith("/"):
        product_slug = product_slug[:-1]

    data = {'success': False, 'user': True}

    if not request.user.is_authenticated():
        data['user'] = False

    elif request.is_ajax() and request.method == "POST":
        try:
            product = Product.objects.get(slug=product_slug, status="pub")
            user_profile = Profile.get_user_profile(request.user)
            try:
                rate = Rate.objects.get(user_id=user_profile.id,
                                        product_id=product.id,
                                        user_type=user_profile.is_editor)
            except Rate.MultipleObjectsReturned:
                rates = Rate.objects.filter(user_id=user_profile.id,
                                            product_id=product.id,
                                            user_type=user_profile.is_editor)
                rate = rates[0]
                for i in range(1, rates.count()):
                    _rate = rates[i]
                    _rate.delete()
            except Rate.DoesNotExist:
                rate = Rate()
                user_profile = Profile.get_user_profile(request.user)
                rate.user = user_profile
                rate.user_type = user_profile.is_editor
                rate.product = product
            rate_form = RateForm(request.POST, instance=rate)
            if rate_form.is_valid():
                rate_form.save()
                data['success'] = True

        except Product.DoesNotExist:
            pass

    mimetype = 'application/json'
    return HttpResponse(json.dumps(data), mimetype)
Exemple #10
0
def add_version(request, product_id):
    product = get_object_or_404(Product, id=product_id)

    if product.version is not None and not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('login'))

    last_version = product.last_approved_version()
    if last_version:
        initial_data = model_to_dict(last_version)
    else:
        initial_data = {}
    form = VersionForm(initial_data)

    if request.method == "POST":
        version = Version()
        version.version_code = 0
        version.product = product
        if request.user.is_authenticated:
            version.editor = Profile.get_user_profile(request.user)
        else:
            version.editor = Profile.get_user_profile(get_sentinel_user())

        form = VersionForm(request.POST, request.FILES, instance=version)
        if form.is_valid():
            form.save()
            data = {
                'message':
                _(u'Successfully added your update. We will review and apply it asap!'
                  ),
                'home':
                True
            }
            data.update(csrf(request))
            return render_to_response('common/message.html', data)

    data = {'form': form}
    data = pack_data(request, data)
    return render_to_response('products/add_version.html', data)
Exemple #11
0
def add_product(request):
    form = ProductForm()

    if request.method == 'POST':
        product = Product()

        if request.user.is_authenticated:
            product.creator = Profile.get_user_profile(request.user)
        else:
            product.creator = Profile.get_user_profile(get_sentinel_user())

        form = ProductForm(request.POST, instance=product)
        if form.is_valid():
            product = form.save()
            if product:
                return HttpResponseRedirect(
                    reverse('add_version', args=[product.id]))

    types = Type.objects.all()

    data = {'form': form, 'types': types}
    data = pack_data(request, data)
    return render_to_response('products/add.html', data)
Exemple #12
0
def add_product(request):
    if request.method != "POST":
        return JSONResponse({'success': False, 'response': 405, 'detail': 'Invalid method!'})
    else:
        product = Product()
        if request.user.is_authenticated:
            product.creator = Profile.get_user_profile(request.user)
        else:
            product.creator = Profile.get_user_profile(get_sentinel_user())

        form = ProductForm(request.POST, instance=product)
        if form.is_valid():
            product = form.save()
            if product:
                data = {'success': True, 'slug': product.slug}
            else:
                return JSONResponse(
                    {'success': False, 'response': 500, 'detail': 'Failed to set password. Please try again!'})
        else:
            return JSONResponse({'success': False, 'response': 555, 'detail': dict(form.errors.items())})

    data = pack_data(request, data)
    return JSONResponse(data)
Exemple #13
0
def product_page(request, slug):
    if slug.endswith("/"):
        slug = slug[:-1]

    product = get_object_or_404(Product, slug=slug, status="pub")
    version = product.last_approved_version()

    product.hit()

    comment_form = CommentForm()

    user_profile = None
    rate = None
    if request.user.is_authenticated:
        user_profile = Profile.get_user_profile(request.user)
        try:
            rate = Rate.objects.get(user_id=user_profile.id,
                                    product_id=product.id,
                                    user_type=user_profile.is_editor)
        except Rate.DoesNotExist:
            pass
        except Rate.MultipleObjectsReturned:
            rates = Rate.objects.filter(user_id=user_profile.id,
                                        product_id=product.id,
                                        user_type=user_profile.is_editor)
            rate = rates[0]
            for i in range(1, rates.count()):
                _rate = rates[i]
                _rate.delete()

    rate_form = RateForm(instance=rate)

    comments = product.comments.filter(status="pub")

    commented = False
    rated = False

    if request.method == "POST":
        if not request.user.is_authenticated:
            return HttpResponseRedirect(reverse('login'))

        if "rating" in request.POST:
            if rate is None:
                rate = Rate()
                user_profile = Profile.get_user_profile(request.user)
                rate.user = user_profile
                rate.user_type = user_profile.is_editor
                rate.product = product
            rate_form = RateForm(request.POST, instance=rate)
            if rate_form.is_valid():
                rate_form.save()
                commented = True

        elif "comment" in request.POST:
            comment = Comment()
            comment.user = Profile.get_user_profile(request.user)
            comment.product = product
            comment_form = CommentForm(request.POST, instance=comment)
            if comment_form.is_valid():
                comment_form.save()
                comment_form = CommentForm()
                commented = True

    data = {
        'product': product,
        'version': version,
        'rate_form': rate_form,
        'comment_form': comment_form,
        'comments': comments,
        'commented': commented,
        'rated': rated
    }
    data = pack_data(request, data)
    return render_to_response('products/product_page.html', data)