예제 #1
0
 def test_attributes(self):
     self.assertEqual(str(L.h1(class_='hello') / 'world'),
                      '<h1 class="hello">world</h1>')
     self.assertEqual(str(L.input(id='hello', value='world')),
                      '<input id="hello" value="world"/>')
     self.assertEqual(str(L.input(what='')), '<input what/>')
     self.assertEqual(str(L.input(what=None)), '<input what/>')
     self.assertEqual(str(L.input(data_trigger='666')),
                      '<input data-trigger="666"/>')
예제 #2
0
def index(request):
    email = None
    message = None

    if 'token' in request.GET:
        try:
            token = LoginToken.objects.get(email=request.GET['email'], token=request.GET['token'])
            email = token.email
        except LoginToken.DoesNotExist:
            pass

    if email: # logged in
        account = Account.objects.get(email=email)

        if request.method == 'POST':
            # todo: atomize / transaction
            # todo: validate email
            dest_email = request.POST['email']
            dest_amount = -1
            message = 'invalid amount'
            try:
                dest_amount = int(request.POST['amount'])
            except ValueError:
                pass
            optional_message = ''
            if request.POST['message']:
                optional_message = '\nHis message: \n' + slugify(request.POST['message'][:140], allow_unicode=True)
            if account.amount >= dest_amount and dest_amount > 0: # TODO: else "not enough founds"
                try:
                    dest_account = Account.objects.get(email=dest_email)
                    dest_account.amount += dest_amount
                    dest_account.save()
                except Account.DoesNotExist:
                    dest_account = Account.objects.create(email=dest_email, amount=dest_amount)

                token = get_random_string()
                LoginToken.objects.create(email=dest_email, token=token, expire_on=datetime.date.today() + datetime.timedelta(days=1))

                send_mail('[petals] %s sent you %d petals' % (email, dest_amount), """%s sent you %d petals.%s
You current balance is now: %d petals

http://petal.x.dam.io/?token=%s&email=%s
""" % (email, dest_amount, optional_message, dest_account.amount, token, dest_email), '*****@*****.**', [dest_email], fail_silently=False)
                account.amount -= dest_amount
                account.save()
                message = '%d sent' % dest_amount
    else:
        if request.method == 'POST':
            email = request.POST['email']
            try:
                account = Account.objects.get(email=email)
                token = get_random_string()
                LoginToken.objects.create(email=email, token=token, expire_on=datetime.date.today() + datetime.timedelta(days=1))
                send_mail('[petals] link to connect to your account', """Here is a temporary link to connect to your account:
http://petal.x.dam.io/?token=%s&email=%s
""" % (token, email), '*****@*****.**', [email], fail_silently=False)
                message = 'connection link sent to %s' % email
            except Account.DoesNotExist:
                pass
        email = None # lol

    total = sum([account.amount for account in Account.objects.all()])

    content = L.div('.container') / (
        L.div('.row') / (
            L.div('.col-sm-12') / (
                L.h3 / (L.a(href='/') / 'Petal'),
                L.strong / ('%d petals in circulation' % total),
                L.hr,
            ),
        ),
        (
            L.div('.row') / (
                L.div('.col-sm-3') / (
                    L.h3 / email,
                    L.p / ('You have %d petals' % account.amount),
                    L.h4 / 'send petals',
                    L.form(method='post') / (
                        L.input(type='hidden', name='csrfmiddlewaretoken', value=get_token(request)),
                        L.div / (
                            L.label('control-label') / 'email', L.br, L.input(name='email'),
                        ),
                        L.div / (
                            L.label('control-label') / 'amount', L.br, L.input(name='amount', type='number'),
                        ),
                        L.div / (
                            L.label('control-label') / 'message (optional)', L.br, L.input(name='message', max_length="140"),
                        ),
                        L.br,
                        L.button(type='submit') / 'send petals',
                    ),
                    L.i / message,
                ),
            )
        ) if email else (
            L.div('.row') / (
                L.div('.col-sm-3') / (
                    L.h3 / 'see your account',
                    L.p / 'You will receive an email with a link to login to your account',
                    L.form(method='post') / (
                        L.input(type='hidden', name='csrfmiddlewaretoken', value=get_token(request)),
                        L.div / (
                            L.label('control-label') / 'email', L.br, L.input(name='email'),
                        ),
                        L.br,
                        L.button(type='submit') / 'send email with login link'
                    ),
                    L.i / message,
                ),
            )
        )
    )
    return HttpResponse(tpl_base(content))