Example #1
0
def product_detail(request, category_slug, slug):
    """
    Return details about a specific product according to a request
    Params:
        - category_slug:[String] A specific ID-like string for a specific category
        - slug: [String]  A specific ID-like string for a specific product
    Return:
        -[Render]: Return a product_detail.html containing information about a specific product
    """

    product = get_object_or_404(Product, slug=slug)
    product.num_visits += 1
    product.last_visit = datetime.now()
    product.save()

    # Add review
    if request.method == 'POST' and request.user.is_authenticated:
        stars = request.POST.get('stars', 3)
        content = request.POST.get('content', '')

        review = ProductReview.objects.create(product=product,
                                              user=request.user,
                                              stars=stars,
                                              content=content)

        return redirect('product_detail',
                        category_slug=category_slug,
                        slug=slug)
    #

    related_products = list(
        product.category.products.all().exclude(id=product.id))
    if (len(related_products) >= 3):
        related_products = random.sample(related_products, 3)

    imagesstring = "{'thumbnail': '%s', 'image': '%s'}," % (
        product.thumbnail.url, product.image.url)

    # for image in product.images.all():
    # imagesstring += ("{'thumbnail': '%s', 'image': '%s'}," %(image.thumbnail.url, image.image.url))

    cart = Cart(request)
    if cart.has_product(product.id):
        product.in_cart = True
    else:
        product.in_cart = False

    owned_stores = utils.get_owned_stores(request)

    product_form = EditProductForm(instance=product, stores=owned_stores)

    context = {
        'product': product,
        'imagesstring': imagesstring,
        'related_products': related_products,
        'owned_stores': owned_stores,
        'product_form': product_form,
    }

    return render(request, 'product_detail.html', context)
Example #2
0
def api_create_category(request):
    if request.method == 'POST' and request.user.is_authenticated:

        try:
            store_id = int(request.POST.get('store_id'))
            store = get_object_or_404(Store, id=store_id)
            owned_stores = utils.get_owned_stores(request)

            if store in owned_stores:
                title = request.POST.get('title')

                parent_id = request.POST.get('parent')
                parent = None
                if parent_id != '':
                    parent = get_object_or_404(Category, id=parent_id)
                ordering = int(request.POST.get('ordering'))
                is_featured = False if request.POST.get(
                    'is_featured') == 'false' else True
                image = request.FILES.get('image')

                category = Category(parent=parent,
                                    store=store,
                                    title=title,
                                    is_featured=is_featured,
                                    ordering=ordering,
                                    image=image)

                category.save()
                return redirect('category_detail', slug=category.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)
Example #3
0
def store_detail(request, slug):
    """
    Renders the details of a specific store using as a parameter the slug (stringify id)
    Params:
        -slug:[String] A string-like id
    Return:
        -[Render] Renders the store_detail.html
    """
    store = get_object_or_404(Store, slug=slug)

    store.num_visits += 1
    store.last_visit = datetime.now()
    store.save()

    owned_stores = utils.get_owned_stores(request)

    categories = store.categories.all()
    products = store.products.all()

    category_form = AddCategoryForm(stores=owned_stores)

    context = {
        'store': store,
        'products': products,
        'categories': categories,
        'owned_stores': owned_stores,
        'category_form': category_form,
    }

    return render(request, 'store_detail.html', context)
Example #4
0
def api_edit_product(request):
    if request.method == 'POST' and request.user.is_authenticated:

        product = get_object_or_404(Product, id=request.POST.get('id'))

        try:
            category_id = request.POST.get('category')
            product.category = get_object_or_404(Category, id=category_id)
            owned_stores = utils.get_owned_stores(request)

            if product.category.store in owned_stores:
                product.title = request.POST.get('title')
                product.description = request.POST.get('description')
                product.price = float(request.POST.get('price'))
                product.is_featured = False if request.POST.get(
                    'is_featured') == 'false' else True
                product.num_available = int(request.POST.get('num_available'))

                if request.FILES.get('image'):
                    product.image = request.FILES.get('image')
                    product.thumbnail = product.make_thumbnail(product.image)

                product.save()
                return redirect('product_detail',
                                category_slug=product.category.slug,
                                slug=product.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)
Example #5
0
def api_delete_product(request):
    if request.method == 'DELETE' and request.user.is_authenticated:

        try:
            data = json.loads(request.body)

            product_id = data['product_id']
            product = get_object_or_404(Product, id=product_id)

            store = get_object_or_404(Store, id=product.store.id)
            owned_stores = utils.get_owned_stores(request)

            if store in owned_stores:
                product.delete()
                return redirect('category_detail', slug=product.category.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)
Example #6
0
def api_delete_category(request):
    if request.method == 'DELETE' and request.user.is_authenticated:

        try:
            data = json.loads(request.body)

            category_id = data['category_id']
            category = get_object_or_404(Category, id=category_id)

            store = get_object_or_404(Store, id=category.store.id)
            owned_stores = utils.get_owned_stores(request)

            if store in owned_stores:
                category.delete()
                return redirect('store_detail', slug=category.store.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)
Example #7
0
def category_detail(request, slug):
    """
    Renders specific details of a category
    Params:
        -slug:[String] A specified id-like of a category
    Return:
        -[Render]: Renders the category_detail.html according to the category has been defined into the slug variable
    """
    category = get_object_or_404(Category, slug=slug)
    products = category.products.all()
    owned_stores = utils.get_owned_stores(request)

    category_form = EditCategoryForm(instance=category, stores=owned_stores)
    product_form = AddProductForm(stores=owned_stores)

    context = {
        'category': category,
        'products': products,
        'owned_stores': owned_stores,
        'category_form': category_form,
        'product_form': product_form,
    }

    return render(request, 'category_detail.html', context)
Example #8
0
def frontpage(request):
    """
    Redirect to the homepage, rendering the available stores, 
    products, most popular and your recent viewed
    """
    stores = Store.objects.filter(is_activated=True).order_by('-num_visits')
    products = Product.objects.filter(is_featured=True)
    featured_categories = Category.objects.filter(is_featured=True)
    popular_products = Product.objects.all().order_by('-num_visits')[0:4]
    recently_viewed_products = Product.objects.all().order_by(
        '-last_visit')[0:4]

    owned_stores = get_owned_stores(request)

    context = {
        'stores': stores,
        'products': products,
        'featured_categories': featured_categories,
        'popular_products': popular_products,
        'recently_viewed_products': recently_viewed_products,
        'owned_stores': owned_stores
    }

    return render(request, 'frontpage.html', context)
Example #9
0
def api_edit_category(request):
    if request.method == 'POST' and request.user.is_authenticated:

        category = get_object_or_404(Category, id=request.POST.get('id'))

        try:
            owned_stores = utils.get_owned_stores(request)
            if category.store in owned_stores:
                parent_id = request.POST.get('parent')
                if parent_id != '':
                    category.parent = get_object_or_404(Category, id=parent_id)
                category.title = request.POST.get('title')
                category.ordering = int(request.POST.get('ordering'))
                category.is_featured = False if request.POST.get(
                    'is_featured') == 'false' else True

                if request.FILES.get('image'):
                    category.image = request.FILES.get('image')

                category.save()
                return redirect('category_detail', slug=category.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)
Example #10
0
def api_create_product(request):
    if request.method == 'POST' and request.user.is_authenticated:

        try:
            category_id = request.POST.get('category')
            category = get_object_or_404(Category, id=category_id)
            owned_stores = utils.get_owned_stores(request)

            if category.store in owned_stores:
                title = request.POST.get('title')
                description = request.POST.get('description')
                price = float(request.POST.get('price'))
                is_featured = False if request.POST.get(
                    'is_featured') == 'false' else True
                image = request.FILES.get('image')
                num_available = int(request.POST.get('num_available'))
                store = category.store

                product = Product(store=store,
                                  title=title,
                                  is_featured=is_featured,
                                  category=category,
                                  description=description,
                                  price=price,
                                  num_available=num_available,
                                  image=image)

                product.thumbnail = product.make_thumbnail(product.image)

                product.save()
                return redirect('product_detail',
                                category_slug=category.slug,
                                slug=product.slug)

        except Exception as e:
            return HttpResponseBadRequest(e)