Skip to content

Commit

Permalink
First Draft of Vend button for Caffeine
Browse files Browse the repository at this point in the history
  • Loading branch information
ace-n committed Mar 9, 2016
1 parent eea410b commit 16d41a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions liquid/intranet/caffeine_manager/trays/views.py
Expand Up @@ -6,7 +6,7 @@
from intranet.caffeine_manager.trays.models import Tray
from intranet.caffeine_manager.trays.forms import TrayForm
from intranet.caffeine_manager.views import fromLocations
import subprocess
import subprocess, datetime

def view(request):
request.session['from']=fromLocations.TRAYS
Expand Down Expand Up @@ -89,27 +89,33 @@ def buy_vend(request, trayId):

# Validate
tray = get_object_or_404(Tray, pk=trayId)
vendUser=request.user.get_vending()
vendUser = request.user.get_vending()
nextVendTime = vendUser.last_vend + datetime.timedelta(seconds=15)
if tray.qty < 1:
errorMessage = 'That tray is empty.'
elif tray.price > vendUser.balance:
errorMessage = 'You can\'t afford that item.'
# TODO add vend-time restriction here
elif datetime.datetime.now() < nextVendTime:
errorMessage = 'Please wait 15 seconds between vends.'

# Process valid purchase
if errorMessage is None:

# Update DB values
# Update Vending values
soda = tray.soda
soda.dispensed += 1
tray.qty -= 1
vendUser.balance -= tray.price
vendUser.spent += tray.price
vendUser.calories += soda.calories
vendUser.caffeine += soda.caffeine
vendUser.last_vend = datetime.datetime.now()
vendUser.sodas += 1
tray.save()
vendUser.save()

# Log purchase
# TODO

# Do vend
ret=0 #do_vend(trayId)
ret=do_vend(trayId)
if ret != 0:
errorMessage = 'Script failure: error code ' + str(ret) + '.'

Expand Down
1 change: 1 addition & 0 deletions liquid/intranet/models.py
Expand Up @@ -121,6 +121,7 @@ class Vending(models.Model):
caffeine = models.FloatField(default=0)
spent = models.DecimalField(max_digits=10, decimal_places=2,default=0)
sodas = models.IntegerField(max_length=11,default=0)
last_vend = models.DateTimeField(auto_now_add=True)

@property
def user(self):
Expand Down

0 comments on commit 16d41a2

Please sign in to comment.