first push
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
from simple_history.admin import SimpleHistoryAdmin
|
||||
from .models import *
|
||||
# Register your models here.
|
||||
|
||||
|
||||
@admin.register(Product)
|
||||
class SupportAdmin(SimpleHistoryAdmin):
|
||||
list_display = ['name', 'organization', 'created_at']
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ProductsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'products'
|
||||
@@ -0,0 +1,49 @@
|
||||
from dataclasses import fields
|
||||
from django import forms
|
||||
from .models import *
|
||||
from dashboard.models import ProductCategory, ProductType
|
||||
|
||||
|
||||
class AddProductsForm(forms.ModelForm):
|
||||
|
||||
name = forms.CharField(required=True, widget=forms.TextInput(
|
||||
attrs={'autocomplete': 'off',
|
||||
'placeholder': 'Product Name', 'class': 'form-control'}
|
||||
))
|
||||
product_picture = forms.ImageField(required=False, widget=forms.FileInput(
|
||||
attrs={'class': 'form-control d-flex', 'id': 'inputFile'}
|
||||
))
|
||||
manufacturer = forms.CharField(required=True, widget=forms.TextInput(
|
||||
attrs={'autocomplete': 'off',
|
||||
'placeholder': 'Manufacturer', 'class': 'form-control'}
|
||||
))
|
||||
description = forms.CharField(required=False, widget=forms.Textarea(
|
||||
attrs={'autocomplete': 'off', 'placeholder': 'Description',
|
||||
'class': 'form-control', 'rows': '2'}
|
||||
))
|
||||
product_category = forms.ModelChoiceField(
|
||||
queryset=None,
|
||||
empty_label='--SELECT--',
|
||||
widget=forms.Select(
|
||||
attrs={'class': 'form-control'}
|
||||
))
|
||||
|
||||
product_type = forms.ModelChoiceField(
|
||||
queryset=None,
|
||||
empty_label='--SELECT--',
|
||||
widget=forms.Select(
|
||||
attrs={'class': 'form-control'}
|
||||
))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._organization = kwargs.pop('organization', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['product_category'].queryset = ProductCategory.undeleted_objects.filter(
|
||||
organization=self._organization, status=True)
|
||||
self.fields['product_type'].queryset = ProductType.undeleted_objects.filter(
|
||||
organization=self._organization, status=True)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ['name', 'product_picture', 'manufacturer',
|
||||
'description', 'product_category', 'product_type']
|
||||
@@ -0,0 +1,37 @@
|
||||
# Generated by Django 3.2 on 2022-05-27 06:01
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('dashboard', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Product',
|
||||
fields=[
|
||||
('status', models.IntegerField(default=1)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('created_by', models.IntegerField()),
|
||||
('updated_by', models.IntegerField()),
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('name', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('manufacturer', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('description', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='dashboard.organization')),
|
||||
('product_category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='dashboard.productcategory')),
|
||||
('product_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='dashboard.producttype')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 3.2 on 2022-06-21 13:50
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='created_by',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='updated_by',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-07-03 18:43
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0002_auto_20220621_1350'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='deleted',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-09-22 10:05
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0003_product_deleted'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='product_pic',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='product_pic'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-09-22 11:16
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0004_product_product_pic'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='product_pic',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='product_pics'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-09-26 06:55
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0005_alter_product_product_pic'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='product',
|
||||
old_name='product_pic',
|
||||
new_name='product_picture',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 3.2 on 2022-09-27 06:55
|
||||
|
||||
from django.db import migrations, models
|
||||
import products.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0006_rename_product_pic_product_product_picture'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='product_picture',
|
||||
field=models.ImageField(blank=True, null=True, upload_to=products.models.path_and_rename),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-09-28 09:51
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0007_alter_product_product_picture'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='product',
|
||||
old_name='deleted',
|
||||
new_name='is_deleted',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-09-29 07:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0008_rename_deleted_product_is_deleted'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='deleted',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 3.2 on 2022-09-29 07:49
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0009_product_deleted'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='product',
|
||||
name='deleted',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2022-11-29 05:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0010_remove_product_deleted'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='description',
|
||||
field=models.TextField(blank=True, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 3.2 on 2022-12-13 13:08
|
||||
|
||||
from django.db import migrations
|
||||
import django_resized.forms
|
||||
import products.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0011_alter_product_description'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='product_picture',
|
||||
field=django_resized.forms.ResizedImageField(blank=True, crop=None, force_format='JPEG', keep_meta=True, null=True, quality=75, scale=None, size=[1024, 768], upload_to=products.models.path_and_rename),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2023-01-04 09:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0012_alter_product_product_picture'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='product',
|
||||
name='status',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,50 @@
|
||||
# Generated by Django 3.2 on 2023-01-04 14:16
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import simple_history.models
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dashboard', '0018_auto_20230104_1455'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('products', '0013_alter_product_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='HistoricalProduct',
|
||||
fields=[
|
||||
('status', models.BooleanField(default=True)),
|
||||
('created_at', models.DateTimeField(blank=True, editable=False)),
|
||||
('updated_at', models.DateTimeField(blank=True, editable=False)),
|
||||
('created_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('updated_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('is_deleted', models.BooleanField(default=False)),
|
||||
('id', models.UUIDField(db_index=True, default=uuid.uuid4, editable=False)),
|
||||
('name', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('product_picture', models.TextField(blank=True, max_length=100, null=True)),
|
||||
('manufacturer', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('description', models.TextField(blank=True, null=True)),
|
||||
('history_id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('history_date', models.DateTimeField(db_index=True)),
|
||||
('history_change_reason', models.CharField(max_length=100, null=True)),
|
||||
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
|
||||
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
|
||||
('organization', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='dashboard.organization')),
|
||||
('product_category', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='dashboard.productcategory')),
|
||||
('product_type', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='dashboard.producttype')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'historical product',
|
||||
'verbose_name_plural': 'historical products',
|
||||
'ordering': ('-history_date', '-history_id'),
|
||||
'get_latest_by': ('history_date', 'history_id'),
|
||||
},
|
||||
bases=(simple_history.models.HistoricalChanges, models.Model),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
import uuid
|
||||
from django.db import models
|
||||
from dashboard.models import TimeStampModel, Organization, ProductCategory, ProductType, SoftDeleteModel
|
||||
import os
|
||||
from uuid import uuid4
|
||||
from django_resized import ResizedImageField
|
||||
from simple_history.models import HistoricalRecords
|
||||
|
||||
# Create your models here.
|
||||
|
||||
def path_and_rename(instance, filename):
|
||||
upload_to = 'product/'
|
||||
ext = filename.split('.')[-1]
|
||||
if instance.pk:
|
||||
filename = '{}.{}'.format(instance.pk, ext)
|
||||
else:
|
||||
filename = '{}.{}'.format(uuid4().hex, ext)
|
||||
return os.path.join(upload_to, filename)
|
||||
|
||||
|
||||
class Product(TimeStampModel, SoftDeleteModel):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
name = models.CharField(max_length=255, blank=True, null=True)
|
||||
product_picture = ResizedImageField(upload_to=path_and_rename, blank=True, null=True)
|
||||
manufacturer = models.CharField(max_length=255, blank=True, null=True)
|
||||
description = models.TextField(blank=True, null=True)
|
||||
product_category = models.ForeignKey(ProductCategory, models.DO_NOTHING, blank=True, null=True)
|
||||
product_type = models.ForeignKey(ProductType, models.DO_NOTHING, blank=True, null=True)
|
||||
organization = models.ForeignKey(Organization, models.DO_NOTHING, blank=True, null=True)
|
||||
history = HistoricalRecords()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -0,0 +1,130 @@
|
||||
from django.test import TestCase
|
||||
from .models import Product
|
||||
from dashboard.models import ProductCategory,ProductType,Organization
|
||||
# Create your tests here.
|
||||
|
||||
class Asseto_test(TestCase):
|
||||
def setUp(self):
|
||||
self.user_organization = Organization.objects.create(
|
||||
name="Organization 1",
|
||||
website="asseto.com",
|
||||
phone="1234567890",
|
||||
email="asseto@asseto.com",
|
||||
currency="INR",
|
||||
date_format="dd-mm-yyyy",
|
||||
logo = "logo.png"
|
||||
)
|
||||
self.product_category=ProductCategory.objects.create(
|
||||
name="Category 1",
|
||||
organization=self.user_organization,
|
||||
)
|
||||
self.product_type=ProductType.objects.create(
|
||||
name="Type 1",
|
||||
organization=self.user_organization,
|
||||
)
|
||||
self.product=Product.objects.create(
|
||||
name = "Product 1",
|
||||
product_picture = "test.jpg",
|
||||
manufacturer = "Manufacturer 1",
|
||||
description = "Description 1",
|
||||
product_category = self.product_category,
|
||||
product_type = self.product_type,
|
||||
organization = self.user_organization
|
||||
)
|
||||
self.updated_product_type=ProductType.objects.create(
|
||||
name="Type 2",
|
||||
organization=self.user_organization,
|
||||
)
|
||||
self.updated_product_category=ProductCategory.objects.create(
|
||||
name="Category 2",
|
||||
organization=self.user_organization,
|
||||
)
|
||||
self.updated_product=Product.objects.create(
|
||||
name = "Product 2",
|
||||
product_picture = "test2.jpg",
|
||||
manufacturer = "Manufacturer 2",
|
||||
description = "Description 2",
|
||||
product_category = self.updated_product_category,
|
||||
product_type = self.updated_product_type,
|
||||
organization = self.user_organization
|
||||
)
|
||||
|
||||
def test_product(self):
|
||||
print("Product Testing Start")
|
||||
# self.assertEqual(product.name, "Vendor 1")
|
||||
# self.assertEqual(address.email, "vendor1@asseto.com")
|
||||
self.assertEqual(self.product.product_picture,"test.jpg")
|
||||
self.assertEqual(self.product.manufacturer,"Manufacturer 1")
|
||||
self.assertEqual(self.product.description, "Description 1")
|
||||
self.assertEqual(self.product.product_category.name, "Category 1")
|
||||
self.assertEqual(self.product.product_type.name, "Type 1")
|
||||
self.assertEqual(self.product.organization.name, "Organization 1")
|
||||
# self.assertEqual(self.product.product_category.name, " ")
|
||||
# self.assertEqual(self.product.product_type.name, "Type 1")
|
||||
# self.assertEqual(self.product.product_type.name, "Type 1")
|
||||
# self.assertEqual(self.product.product_type.name, "Type 1")
|
||||
# self.assertEqual(self.product.product_type.name, "Type 1")
|
||||
print("Product Testing Successful\n")
|
||||
# class DeleteProduct(TestCase):
|
||||
# def setUp(self):
|
||||
# user_organization = Organization.objects.create(
|
||||
# name="Organization 1",
|
||||
# website="asseto.com",
|
||||
# phone="1234567890",
|
||||
# email="asseto@asseto.com",
|
||||
# currency="INR",
|
||||
# date_format="dd-mm-yyyy",
|
||||
# logo = "logo.png"
|
||||
# )
|
||||
# product_category=ProductCategory.objects.create(
|
||||
# name="Category 1",
|
||||
# organization=user_organization,
|
||||
# )
|
||||
# product_type=ProductType.objects.create(
|
||||
# name="Type 1",
|
||||
# organization=user_organization,
|
||||
# )
|
||||
# self.product=Product.objects.create(
|
||||
# name = "Product 1",
|
||||
# product_picture = "test.jpg",
|
||||
# manufacturer = "Manufacturer 1",
|
||||
# description = "Description 1",
|
||||
# product_category = product_category,
|
||||
# product_type = product_type,
|
||||
# organization = user_organization
|
||||
# )
|
||||
# self.product.save()
|
||||
# print("Delete setup")
|
||||
def test_edit_porduct(self):
|
||||
print("Product Edit Testing Start")
|
||||
self.product.name='Product 2'
|
||||
self.product.product_picture='test2.jpg'
|
||||
self.product.manufacturer="Manufacturer 2"
|
||||
self.product.description="Description 2"
|
||||
self.product.product_category.name="Category 2"
|
||||
self.product.product_type.name="Type 2"
|
||||
|
||||
self.product.save()
|
||||
|
||||
self.assertEqual(self.product.name,'Product 2')
|
||||
self.assertEqual(self.product.product_picture,'test2.jpg')
|
||||
self.assertEqual(self.product.manufacturer,"Manufacturer 2")
|
||||
self.assertEqual(self.product.description,"Description 2")
|
||||
self.assertEqual(self.product.product_category.name,"Category 2")
|
||||
self.assertEqual(self.product.product_type.name,"Type 2")
|
||||
print("Product Edit Testing Successful\n")
|
||||
|
||||
def del_product(self):
|
||||
print("Product Delete Testing Start")
|
||||
self.product.delete()
|
||||
|
||||
with self.assertRaises(Product.DoesNotExist):
|
||||
Product.objects.get(id=self.product.id)
|
||||
|
||||
product_category=ProductCategory.objects.get(id=self.product.product_category.id)
|
||||
product_type=ProductType.objects.get(id=self.product.product_type.id)
|
||||
organization = Organization.objects.get(id=self.product.organization.id)
|
||||
self.assertIsNotNone(product_category)
|
||||
self.assertIsNotNone(product_type)
|
||||
self.assertIsNotNone(organization)
|
||||
print("Product Delete Testing Successful\n")
|
||||
@@ -0,0 +1,16 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = 'products'
|
||||
|
||||
urlpatterns = [
|
||||
path('list', views.list, name='list'),
|
||||
path('add', views.add_product, name='add'),
|
||||
path('details/<uuid:id>', views.details_product, name='details_product'),
|
||||
path('delete/<uuid:id>', views.delete_product, name='delete_product'),
|
||||
path('update/<uuid:id>', views.update_product, name='update_product'),
|
||||
path('status/<uuid:id>', views.status, name='status'),
|
||||
path('search/<str:page>', views.search, name='search'),
|
||||
path('csv', views.export_products_csv, name='export_products_csv'),
|
||||
path('pdf', views.export_products_pdf, name='export_products_pdf'),
|
||||
]
|
||||
@@ -0,0 +1,189 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from .forms import AddProductsForm
|
||||
from django.contrib import messages
|
||||
from .models import Product, ProductCategory, ProductType
|
||||
from django.core.paginator import Paginator
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from vendors.utils import render_to_csv, render_to_pdf
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
from datetime import date
|
||||
today = date.today()
|
||||
|
||||
PAGE_SIZE = 10
|
||||
ORPHANS = 1
|
||||
|
||||
|
||||
def check_admin(user):
|
||||
return user.is_superuser
|
||||
|
||||
|
||||
def manage_access(user):
|
||||
permissions_list = [
|
||||
'authentication.view_product',
|
||||
'authentication.delete_product',
|
||||
'authentication.edit_product',
|
||||
'authentication.add_product',
|
||||
]
|
||||
|
||||
for permission in permissions_list:
|
||||
if user.has_perm(permission):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@login_required
|
||||
@user_passes_test(manage_access)
|
||||
def list(request):
|
||||
|
||||
product_list = Product.undeleted_objects.filter(
|
||||
organization=request.user.organization).order_by('-created_at')
|
||||
paginator = Paginator(product_list, PAGE_SIZE, orphans=ORPHANS)
|
||||
page_number = request.GET.get('page')
|
||||
page_object = paginator.get_page(page_number)
|
||||
|
||||
context = {
|
||||
'sidebar': 'products',
|
||||
'page_object': page_object,
|
||||
'title': 'Products'
|
||||
}
|
||||
|
||||
return render(request, 'products/list.html', context=context)
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.view_product')
|
||||
def details_product(request, id):
|
||||
|
||||
product = get_object_or_404(
|
||||
Product.undeleted_objects, pk=id, organization=request.user.organization)
|
||||
|
||||
history_list = product.history.all()
|
||||
paginator = Paginator(history_list, 5, orphans=1)
|
||||
page_number = request.GET.get('page')
|
||||
page_object = paginator.get_page(page_number)
|
||||
|
||||
context = {
|
||||
'sidebar': 'products',
|
||||
'product': product,
|
||||
'title': 'Product - Details',
|
||||
'page_object': page_object
|
||||
}
|
||||
|
||||
return render(request, 'products/detail.html', context=context)
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.add_product')
|
||||
def add_product(request):
|
||||
form = AddProductsForm(organization=request.user.organization)
|
||||
|
||||
if request.method == "POST":
|
||||
form = AddProductsForm(
|
||||
request.POST, request.FILES, organization=request.user.organization)
|
||||
|
||||
if form.is_valid():
|
||||
product = form.save(commit=False)
|
||||
product.organization = request.user.organization
|
||||
product.save()
|
||||
messages.success(request, 'Product added successfully')
|
||||
return HttpResponse(status=204)
|
||||
|
||||
context = {'form': form}
|
||||
return render(request, 'products/add-product-modal.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.delete_product')
|
||||
def delete_product(request, id):
|
||||
|
||||
if request.method == 'POST':
|
||||
product = get_object_or_404(
|
||||
Product.undeleted_objects, pk=id, organization=request.user.organization)
|
||||
product.status = False
|
||||
product.soft_delete()
|
||||
history_id = product.history.first().history_id
|
||||
product.history.filter(pk=history_id).update(history_type='-')
|
||||
messages.success(request, 'Product deleted successfully')
|
||||
|
||||
return redirect(request.META.get('HTTP_REFERER'))
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.edit_product')
|
||||
def update_product(request, id):
|
||||
|
||||
product = get_object_or_404(
|
||||
Product.undeleted_objects, pk=id, organization=request.user.organization)
|
||||
form = AddProductsForm(
|
||||
instance=product, organization=request.user.organization)
|
||||
|
||||
if request.method == "POST":
|
||||
form = AddProductsForm(request.POST, request.FILES,
|
||||
instance=product, organization=request.user.organization)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'Product updated successfully')
|
||||
return HttpResponse(status=204)
|
||||
|
||||
context = {'form': form, 'product': product}
|
||||
return render(request, 'products/update-product-modal.html', context)
|
||||
|
||||
|
||||
@user_passes_test(check_admin)
|
||||
def status(request, id):
|
||||
if request.method == "POST":
|
||||
product = get_object_or_404(
|
||||
Product.undeleted_objects, pk=id, organization=request.user.organization)
|
||||
product.status = False if product.status else True
|
||||
product.save()
|
||||
return HttpResponse(status=204)
|
||||
|
||||
|
||||
@login_required
|
||||
def search(request, page):
|
||||
search_text = request.GET.get('search_text').strip()
|
||||
if search_text:
|
||||
return render(request, 'products/products-data.html', {
|
||||
'page_object': Product.undeleted_objects.filter(Q(organization=request.user.organization) & (Q(
|
||||
name__icontains=search_text) | Q(manufacturer__icontains=search_text) | Q(product_category__name__icontains=search_text) | Q(product_type__name__icontains=search_text)
|
||||
)).order_by('-created_at')[:10]
|
||||
})
|
||||
|
||||
product_list = Product.undeleted_objects.filter(
|
||||
organization=request.user.organization).order_by('-created_at')
|
||||
paginator = Paginator(product_list, PAGE_SIZE, orphans=ORPHANS)
|
||||
page_number = page
|
||||
page_object = paginator.get_page(page_number)
|
||||
return render(request, 'products/products-data.html', {'page_object': page_object})
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.view_product')
|
||||
def export_products_csv(request):
|
||||
header_list = ['Product Name', 'Product Category',
|
||||
'Product Type', 'Manufacturer', 'Description']
|
||||
product_list = Product.undeleted_objects.filter(organization=request.user.organization).order_by(
|
||||
'-created_at').values_list('name', 'product_category__name', 'product_type__name', 'manufacturer', 'description')
|
||||
context = {'header_list': header_list, 'rows': product_list}
|
||||
response = render_to_csv(context_dict=context)
|
||||
response['Content-Disposition'] = f'attachment; filename="export-products-{today}.csv"'
|
||||
return response
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('authentication.view_product')
|
||||
def export_products_pdf(request):
|
||||
products = Product.undeleted_objects.filter(
|
||||
organization=request.user.organization).order_by('-created_at')
|
||||
context = {'products': products}
|
||||
pdf = render_to_pdf('products/products-pdf.html', context_dict=context)
|
||||
response = HttpResponse(pdf, content_type='application/pdf')
|
||||
response['Content-Disposition'] = f'attachment; filename="export-products-{today}.pdf"'
|
||||
return response
|
||||
Reference in New Issue
Block a user