Beispiel #1
0
def create_voucher():
    """register new voucher"""
    data = request.get_json(silent=True)
    if not data or not 'code' or not 'value' or not 'start' or not 'end' in data:
        abort(400)

    start = datetime.strptime(data['start'], '%Y-%m-%d %H:%M:%S')
    end   = datetime.strptime(data['end'], '%Y-%m-%d %H:%M:%S')
    if end <= start:
        abort(400)
    voucher = Voucher(code=data['code'], value=data['value'], start=start, end=end)
    if voucher.is_overlap():
        abort(400)
    voucher.save()
    return make_response(jsonify(voucher=voucher.serialize()), 201)
Beispiel #2
0
    def test_voucher_update(self):
        """Test API can update a Voucher (PUT request)"""
        start1 = datetime.strptime('2018-09-01 17:00:00', "%Y-%m-%d %H:%M:%S")
        end1 = datetime.strptime('2019-09-15 17:00:00', '%Y-%m-%d %H:%M:%S')
        v1 = Voucher(code='SNSD', value=5000, start=start1, end=end1)
        v1.save()

        params = dict(code='ABCD',
                      value=10000,
                      start='2018-09-01 17:00:00',
                      end='2019-09-15 17:00:00')
        res = self.client().put('/api/v1.0/vouchers/{}'.format(v1.id),
                                data=json.dumps(params, indent=4),
                                content_type='application/json')
        self.assertEqual(res.status_code, 201)
        self.assertIn('ABCD', str(res.data))
Beispiel #3
0
 def post(self, request):
     form = VoucherForm(request.POST)
     if form.is_valid():
         n = form.cleaned_data['n']
         price = form.cleaned_data['amount']
         startTime = form.cleaned_data['startTime']
         endTime = form.cleaned_data['endTime']
         for i in range(0, n):
             s = ''.join(choice(ascii_uppercase) for i in range(10))
             u = Voucher(code=s,
                         amount=price,
                         startTime=startTime,
                         endTime=endTime,
                         redeemed=False,
                         assigned=False)
             u.save()
         form = VoucherForm()
     args = {'form': form, 'text': "Vouchers Generated"}
     return render(request, 'index.html', args)