# Path: src/products/models.py
# class Product(models.Model):
#     user = models.ForeignKey(User, null=True, blank=True)
#     title = models.CharField(max_length=180)
#     description = models.CharField(max_length=500)
#     download = models.FileField(upload_to=download_loc, storage=FileSystemStorage(location=protected_loc), null=True)
#     price = models.DecimalField(max_digits=20, decimal_places=2)
#     sale_price = models.DecimalField(max_digits=20, decimal_places=2, null=True, blank=True)
#     slug = models.SlugField()
#     order = models.IntegerField(default=0)
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     active = models.BooleanField(default=True)
#     
#     def __unicode__(self):
#         return str(self.title)
#     
#     class Meta:
#         ordering = ['-order']
#         
#     def get_price(self):
#         if self.sale_price > 0:
#             return self.sale_price
#         else:
#             return self.price
#     
#     def get_featured_image(self):
#         try:
#             images = self.productimage_set.all()
#         except:
#             return None
#         
#         for i in images:
#             if i.featured_image:
#                 return i.image
#             else:
#                 return None
# 
# 
#     def is_active(self):
#         return self.active
#         
#     def get_absolute_url(self, ):
#         return reverse('single_product', args=[self.slug])
# 
# class Category(models.Model):
#     products = models.ManyToManyField(Product)
#     title = models.CharField(max_length=120)
#     description = models.CharField(max_length=500)
#     slug = models.SlugField()
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     
#     def __unicode__(self):
#         return str(self.title)
#     
#     class Meta:
#         verbose_name = "Category"
#         verbose_name_plural = "Categories"
#     
#     def get_absolute_url(self, ):
#         return reverse('category', args=[self.slug])
# 
# class ProductImage(models.Model):
#     product = models.ForeignKey(Product)
#     image = models.ImageField(upload_to="products/image/")
#     title = models.CharField(max_length=120, null=True, blank=True)
#     featured_image = models.BooleanField(default=False)
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     
#     def __unicode__(self):
#         return str(self.title)
# 
# Path: src/products/forms.py
# class ProductForm(ModelForm):
#     class Meta:
#         model = Product
#         fields = ('title', 'description', 'price', 'sale_price')
# 
# class ProductImageForm(ModelForm):
#     class Meta:
#         model = ProductImage
#         fields = ('image', 'featured_image', )


import os
from itertools import chain
from mimetypes import guess_type
from django.conf import settings
from django.core.servers.basehttp import FileWrapper
from django.db.models import Q
from django.forms.models import modelformset_factory
from django.shortcuts import render, RequestContext, Http404, HttpResponseRedirect, HttpResponse
from django.template.defaultfilters import slugify
from cart.models import Cart
from .models import Product, Category, ProductImage
from .forms import ProductForm, ProductImageForm
    return render(request, "products/all.html", context)

def add_product(request):
    
    form = ProductForm(request.POST or None)
        
    if form.is_valid():
        product = form.save(commit=False)
        product.user = request.user
        product.slug = slugify(form.cleaned_data['title'])
        product.active = False
        product.save()
        return HttpResponseRedirect('/products/%s'%(product.slug))
    
    context = {
        "form": form,
    }

    return render(request, "products/edit.html",context)


def manage_product_image(request, slug):
    try:
        product = Product.objects.get(slug=slug)
    except:
        product = False

    
    if request.user == product.user:
        queryset = ProductImage.objects.filter(product__slug=slug)

# Path: src/products/models.py
# class Product(models.Model):
#     user = models.ForeignKey(User, null=True, blank=True)
#     title = models.CharField(max_length=180)
#     description = models.CharField(max_length=500)
#     download = models.FileField(upload_to=download_loc, storage=FileSystemStorage(location=protected_loc), null=True)
#     price = models.DecimalField(max_digits=20, decimal_places=2)
#     sale_price = models.DecimalField(max_digits=20, decimal_places=2, null=True, blank=True)
#     slug = models.SlugField()
#     order = models.IntegerField(default=0)
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     active = models.BooleanField(default=True)
#     
#     def __unicode__(self):
#         return str(self.title)
#     
#     class Meta:
#         ordering = ['-order']
#         
#     def get_price(self):
#         if self.sale_price > 0:
#             return self.sale_price
#         else:
#             return self.price
#     
#     def get_featured_image(self):
#         try:
#             images = self.productimage_set.all()
#         except:
#             return None
#         
#         for i in images:
#             if i.featured_image:
#                 return i.image
#             else:
#                 return None
# 
# 
#     def is_active(self):
#         return self.active
#         
#     def get_absolute_url(self, ):
#         return reverse('single_product', args=[self.slug])
# 
# class Category(models.Model):
#     products = models.ManyToManyField(Product)
#     title = models.CharField(max_length=120)
#     description = models.CharField(max_length=500)
#     slug = models.SlugField()
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     
#     def __unicode__(self):
#         return str(self.title)
#     
#     class Meta:
#         verbose_name = "Category"
#         verbose_name_plural = "Categories"
#     
#     def get_absolute_url(self, ):
#         return reverse('category', args=[self.slug])
# 
# class ProductImage(models.Model):
#     product = models.ForeignKey(Product)
#     image = models.ImageField(upload_to="products/image/")
#     title = models.CharField(max_length=120, null=True, blank=True)
#     featured_image = models.BooleanField(default=False)
#     timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     
#     def __unicode__(self):
#         return str(self.title)
# 
# Path: src/products/forms.py
# class ProductForm(ModelForm):
#     class Meta:
#         model = Product
#         fields = ('title', 'description', 'price', 'sale_price')
# 
# class ProductImageForm(ModelForm):
#     class Meta:
#         model = ProductImage
#         fields = ('image', 'featured_image', )


# Path: src/products/views.py
import os
from itertools import chain
from mimetypes import guess_type
from django.conf import settings
from django.core.servers.basehttp import FileWrapper
from django.db.models import Q
from django.forms.models import modelformset_factory
from django.shortcuts import render, RequestContext, Http404, HttpResponseRedirect, HttpResponse
from django.template.defaultfilters import slugify
from cart.models import Cart
from .models import Product, Category, ProductImage
from .forms import ProductForm, ProductImageForm

    return render(request, "products/all.html", context)

def add_product(request):
    
    form = ProductForm(request.POST or None)
        
    if form.is_valid():
        product = form.save(commit=False)
        product.user = request.user
        product.slug = slugify(form.cleaned_data['title'])
        product.active = False
        product.save()
        return HttpResponseRedirect('/products/%s'%(product.slug))
    
    context = {
        "form": form,
    }

    return render(request, "products/edit.html",context)


def manage_product_image(request, slug):
    try:
        product = Product.objects.get(slug=slug)
    except:
        product = False

    
    if request.user == product.user:
        queryset = ProductImage.objects.filter(product__slug=slug)

        ProductImageFormset = modelformset_factory(ProductImage, form=ProductImageForm, extra=0, can_delete=True)