22
33from django .http import JsonResponse
44from django .views import View
5+ from django .views .generic import ListView , DetailView
56from django .core .serializers import serialize #, JSONSerializer
67from django .core .serializers .json import DjangoJSONEncoder
78#from django.core.signing import Signer
1011from django .views .decorators .csrf import csrf_exempt , csrf_protect , requires_csrf_token , ensure_csrf_cookie
1112from django .utils .decorators import method_decorator
1213from django .views .decorators .http import require_http_methods
14+ from django .core .paginator import Paginator
1315from django .conf import settings
1416from django .db .models import F
1517
1618from users .models import User
17- from refs .models import Product
19+ from refs .models import DocType , Product
20+ from core .models import Doc , Record
1821
1922
2023@csrf_exempt
@@ -68,15 +71,11 @@ def url_logout(request):
6871 return JsonResponse ({'success' :"You're logged out." })
6972
7073
71- #import asyncio
7274class ProductsView (View ):
7375 queryset = Product .objects .all ()
7476
75- #@method_decorator([login_required, csrf_protect, requires_csrf_token, ensure_csrf_cookie])
76- #async def get(self, request, *args, **kwargs):
7777 @method_decorator ([ensure_csrf_cookie ])
7878 def get (self , request , * args , ** kwargs ):
79- #await asyncio.sleep(1)
8079 data = serialize ('json' , self .queryset , fields = ('id' , 'article' , 'name' , 'cost' , 'price' , 'barcodes' , 'currency' ))
8180 return JsonResponse (data , safe = False )
8281
@@ -90,8 +89,83 @@ def get(self, request, *args, **kwargs):
9089 #qs = self.queryset.annotate(curr=F('currency__name')).prefetch_related('currency', 'barcodes').values('id', 'article', 'name', 'price', 'barcodes', 'curr')
9190 #json_data = json.dumps(list(qs), cls=DjangoJSONEncoder)
9291 #data = {it['id']:dict(filter(dflt, it.items())) for it in qs}
93- data = {}
92+ data = []
9493 for it in self .queryset .prefetch_related ('currency' , 'barcodes' , 'qrcodes' , 'group' , 'unit' ):
95- data [ it .id ] = { 'article' :it .article , 'name' :it .name , 'price' :it .price , 'barcodes' :list (it .barcodes .values_list ('id' , flat = True )), 'qrcodes' :list (it .qrcodes .values_list ('id' , flat = True )), 'currency' :it .currency .name , 'group ' :it .group .name , 'unit' :it .unit .label }
94+ data . append ({ 'id' : it .id , 'article' :it .article , 'name' :it .name , 'price' :it .price , 'barcodes' :list (it .barcodes .values_list ('id' , flat = True )), 'qrcodes' :list (it .qrcodes .values_list ('id' , flat = True )), 'currency' :it .currency .name if it . currency else '' , 'grp ' :it .group .name if it . group else '' , 'unit' :it .unit .label if it . unit else '' })
9695 json_data = json .dumps (data , cls = DjangoJSONEncoder )
9796 return JsonResponse (json_data , safe = False )
97+
98+
99+ class DocView (DetailView ):
100+ context_object_name = 'doc'
101+ queryset = Doc .objects .none ()
102+
103+ @method_decorator ([ensure_csrf_cookie ])
104+ def get_context_data (self , ** kwargs ):
105+ context = super ().get_context_data (** kwargs )
106+ context ['doc_list' ] = Doc .objects .all ()
107+ return context
108+
109+ @method_decorator ([ensure_csrf_cookie ])
110+ def get (self , request , * args , ** kwargs ):
111+ data = serialize ('json' , self .queryset , fields = ('id' , 'article' , 'name' , 'cost' , 'price' , 'barcodes' , 'currency' ))
112+ return JsonResponse (data , safe = False )
113+
114+
115+ class DocCashAddView (DocView ):
116+ context_object_name = 'doc-cash'
117+
118+ @method_decorator ([ensure_csrf_cookie ])
119+ def post (self , request , * args , ** kwargs ):
120+ try :
121+ data = json .loads (request .body )
122+ except json .decoder .JSONDecodeError as e :
123+ logging .error (e )
124+ return JsonResponse ({'result' :f'error: { e } ' }, status = 400 )
125+ except Exception as e :
126+ logging .error (e )
127+ return JsonResponse ({'result' :f'error: { e } ' }, status = 500 )
128+ sum_final = data .get ('sum_final' , 0 )
129+ records = data .get ('records' , [])
130+ if records and sum_final :
131+ t , created = DocType .objects .get_or_create (alias = 'sale' , defaults = {'alias' :'sale' , 'name' :'Sale' })
132+ doc = Doc (type = t , author = request .user )
133+ try :
134+ doc .save ()
135+ except Exception as e :
136+ logging .error (e )
137+ return JsonResponse ({'result' :f'error: { e } ' }, status = 500 )
138+ else :
139+ for r in records :
140+ try :
141+ p = Product .objects .get (pk = r ['product' ])
142+ except Exception as e :
143+ logging .error ([r , e ])
144+ else :
145+ record = Record (count = r ['count' ], cost = p .cost , price = r ['price' ], doc = doc , currency = p .currency , product = p )
146+ try :
147+ record .save ()
148+ except Exception as e :
149+ logging .error ([r , record , e ])
150+ return JsonResponse ({'result' :'success' , 'doc' :f'{ doc .id } ' })
151+
152+
153+ class DocsView (ListView ):
154+ paginate_by = 10
155+ model = Doc
156+ context_object_name = 'docs'
157+ queryset = Doc .objects .all ()
158+
159+ @method_decorator ([ensure_csrf_cookie ])
160+ def get (self , request , * args , ** kwargs ):
161+ if request .GET :
162+ self .queryset = self .queryset .filter (** request .GET )
163+ paginator = Paginator (self .queryset , self .paginate_by )
164+ page_number = request .GET .get ('page' )
165+ page_obj = paginator .get_page (page_number )
166+ logging .debug (page_obj .object_list )
167+ #data = {'page':page_number, 'docs':[self.queryset]}
168+ #json_data = json.dumps(data, cls=DjangoJSONEncoder)
169+ json_data = serialize ('json' , page_obj , fields = ('id' , 'created_at' , 'registered_at' , 'owner' , 'contractor' , 'type' , 'tax' , 'sale_point' , 'sum_final' , 'author' ))
170+ logging .debug (json_data )
171+ return JsonResponse (json_data , safe = False )
0 commit comments