1
1
from django .shortcuts import render
2
2
from django .contrib .auth .decorators import login_required
3
3
from django .views .generic import ListView ,CreateView ,UpdateView ,DetailView ,View
4
- from DjangoEcommerceApp .models import Categories ,SubCategories ,CustomUser ,MerchantUser ,Products ,ProductAbout ,ProductDetails ,ProductMedia ,ProductTransaction ,ProductTags
4
+ from DjangoEcommerceApp .models import Categories ,SubCategories ,CustomUser ,MerchantUser ,Products ,ProductAbout ,ProductDetails ,ProductMedia ,ProductTransaction ,ProductTags , StaffUser , CustomerUser
5
5
from django .contrib .messages .views import SuccessMessageMixin
6
6
from django .core .files .storage import FileSystemStorage
7
7
from django .contrib .messages .views import messages
@@ -284,3 +284,315 @@ def get_context_data(self,**kwargs):
284
284
context ["orderby" ]= self .request .GET .get ("orderby" ,"id" )
285
285
context ["all_table_fields" ]= Products ._meta .get_fields ()
286
286
return context
287
+
288
+
289
+ class ProductEdit (View ):
290
+
291
+ def get (self ,request ,* args ,** kwargs ):
292
+ product_id = kwargs ["product_id" ]
293
+ product = Products .objects .get (id = product_id )
294
+ product_details = ProductDetails .objects .filter (product_id = product_id )
295
+ product_about = ProductAbout .objects .filter (product_id = product_id )
296
+ product_tags = ProductTags .objects .filter (product_id = product_id )
297
+
298
+ categories = Categories .objects .filter (is_active = 1 )
299
+ categories_list = []
300
+ for category in categories :
301
+ sub_category = SubCategories .objects .filter (is_active = 1 ,category_id = category .id )
302
+ categories_list .append ({"category" :category ,"sub_category" :sub_category })
303
+
304
+ return render (request ,"admin_templates/product_edit.html" ,{"categories" :categories_list ,"product" :product ,"product_details" :product_details ,"product_about" :product_about ,"product_tags" :product_tags })
305
+
306
+ def post (self ,request ,* args ,** kwargs ):
307
+
308
+ product_name = request .POST .get ("product_name" )
309
+ brand = request .POST .get ("brand" )
310
+ url_slug = request .POST .get ("url_slug" )
311
+ sub_category = request .POST .get ("sub_category" )
312
+ product_max_price = request .POST .get ("product_max_price" )
313
+ product_discount_price = request .POST .get ("product_discount_price" )
314
+ product_description = request .POST .get ("product_description" )
315
+ title_title_list = request .POST .getlist ("title_title[]" )
316
+ details_ids = request .POST .getlist ("details_id[]" )
317
+ title_details_list = request .POST .getlist ("title_details[]" )
318
+ about_title_list = request .POST .getlist ("about_title[]" )
319
+ about_ids = request .POST .getlist ("about_id[]" )
320
+ product_tags = request .POST .get ("product_tags" )
321
+ long_desc = request .POST .get ("long_desc" )
322
+ subcat_obj = SubCategories .objects .get (id = sub_category )
323
+
324
+ product_id = kwargs ["product_id" ]
325
+ product = Products .objects .get (id = product_id )
326
+ product .product_name = product_name
327
+ product .url_slug = url_slug
328
+ product .brand = brand
329
+ product .subcategories_id = subcat_obj
330
+ product .product_description = product_description
331
+ product .product_max_price = product_max_price
332
+ product .product_discount_price = product_discount_price
333
+ product .product_long_description = long_desc
334
+ product .save ()
335
+
336
+
337
+ j = 0
338
+ for title_title in title_title_list :
339
+ detail_id = details_ids [j ]
340
+ if detail_id == "blank" and title_title != "" :
341
+ product_details = ProductDetails (title = title_title ,title_details = title_details_list [j ],product_id = product )
342
+ product_details .save ()
343
+ else :
344
+ if title_title != "" :
345
+ product_details = ProductDetails .objects .get (id = detail_id )
346
+ product_details .title = title_title
347
+ product_details .title_details = title_details_list [j ]
348
+ product_details .product_id = product
349
+ product_details .save ()
350
+ j = j + 1
351
+
352
+
353
+ k = 0
354
+ for about in about_title_list :
355
+ about_id = about_ids [k ]
356
+ if about_id == "blank" and about != "" :
357
+ product_about = ProductAbout (title = about ,product_id = product )
358
+ product_about .save ()
359
+ else :
360
+ if about != "" :
361
+ product_about = ProductAbout .objects .get (id = about_id )
362
+ product_about .title = about
363
+ product_about .product_id = product
364
+ product_about .save ()
365
+ k = k + 1
366
+
367
+ ProductTags .objects .filter (product_id = product_id ).delete ()
368
+
369
+ product_tags_list = product_tags .split ("," )
370
+
371
+ for product_tag in product_tags_list :
372
+ product_tag_obj = ProductTags (product_id = product ,title = product_tag )
373
+ product_tag_obj .save ()
374
+
375
+ return HttpResponse ("OK" )
376
+
377
+ class ProductAddMedia (View ):
378
+ def get (self ,request ,* args ,** kwargs ):
379
+ product_id = kwargs ["product_id" ]
380
+ product = Products .objects .get (id = product_id )
381
+ return render (request ,"admin_templates/product_add_media.html" ,{"product" :product })
382
+
383
+ def post (self ,request ,* args ,** kwargs ):
384
+ product_id = kwargs ["product_id" ]
385
+ product = Products .objects .get (id = product_id )
386
+ media_type_list = request .POST .getlist ("media_type[]" )
387
+ media_content_list = request .FILES .getlist ("media_content[]" )
388
+
389
+ i = 0
390
+ for media_content in media_content_list :
391
+ fs = FileSystemStorage ()
392
+ filename = fs .save (media_content .name ,media_content )
393
+ media_url = fs .url (filename )
394
+ product_media = ProductMedia (product_id = product ,media_type = media_type_list [i ],media_content = media_url )
395
+ product_media .save ()
396
+ i = i + 1
397
+
398
+ return HttpResponse ("OK" )
399
+
400
+ class ProductEditMedia (View ):
401
+ def get (self ,request ,* args ,** kwargs ):
402
+ product_id = kwargs ["product_id" ]
403
+ product = Products .objects .get (id = product_id )
404
+ product_medias = ProductMedia .objects .filter (product_id = product_id )
405
+ return render (request ,"admin_templates/product_edit_media.html" ,{"product" :product ,"product_medias" :product_medias })
406
+
407
+ class ProductMediaDelete (View ):
408
+ def get (self ,request ,* args ,** kwargs ):
409
+ media_id = kwargs ["id" ]
410
+ product_media = ProductMedia .objects .get (id = media_id )
411
+ import os
412
+ from DjangoEcommerce import settings
413
+
414
+ #It will work too Sometimes
415
+ #product_media.media_content.delete()
416
+ os .remove (settings .MEDIA_ROOT .replace ("\media" ,"" )+ str (product_media .media_content ).replace ("/" ,"\\ " ))
417
+
418
+ product_id = product_media .product_id .id
419
+ product_media .delete ()
420
+ return HttpResponseRedirect (reverse ("product_edit_media" ,kwargs = {"product_id" :product_id }))
421
+
422
+ class ProductAddStocks (View ):
423
+ def get (self ,request ,* args ,** kwargs ):
424
+ product_id = kwargs ["product_id" ]
425
+ product = Products .objects .get (id = product_id )
426
+ return render (request ,"admin_templates/product_add_stocks.html" ,{"product" :product })
427
+
428
+ def post (self ,request ,* args ,** kwargs ):
429
+ product_id = kwargs ["product_id" ]
430
+ new_instock = request .POST .get ("add_stocks" )
431
+ product = Products .objects .get (id = product_id )
432
+ old_stocks = product .in_stock_total
433
+ new_stocks = int (new_instock )+ int (old_stocks )
434
+ product .in_stock_total = new_stocks
435
+ product .save ()
436
+
437
+ product_obj = Products .objects .get (id = product_id )
438
+ product_transaction = ProductTransaction (product_id = product_obj ,transaction_product_count = new_instock ,transaction_description = "New Product Added" ,transaction_type = 1 )
439
+ product_transaction .save ()
440
+ return HttpResponseRedirect (reverse ("product_add_stocks" ,kwargs = {"product_id" :product_id }))
441
+
442
+
443
+ class StaffUserListView (ListView ):
444
+ model = StaffUser
445
+ template_name = "admin_templates/staff_list.html"
446
+ paginate_by = 3
447
+
448
+ def get_queryset (self ):
449
+ filter_val = self .request .GET .get ("filter" ,"" )
450
+ order_by = self .request .GET .get ("orderby" ,"id" )
451
+ if filter_val != "" :
452
+ cat = StaffUser .objects .filter (Q (auth_user_id__first_name__contains = filter_val ) | Q (auth_user_id__last_name__contains = filter_val ) | Q (auth_user_id__email__contains = filter_val ) | Q (auth_user_id__username__contains = filter_val )).order_by (order_by )
453
+ else :
454
+ cat = StaffUser .objects .all ().order_by (order_by )
455
+
456
+ return cat
457
+
458
+ def get_context_data (self ,** kwargs ):
459
+ context = super (StaffUserListView ,self ).get_context_data (** kwargs )
460
+ context ["filter" ]= self .request .GET .get ("filter" ,"" )
461
+ context ["orderby" ]= self .request .GET .get ("orderby" ,"id" )
462
+ context ["all_table_fields" ]= StaffUser ._meta .get_fields ()
463
+ return context
464
+
465
+
466
+ class StaffUserCreateView (SuccessMessageMixin ,CreateView ):
467
+ template_name = "admin_templates/staff_create.html"
468
+ model = CustomUser
469
+ fields = ["first_name" ,"last_name" ,"email" ,"username" ,"password" ]
470
+
471
+ def form_valid (self ,form ):
472
+
473
+ #Saving Custom User Object for Merchant User
474
+ user = form .save (commit = False )
475
+ user .is_active = True
476
+ user .user_type = 2
477
+ user .set_password (form .cleaned_data ["password" ])
478
+ user .save ()
479
+
480
+ #Saving Merchant user
481
+ profile_pic = self .request .FILES ["profile_pic" ]
482
+ fs = FileSystemStorage ()
483
+ filename = fs .save (profile_pic .name ,profile_pic )
484
+ profile_pic_url = fs .url (filename )
485
+
486
+ user .staffuser .profile_pic = profile_pic_url
487
+ user .save ()
488
+ messages .success (self .request ,"Staff User Created" )
489
+ return HttpResponseRedirect (reverse ("staff_list" ))
490
+
491
+ class StaffUserUpdateView (SuccessMessageMixin ,UpdateView ):
492
+ template_name = "admin_templates/staff_update.html"
493
+ model = CustomUser
494
+ fields = ["first_name" ,"last_name" ,"email" ,"username" ]
495
+
496
+ def get_context_data (self ,** kwargs ):
497
+ context = super ().get_context_data (** kwargs )
498
+ staffuser = StaffUser .objects .get (auth_user_id = self .object .pk )
499
+ context ["staffuser" ]= staffuser
500
+ return context
501
+
502
+ def form_valid (self ,form ):
503
+
504
+ #Saving Custom User Object for Merchant User
505
+ user = form .save (commit = False )
506
+ user .save ()
507
+
508
+ #Saving Merchant user
509
+ staffuser = StaffUser .objects .get (auth_user_id = user .id )
510
+ if self .request .FILES .get ("profile_pic" ,False ):
511
+ profile_pic = self .request .FILES ["profile_pic" ]
512
+ fs = FileSystemStorage ()
513
+ filename = fs .save (profile_pic .name ,profile_pic )
514
+ profile_pic_url = fs .url (filename )
515
+ staffuser .profile_pic = profile_pic_url
516
+
517
+ staffuser .save ()
518
+ messages .success (self .request ,"Staff User Updated" )
519
+ return HttpResponseRedirect (reverse ("staff_list" ))
520
+
521
+
522
+ class CustomerUserListView (ListView ):
523
+ model = CustomerUser
524
+ template_name = "admin_templates/customer_list.html"
525
+ paginate_by = 3
526
+
527
+ def get_queryset (self ):
528
+ filter_val = self .request .GET .get ("filter" ,"" )
529
+ order_by = self .request .GET .get ("orderby" ,"id" )
530
+ if filter_val != "" :
531
+ cat = CustomerUser .objects .filter (Q (auth_user_id__first_name__contains = filter_val ) | Q (auth_user_id__last_name__contains = filter_val ) | Q (auth_user_id__email__contains = filter_val ) | Q (auth_user_id__username__contains = filter_val )).order_by (order_by )
532
+ else :
533
+ cat = CustomerUser .objects .all ().order_by (order_by )
534
+
535
+ return cat
536
+
537
+ def get_context_data (self ,** kwargs ):
538
+ context = super (CustomerUserListView ,self ).get_context_data (** kwargs )
539
+ context ["filter" ]= self .request .GET .get ("filter" ,"" )
540
+ context ["orderby" ]= self .request .GET .get ("orderby" ,"id" )
541
+ context ["all_table_fields" ]= CustomerUser ._meta .get_fields ()
542
+ return context
543
+
544
+
545
+ class CustomerUserCreateView (SuccessMessageMixin ,CreateView ):
546
+ template_name = "admin_templates/customer_create.html"
547
+ model = CustomUser
548
+ fields = ["first_name" ,"last_name" ,"email" ,"username" ,"password" ]
549
+
550
+ def form_valid (self ,form ):
551
+
552
+ #Saving Custom User Object for Merchant User
553
+ user = form .save (commit = False )
554
+ user .is_active = True
555
+ user .user_type = 4
556
+ user .set_password (form .cleaned_data ["password" ])
557
+ user .save ()
558
+
559
+ #Saving Merchant user
560
+ profile_pic = self .request .FILES ["profile_pic" ]
561
+ fs = FileSystemStorage ()
562
+ filename = fs .save (profile_pic .name ,profile_pic )
563
+ profile_pic_url = fs .url (filename )
564
+
565
+ user .customeruser .profile_pic = profile_pic_url
566
+ user .save ()
567
+ messages .success (self .request ,"Customer User Created" )
568
+ return HttpResponseRedirect (reverse ("customer_list" ))
569
+
570
+ class CustomerUserUpdateView (SuccessMessageMixin ,UpdateView ):
571
+ template_name = "admin_templates/customer_update.html"
572
+ model = CustomUser
573
+ fields = ["first_name" ,"last_name" ,"email" ,"username" ]
574
+
575
+ def get_context_data (self ,** kwargs ):
576
+ context = super ().get_context_data (** kwargs )
577
+ customeruser = CustomerUser .objects .get (auth_user_id = self .object .pk )
578
+ context ["CustomerUser" ]= customeruser
579
+ return context
580
+
581
+ def form_valid (self ,form ):
582
+
583
+ #Saving Custom User Object for Merchant User
584
+ user = form .save (commit = False )
585
+ user .save ()
586
+
587
+ #Saving Merchant user
588
+ customeruser = CustomerUser .objects .get (auth_user_id = user .id )
589
+ if self .request .FILES .get ("profile_pic" ,False ):
590
+ profile_pic = self .request .FILES ["profile_pic" ]
591
+ fs = FileSystemStorage ()
592
+ filename = fs .save (profile_pic .name ,profile_pic )
593
+ profile_pic_url = fs .url (filename )
594
+ customeruser .profile_pic = profile_pic_url
595
+
596
+ customeruser .save ()
597
+ messages .success (self .request ,"Customer User Updated" )
598
+ return HttpResponseRedirect (reverse ("customer_list" ))
0 commit comments