Skip to content

Commit

Permalink
Add placeholder for writing credit cards
Browse files Browse the repository at this point in the history
  • Loading branch information
vr2262 committed Mar 10, 2017
1 parent 0e3cd95 commit fda5659
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
41 changes: 41 additions & 0 deletions minigrid/device_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,44 @@ def write_customer_card(minigrid_id, customer):
print(block_5.hex())
print(block_6.hex())
print('=' * 60)


def write_credit_card(minigrid_id, credit_amount, day_tariff, night_tariff):
"""Write information to a credit card."""
block_4 = _wrap_binary(b''.join((
b'C', # C for credit
b'\1', # 1 for int
credit_amount.to_bytes(4, 'big'), # 4 byte unsigned int
int(time.time()).to_bytes(4, 'big'), # card produced time
bytes(2), # intentionally empty
bytes(4), # card read time TODO
)))
block_5 = _wrap_binary(uuid.uuid4().bytes)
block_6 = _wrap_binary(uuid.UUID(minigrid_id).bytes)
block_8 = _wrap_binary(b''.join((
b'\1', # 1 for int
bytes(4), # tariff 1 validate time??? TODO
(int(day_tariff * 100)).to_bytes(4, 'big'), # day tariff in cents
bytes(7), # intentionally empty
)))
block_9 = _wrap_binary(b''.join((
b'\1', # 1 for int
bytes(4), # tariff 2 validate time??? TODO
(int(night_tariff * 100)).to_bytes(4, 'big'), # night tariff in cents
bytes(7), # intentionally empty
)))
block_10 = _wrap_binary(b''.join((
bytes(4), # time of these 2 tariff was created??? TODO
bytes(4), # time to start use tariff??? TODO
bytes(8), # intentionally empty
)))

# TODO write to device
print('=' * 60)
print(block_4.hex())
print(block_5.hex())
print(block_6.hex())
print(block_8.hex())
print(block_9.hex())
print(block_10.hex())
print('=' * 60)
19 changes: 17 additions & 2 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import tornado.web

from minigrid.device_interface import write_vendor_card, write_customer_card
from minigrid.device_interface import (
write_vendor_card, write_customer_card, write_credit_card)
import minigrid.error
import minigrid.models as models
from minigrid.options import options
Expand Down Expand Up @@ -250,7 +251,6 @@ def get(self, minigrid_id):
minigrid=models.get_minigrid(self.session, minigrid_id))


# TODO: this button should do something
class MinigridWriteCreditHandler(BaseHandler):
"""Handlers for writing credit cards view."""

Expand All @@ -261,6 +261,21 @@ def get(self, minigrid_id):
'minigrid_write_credit.html',
minigrid=models.get_minigrid(self.session, minigrid_id))

@tornado.web.authenticated
def post(self, minigrid_id):
"""Write a credit card for this minigrid."""
minigrid = models.get_minigrid(self.session, minigrid_id)
system = self.session.query(models.System).one()
write_credit_card(
minigrid_id,
int(self.get_argument('credit_value')),
system.day_tariff,
system.night_tariff,
)
message = 'Card written'
self.render(
'minigrid_write_credit.html', minigrid=minigrid, message=message)


class MinigridVendorsHandler(BaseHandler):
"""Handlers for vendors view."""
Expand Down
3 changes: 2 additions & 1 deletion minigrid/templates/minigrid_write_credit.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<h1>Minigrid Name: {{ minigrid.minigrid_name }}</h1>
<p>ID: {{ minigrid.minigrid_id }}</p>
<p>Write credit card:</p>
<form>
<form action="/minigrids/{{ minigrid.minigrid_id }}/write_credit" method="POST">
{% module xsrf_form_html() %}
<label>Card value: <input type="number" step="1" min="0" name="credit_value" required /></label><br>
<input type="submit" value="Write credit card" />
</form>
<footer>
Expand Down

0 comments on commit fda5659

Please sign in to comment.