Skip to content

Commit ce706ee

Browse files
committed
groups and permissions
1 parent c539e2c commit ce706ee

File tree

16 files changed

+361
-31
lines changed

16 files changed

+361
-31
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ media
2121
static
2222
static_cdn
2323
.env
24-
data.json
24+
data.json
25+
backends.py
26+
testsocket.py

firstapp/admin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Register your models here.
66

7-
from .models import Cart, Product, ProductInCart, Order, ProductInOrder, Deal, Customer, Seller, Contact, SellerAdditional, OtpModel
7+
from .models import Cart, Product, ProductInCart, Order, ProductInOrder, Deal, Customer, Seller, Contact, SellerAdditional, OtpModel, PremiumProduct
88

99
from django.contrib import admin
1010
from django.contrib.auth.admin import UserAdmin
@@ -23,7 +23,7 @@ class CustomUserAdmin(UserAdmin):
2323
list_filter = ('email', 'is_staff', 'is_active',)
2424
fieldsets = (
2525
(None, {'fields': ('email', 'phone', 'name','type', 'password')}),
26-
('Permissions', {'fields': ('is_staff', 'is_active')}), #'is_customer' , 'is_seller'
26+
('Permissions', {'fields': ('is_staff', 'is_active', 'is_superuser', 'groups', 'user_permissions',)}), #'is_customer' , 'is_seller'
2727
)
2828
add_fieldsets = (
2929
(None, {
@@ -220,3 +220,4 @@ def _session_data(self, obj):
220220

221221

222222
admin.site.register(OtpModel)
223+
admin.site.register(PremiumProduct)

firstapp/decorators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import six
2+
from django.core.exceptions import PermissionDenied
3+
from django.contrib.auth.decorators import user_passes_test
4+
5+
def group_required(group, login_url=None, raise_exception=False):
6+
def check_perms(user):
7+
if isinstance(group, six.string_types):
8+
groups = (group, )
9+
else:
10+
groups = group
11+
# First check if the user has the permission (even anon users)
12+
13+
if user.groups.filter(name__in=groups).exists():
14+
return True
15+
# In case the 403 handler should be called raise the exception
16+
if raise_exception:
17+
raise PermissionDenied
18+
# As the last resort, show the login form
19+
return False
20+
return user_passes_test(check_perms, login_url=login_url)

firstapp/forms.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ class Meta:
9090
fields = [
9191
'quantity'
9292
]
93-
94-
95-
96-
9793

9894

9995

firstapp/mixins.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.core.exceptions import PermissionDenied
2+
class CheckPremiumGroupMixin:
3+
def dispatch(self, request, *args, **kwargs):
4+
if request.user.groups.filter(name = "premium").exists():
5+
#return True
6+
return super().dispatch(request, *args, **kwargs)
7+
8+
else:
9+
raise PermissionDenied

firstapp/models.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ class Product(models.Model):
170170
brand = models.CharField(max_length=1000)
171171
date_added = models.DateTimeField(default=timezone.now)
172172

173-
class Meta:
174-
ordering = ['-price'] # default ordering whenever you query to database retrieval in order as stored in DB ---> ordering ---> returned as a queryset where called
173+
#class Meta:
174+
#ordering = ['-price'] # default ordering whenever you query to database retrieval in order as stored in DB ---> ordering ---> returned as a queryset where called
175175

176176
@classmethod
177177
def updateprice(cls,product_id, price):
@@ -292,3 +292,80 @@ class OtpModel(models.Model):
292292
# class Subcategory(models.Model):
293293
# subcategory_name = models.CharField(max_length=1000)
294294
# category = models.ForeignKey(Category, on_delete = models.CASCADE)
295+
296+
297+
298+
class PremiumProduct(models.Model):
299+
product_name = models.CharField(max_length=15)
300+
image = models.ImageField(upload_to = "firstapp/premiumproductimages", default = None, null = True, blank = True)
301+
price = models.FloatField()
302+
brand = models.CharField(max_length=1000)
303+
date_added = models.DateTimeField(default=timezone.now)
304+
305+
# # custom permissions dependent to a specific model
306+
class Meta:
307+
permissions = (
308+
('can_avail_premium_delivery', 'can avail for premium delivery on premium products'),
309+
('can_add_premium_discount', 'can avail more premium discount on premium products')
310+
)
311+
312+
313+
314+
class CustomPermissions(models.Model):
315+
316+
class Meta:
317+
318+
managed = False # No database table creation or deletion \
319+
# operations will be performed for this model.
320+
321+
default_permissions = () # disable "add", "change", "delete"
322+
# and "view" default permissions
323+
324+
# All the custom permissions not related to models on Manufacturer
325+
permissions = (
326+
('accept_order', 'can accept order'),
327+
('reject_order', 'can reject order'),
328+
('view_order', 'can view order'),
329+
('change_order', 'can change order'),
330+
('view_return', 'can view return'),
331+
('accept_return', 'can accept return'),
332+
('reject_return', 'can reject return'),
333+
('change_return', 'can change return'),
334+
('view_dashboard', 'can view dashboard'),
335+
)
336+
337+
338+
339+
340+
341+
342+
343+
344+
345+
346+
347+
348+
349+
350+
351+
352+
353+
354+
355+
356+
357+
358+
359+
360+
361+
362+
363+
364+
365+
366+
367+
368+
369+
370+
371+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{% extends 'firstapp/basic.html' %}
2+
{% load hosts %}
3+
{% load static %}
4+
{% load myfilters %}
5+
{% block title %}
6+
{% endblock %}
7+
{% block css %}
8+
<style>
9+
.main-area {
10+
padding-left: 20px;
11+
padding-right: 20px;
12+
padding-top: 0px;
13+
background-color: #f8f9fa;
14+
}
15+
16+
.product-cards {
17+
background-color: white;
18+
padding: 10px;
19+
}
20+
21+
.btn-primary {
22+
margin: 10px;
23+
}
24+
25+
.pagination {
26+
justify-content: center;
27+
}
28+
29+
.utility {
30+
display: flex;
31+
}
32+
</style>
33+
{% endblock %}
34+
35+
{% block body%}
36+
<div class="main-area">
37+
<div class="product-cards">
38+
<div class="utility">
39+
40+
41+
42+
43+
</div>
44+
<br>
45+
<div class="row row-cols-1 row-cols-md-4 g-4">
46+
{% for i in product %}
47+
<div class="col">
48+
<div class="card h-100">
49+
<img src="/media/{{i.image}}" class="card-img-top" alt="...">
50+
<div class="card-body">
51+
<h5 class="card-title"><a href="#">{{i.product_name}}</a></h5>
52+
<p class="card-text">{{i.price}}</p>
53+
</div>
54+
<a class="btn btn-primary" href="#">Add To Cart</a>
55+
</div>
56+
</div>
57+
{% endfor %}
58+
</div>
59+
60+
61+
62+
63+
<br>
64+
</div>
65+
66+
67+
68+
</div>
69+
{% endblock %}
70+
71+
{% block js %}
72+
{% endblock %}

firstapp/templates/firstapp/listproducts.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@
7272
<div class="card h-100">
7373
<img src="/media/{{i.image}}" class="card-img-top" alt="...">
7474
<div class="card-body">
75-
<h5 class="card-title"><a
76-
href="{% host_url 'productdetail' pk=i.product_id host 'www' %}">{{i.product_name}}</a>
77-
</h5>
75+
<h5 class="card-title"><a href="{% host_url 'productdetail' pk=i.product_id host 'www' %}">{{i.product_name}}</a></h5>
7876
<p class="card-text">{{i.price}}</p>
7977
</div>
8078
<a class="btn btn-primary" href="{% url 'addtocart' id=i.product_id %}">Add To Cart</a>

firstapp/templates/firstapp/payment/emailinvoice.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{% autoescape off %}
1+
<div style="background-color: rgb(228, 228, 228);">
2+
<div style="margin: 20px; background-color: #fff;">
3+
24
Hi {{ user.name }},
35
Your order with order id: {{order.order_id}} has been placed successfully.
46

@@ -16,4 +18,6 @@
1618

1719
Sincerely,
1820
WonderShop Team
19-
{% endautoescape %}
21+
22+
</div>
23+
</div>

firstapp/urls.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
path('contactus/', views.contactus2, name="contact"),
1515
path('contactusclass/', views.ContactUs.as_view(), name="contactclass"),
1616
path('testsessions/', views.testsessions, name="testsessions"),
17-
#path('firstasyncview/', views.firstasyncview, name="firstasyncview"),
17+
path('firstasyncview/', views.firstasyncview, name="firstasyncview"),
1818

1919
#path('listproducts/', views.ListProducts.as_view(), name="listproducts"),
2020
path('listproducts/', views.listProducts, name="listproducts"),
@@ -24,6 +24,11 @@
2424
path('updatecart/<int:pk>/', views.UpdateCart.as_view(), name="updatecart"),
2525
path('deletefromcart/<int:pk>/', views.DeleteFromCart.as_view(), name="deletefromcart"),
2626

27+
28+
path('addtopremium/', views.addToPremiumGroup, name="addtopremium"),
29+
#path('premiumproducts/', views.premiumProducts, name="premiumproducts"),
30+
path('premiumproducts/', views.PremiumProducts.as_view(), name="premiumproducts"),
31+
2732
path('api/listproductsapi/', views.listProductsApi, name="listproductsapi"),
2833
path('api/suggestionapi/', views.suggestionApi, name="suggestionapi"),
2934

@@ -63,9 +68,9 @@
6368

6469

6570
# when debug=True
66-
if settings.DEBUG:
67-
urlpatterns += static(settings.STATIC_URL ,document_root = settings.STATIC_ROOT)
68-
urlpatterns += static(settings.MEDIA_URL ,document_root = settings.MEDIA_ROOT)
71+
#if settings.DEBUG:
72+
#urlpatterns += static(settings.STATIC_URL ,document_root = settings.STATIC_ROOT)
73+
urlpatterns += static(settings.MEDIA_URL ,document_root = settings.MEDIA_ROOT)
6974

7075

7176

0 commit comments

Comments
 (0)