No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # This file is part of k4ever, a point-of-sale system
  3. # Contact............ <k4ever@lists.someserver.de>
  4. # Website............ http://k4ever.someserver.de/
  5. # Bug tracker........ http://k4ever.someserver.de/report
  6. #
  7. # Licensed under GNU Affero General Public License v3 or later
  8. from django.shortcuts import render_to_response
  9. from django.template import RequestContext
  10. from django.contrib.auth.decorators import login_required
  11. from django.http import HttpResponseRedirect
  12. import datetime
  13. from models import Buyable, Purchase, Order
  14. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger, InvalidPage
  15. @login_required
  16. def showItems(request):
  17. # FIXME: Implement pagination here
  18. items = Buyable.objects.all()
  19. try:
  20. order_by = request.GET.get('order', 'date')
  21. except ValueError:
  22. order_by = 'date'
  23. if order_by == 'alphabet':
  24. items = items.order_by('name')
  25. if order_by == 'price':
  26. items = items.order_by('price')
  27. return render_to_response("buyables/showItems.html", {'items': items}, RequestContext(request))
  28. @login_required
  29. def showItem(request, itemid):
  30. try:
  31. item = Buyable.objects.get(id=itemid)
  32. except Buyable.DoesNotExist:
  33. # baww, kein item mit der id :( (oder mutax trollt rum)
  34. return HttpResponseRedirect("/buy/")
  35. return render_to_response("buyables/showItem.html", {'item': item}, RequestContext(request))
  36. @login_required
  37. def buyItem(request, itemid, buymode=""):
  38. user = request.user
  39. try:
  40. item = Buyable.objects.get(id=itemid)
  41. except Buyable.DoesNotExist:
  42. # oh no! kein item mit der id :( mutax trollt rum)
  43. return HttpResponseRedirect("/store/buy/")
  44. """ tristate variable buymode:
  45. "" only item
  46. "with/deposit" item and deposit
  47. "only/deposit" only deposit
  48. """
  49. order = Order(user=request.user)
  50. order.save() # for the id!
  51. if buymode == "" or buymode == "with/deposit":
  52. p = Purchase(order=order, buyable=item, isDeposit=False)
  53. p.save(saveOrder=False)
  54. if buymode == "with/deposit" or buymode == "only/deposit":
  55. p = Purchase(order=order, buyable=item, isDeposit=True)
  56. p.save(saveOrder=False)
  57. order.save()
  58. return HttpResponseRedirect("/store/bought/%s/" % (order.id))
  59. @login_required
  60. def boughtItem(request, orderid):
  61. """ View which shows what was bought."""
  62. error = None
  63. try:
  64. item = Order.objects.get(id=orderid)
  65. except Order.DoesNotExist:
  66. error = "Die angegebene Order existiert nicht"
  67. if error == None and (item.user != request.user):
  68. item1 = None
  69. error = u"Diese Items gehören dir nicht"
  70. return render_to_response("buyables/itemBought.html", {'order': item, 'error': error}, RequestContext(request))
  71. @login_required
  72. def history(request):
  73. """ Show the users previous orders. """
  74. hist = Order.objects.filter(user=request.user.id).order_by("-dateTime")
  75. paginator = Paginator(hist, 10, orphans=3)
  76. try:
  77. pagenum = request.GET.get('page', 1)
  78. except ValueError:
  79. pagenum = 1
  80. try:
  81. histpage = paginator.page(pagenum)
  82. except PageNotAnInteger:
  83. # If page is not an integer, deliver first page
  84. histpage = paginator.page(1)
  85. except (EmptyPage, InvalidPage):
  86. # If page is out of range, deliver last page
  87. histpage = paginator.page(paginator.num_pages)
  88. return render_to_response("buyables/history.html", {'history': histpage}, RequestContext(request))