Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. from django.shortcuts import render, redirect
  2. from django.http import Http404, HttpResponse,HttpResponseRedirect
  3. from django.template.context import RequestContext
  4. from django.db.models import Q
  5. from django.contrib import auth
  6. from django.contrib.auth.decorators import login_required
  7. from django.contrib.auth.forms import UserCreationForm
  8. from django.contrib.auth.models import User
  9. from django.contrib.auth.views import login, logout
  10. from .models import *
  11.  
  12.  
  13. def index(request):
  14. return render(request, 'index.html')
  15.  
  16.  
  17. def add_item_form(request):
  18. success = 0
  19.  
  20. if request.method == 'POST':
  21. success = 1
  22. new_item_name = request.POST['item_name']
  23. new_item_quantity = request.POST['item_quantity']
  24. new_item_price = request.POST['item_price']
  25. new_item_total_price = int(new_item_quantity) * int(new_item_price)
  26. try:
  27. old_item = Item.objects.get(storage=StorageRoom.objects.get(pk=1), item_name=new_item_name, item_price=new_item_price)
  28. except Item.DoesNotExist:
  29. old_item = None
  30. if old_item:
  31. old_item.item_quantity += int(new_item_quantity)
  32. old_item.item_total_price = int(old_item.item_quantity)*int(old_item.item_price)
  33. old_item.save()
  34. else:
  35. item = Item.objects.create(item_name=new_item_name,
  36. item_quantity=new_item_quantity,
  37. item_price=new_item_price,
  38. item_total_price=new_item_total_price,
  39. storage=StorageRoom.objects.get(pk=1))
  40. StorageRoom.objects.get(pk=1).storage_items.add(item)
  41. ctx = {'success': success}
  42. if 'SubmitAndNew' in request.POST:
  43. return render(request, 'add_item_form.html', ctx)
  44. elif 'SubmitToIndex' in request.POST:
  45. return redirect(index)
  46. else:
  47. return render(request, 'add_item_form.html', ctx)
  48.  
  49.  
  50. def storage_details(request, storage_id):
  51. storage = StorageRoom.objects.get(pk=storage_id)
  52. items = Item.objects.filter(storage=storage)
  53. services = Service.objects.filter(storage=storage)
  54. services.order_by("-date")
  55. success = 0
  56. if request.method == "POST":
  57. if request.POST.get('delete_items'):
  58. print('delete_items')
  59. quantity_to_delete = request.POST['item_quantity']
  60. success = 1
  61. item_to_delete = Item.objects.get(pk=int(request.POST['item_id']))
  62. if int(quantity_to_delete) >= int(item_to_delete.item_quantity):
  63. item_to_delete.delete()
  64. quantity_to_delete = str(max(int(quantity_to_delete), int(item_to_delete.item_quantity)))
  65. else:
  66. item_to_delete.item_quantity = str(int(item_to_delete.item_quantity) - int(quantity_to_delete))
  67.  
  68. Report.objects.create(item=item_to_delete,
  69. storage_from=StorageRoom.objects.get(storage_name=item_to_delete.storage.storage_name),
  70. storage_to="DELETED",
  71. person=request.user.username,
  72. quantity=quantity_to_delete)
  73. else:
  74. print('submitservice')
  75. service_to_add = request.POST['ServiceSelect']
  76. service_price = request.POST['service_price']
  77. Service.objects.create(type = service_to_add,
  78. storage = storage,
  79. price = service_price)
  80.  
  81. ctx = {'storage': storage,
  82. 'items': items,
  83. 'success': success,
  84. 'services': services}
  85.  
  86. return render(request, 'storage_details.html', ctx)
  87.  
  88.  
  89. def add_storage_form(request):
  90. success = 0
  91. storages = StorageRoom.objects.all()
  92. ctx = {}
  93. ctx.update({'storages': storages})
  94. if request.method == 'POST':
  95. success = 1
  96. new_storage_name = request.POST['storage_name']
  97.  
  98. StorageRoom.objects.create(storage_name=new_storage_name)
  99. ctx.update({'success': success})
  100. if 'SubmitAndNew' in request.POST:
  101. return render(request, 'add_storage_form.html', ctx)
  102. elif 'SubmitToIndex' in request.POST:
  103. return redirect(index)
  104. else:
  105. return render(request, 'add_storage_form.html', ctx)
  106.  
  107.  
  108. def transport_item(request, item_id):
  109. item_to_transport = Item.objects.get(pk=item_id)
  110. success = 0
  111. """for stor in StorageRoom.objects.all():
  112. if item_to_transport.storage == stor:
  113. storage_from = stor"""
  114. storage_from = item_to_transport.storage
  115. if request.method == 'POST':
  116. success = 1
  117. quantity_to_transport = request.POST['item_quantity']
  118. storage_to = StorageRoom.objects.get(storage_name=request.POST['storage_to_name'])
  119. Report.objects.create(item=item_to_transport.item_name,
  120. storage_from=StorageRoom.objects.get(storage_name=storage_from.storage_name),
  121. storage_to=storage_to.storage_name,
  122. person=str(request.POST['person_name']),
  123. quantity=quantity_to_transport)
  124. item_price = item_to_transport.item_price
  125. try:
  126. old_item = Item.objects.get(item_name=item_to_transport, storage=storage_to)
  127. except Item.DoesNotExist:
  128. old_item = None
  129. if old_item is not None:
  130. old_item.item_quantity += int(quantity_to_transport)
  131. old_item.item_total_price += int(old_item.item_quantity)*int(old_item.item_price)
  132. old_item.save()
  133. else:
  134. item = Item.objects.create(item_name=item_to_transport.item_name,
  135. item_quantity=quantity_to_transport,
  136. item_price=item_price,
  137. item_total_price=int(item_price)*int(quantity_to_transport),
  138. storage=storage_to)
  139. storage_to.storage_items.add(item)
  140. storage_to.save()
  141. if int(quantity_to_transport) == int(item_to_transport.item_quantity):
  142. Item.objects.get(pk=item_to_transport.id).delete()
  143. else:
  144. item_to_transport.item_quantity -= int(quantity_to_transport)
  145. item_to_transport.item_total_price -= int(quantity_to_transport) * int(item_price)
  146. item_to_transport.save()
  147.  
  148. storages = StorageRoom.objects.all()
  149. ctx = {'storage_from': storage_from,
  150. 'storages': storages,
  151. 'item_to_transport': item_to_transport,
  152. 'success': success}
  153. return render(request, 'transport_item.html', ctx)
  154.  
  155.  
  156. def storage_list(request):
  157. storages = StorageRoom.objects.all()
  158. items = Item.objects.all()
  159. #items_quantites = StorageRoom.storage_items.all()
  160. # items = Item.objects.all()
  161. # items_quantity = len(items)
  162. #items =StorageRoom.sorage_items.
  163. ctx = {'storages': storages,
  164. 'items': items}
  165.  
  166. return render(request, 'storage_list.html', ctx)
  167.  
  168.  
  169. def report(request):
  170. reports = Report.objects.all()
  171.  
  172. ctx = {'reports': reports}
  173.  
  174. return render(request, 'report.html', ctx)
  175.  
  176.  
  177. def delete_item(request, item_id):
  178. item_to_delete = Item.objects.get(pk=item_id)
  179. storage_id = Item.objects.get(pk=item_id).storage.id
  180.  
  181. if request.method == "POST":
  182. quantity_to_transport = request.POST['item_quantity']
  183. storage_from = item_to_delete.storage
  184. storage_to = "DELETED"
  185. Report.objects.create(item=item_to_delete.item_name,
  186. storage_from=StorageRoom.objects.get(storage_name=storage_from.storage_name),
  187. storage_to=storage_to,
  188. person=str(request.POST['person_name']),
  189. quantity=quantity_to_transport)
  190.  
  191. item_price = item_to_delete.item_price
  192. item_to_delete.delete()
  193. return redirect(storage_details, storage_id=storage_id)
  194. ctx = {'item_to_delete': item_to_delete}
  195. return render(request, 'delete_item.html', ctx)
  196.  
  197.  
  198. def search(request):
  199. if request.is_ajax():
  200. q = request.GET.get('q')
  201. revers = request.GET.get('rev')
  202. if q is not None:
  203. items = Item.objects.filter(
  204. Q(item_name__contains=q))
  205. storages = StorageRoom.objects.filter(
  206. Q(storage_name__contains=q))
  207.  
  208. storage_items = Item.objects.all()
  209. ctx = {
  210.  
  211. 'items': items,
  212. 'storages': storages,
  213. 'storage_items': storage_items
  214. }
  215.  
  216. return render(request, 'results.html', ctx)
  217.  
  218.  
  219. def report_search(request):
  220. if request.is_ajax():
  221. q = request.GET.get('q')
  222. revers = request.GET.get('rev')
  223. if q is not None:
  224. #items = Report.objects.filter(Q(date__contains=q))
  225. #items += Report.objects.filter(Q(storage_from__contains=q))
  226. items = Report.objects.filter(
  227. Q(storage_to__contains=q) |
  228. Q(date__year__contains=q) |
  229. Q(date__month__contains=q) |
  230. Q(date__day__contains=q) |
  231. Q(person__contains=q))
  232. if int(revers) == 1:
  233. items = items.order_by("-date")
  234. ctx = {
  235. 'items': items,
  236. }
  237.  
  238. return render(request, 'report_result.html', ctx)
  239.  
  240.  
  241. def login(request):
  242. """Shows a login form and a registration link."""
  243.  
  244. if request.method == 'POST':
  245. username = request.POST['username']
  246. password = request.POST['password']
  247. user = auth.authenticate(username=username, password=password)
  248. print(user)
  249. if user is not None and user.is_active:
  250. auth.login(request, user)
  251. return HttpResponseRedirect("/")
  252.  
  253. else:
  254. return HttpResponse("Invalid login. Please try again.")
  255.  
  256. # if not POST then return login form
  257. return render(request, "login.html", {'next': ''})
  258.  
  259.  
  260. def search_storages(request):
  261. if request.is_ajax():
  262. q = request.GET.get('q')
  263. if q is not None:
  264. storages = StorageRoom.objects.filter(
  265. Q(storage_name__contains=q))
  266. ctx = {
  267. 'storages': storages,
  268. }
  269.  
  270. return render(request, 'search_storages.html', ctx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement