Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Membership expiries #20

Merged
merged 4 commits into from Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions uqcs/__init__.py
Expand Up @@ -2,22 +2,22 @@
from .workers import mailchimp_worker, mailer_worker
from .base import mailchimp_queue, mailer_queue, Session, DB
from . import models as m
import sqlalchemy as sa
import os
import threading
import waitress
import logging


def main(args):
logging.basicConfig(level=logging.DEBUG)
app.config['SQLALCHEMY_DATABASE_URI'] = args[1]
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
DB.init_app(app)
with app.app_context():
s = Session()
m.Base.metadata.create_all(s.connection())
s.commit()


mailchimp_thread = threading.Thread(
target=mailchimp_worker,
args=(mailchimp_queue,),
Expand Down
19 changes: 18 additions & 1 deletion uqcs/app.py
@@ -1,6 +1,8 @@
import os
import re
import stripe
from datetime import date
from calendar import isleap
from .templates import lookup
from flask import Flask, request, session, flash, get_flashed_messages, redirect
from . import models as m
Expand Down Expand Up @@ -80,10 +82,25 @@ def user_from_request(req):
@app.route("/", methods=["GET", "POST"])
@needs_db
def form(s):
def expiry():
curr_year = date.today().year
expiry_today = f"Feb 29th, {curr_year + 1}" if isleap(
curr_year + 1) else f"Feb 28th, {curr_year + 1}"
start_future = f"Jan 1st, {curr_year + 1}"
expiry_future = f"Feb 29th, {curr_year + 2}" if isleap(
curr_year + 2) else f"Feb 28th, {curr_year + 2}"
return expiry_today, start_future, expiry_future

stripe_pubkey = os.environ.get('STRIPE_PUBLIC_KEY')
if request.method == "GET":
template = lookup.get_template('form.mako')
return template.render(request=request, get_msgs=get_flashed_messages, STRIPE_PUBLIC_KEY=stripe_pubkey), 200
expiry_today, start_future, expiry_future = expiry()
return template.render(request=request,
get_msgs=get_flashed_messages,
expiry_today=expiry_today,
start_future=start_future,
expiry_future=expiry_future,
STRIPE_PUBLIC_KEY=stripe_pubkey), 200
else:
if s.query(m.Member).filter(m.Member.email == request.form.get('email')).count() > 0:
flash("That email has already been registered", 'danger')
Expand Down
2 changes: 2 additions & 0 deletions uqcs/base.py
Expand Up @@ -9,12 +9,14 @@

DB = SQLAlchemy()
Session = DB.session
Session.expire_on_commit = False


def needs_db(fn):
@wraps(fn)
def decorated(*args, **kwargs):
s = Session()
s.expire_on_commit = False
with s.begin_nested():
result = fn(s, *args, **kwargs)
s.commit()
Expand Down
2 changes: 1 addition & 1 deletion uqcs/emails/email.mako
Expand Up @@ -304,7 +304,7 @@ a {
</tr>
<tr>
<td class="content-block">
<h2 class="aligncenter">Welcome to UQCS in 2019</h2>
<h2 class="aligncenter">Welcome to UQCS in 2020</h2>
</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions uqcs/emails/email.mtxt
Expand Up @@ -3,13 +3,13 @@ $ ${'5.40' if user.paid != "CASH" else '5.00'} Paid
**********

-----------------------
Welcome to UQCS in 2019
Welcome to UQCS in 2020
-----------------------

${user.first_name} ${user.last_name}
${str(dt.datetime.now())}

UQCS 2019 Membership
UQCS 2020 Membership
$ 5.00

% if user.paid != "CASH":
Expand Down
2 changes: 1 addition & 1 deletion uqcs/templates/base.mako
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Join UQCS 2019</title>
<title>Join UQCS 2020</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Favicon -->
<link rel="apple-touch-icon" sizes="180x180" href="static/apple-touch-icon.png">
Expand Down
15 changes: 11 additions & 4 deletions uqcs/templates/form.mako
Expand Up @@ -2,8 +2,9 @@

<div class="row" style="text-align:center">
<div id="body" class="col-sm-12 col-md-6 col-md-offset-3">
<br/>
<img src="/static/logo.png" />
<h1>2019 UQCS Registration</h1>
<h1>2020 UQCS Registration</h1>
<div class="flash">
% for category, msg in get_msgs(with_categories=True):
<div class="alert alert-${category} alert-dismissible" role="alert">
Expand Down Expand Up @@ -161,8 +162,9 @@
</div>
</div>
<input type="hidden" name="stripeToken" value="" id="stripeToken" />
<br/>
<input
class="btn btn-primary"
class="btn btn-success"
name="submit"
type="submit"
id="payonline_submit"
Expand All @@ -177,16 +179,21 @@
id="submitbtn"
/>
<input
class="btn btn-primary"
class="btn btn-success"
name="submission"
type="submit"
value="Pay Cash"
/>
</form>
<br/>
<div class="text-muted">
<p></p>
<p>Online payments have a 40c card surcharge.</p>
</div>
<div class="text-muted">
<p>Membership purchased today will expire on ${expiry_today}.</p>
<p>Membership purchased from ${start_future} will expire on ${expiry_future}.</p>
</div>
</div>
</div>
<script src="https://checkout.stripe.com/checkout.js"></script>
Expand All @@ -206,7 +213,7 @@
}
handler.open({
name: "UQCS",
description: "2019 Membership",
description: "2020 Membership",
currency: "aud",
amount: 540,
email: $("#emailInput").val()
Expand Down
6 changes: 3 additions & 3 deletions uqcs/workers.py
@@ -1,4 +1,4 @@
from mailchimp3 import MailChimp
from mailchimp3 import MailChimp, mailchimpclient
import datetime as dt
import premailer
import requests
Expand Down Expand Up @@ -29,7 +29,7 @@ def mailchimp_worker(queue: Queue):
digest = h.hexdigest()
client.lists.members.get(list_id, digest)
save_fn = functools.partial(client.lists.members.update, list_id, digest)
except requests.exceptions.HTTPError as e:
except (requests.exceptions.HTTPError, mailchimpclient.MailChimpError) as _:
save_fn = functools.partial(client.lists.members.create, list_id)

data = {
Expand Down Expand Up @@ -77,7 +77,7 @@ def mailer_worker(mailqueue):
'bcc': "receipts@uqcs.org.au",
'text': receiptText,
'html': premailer.transform(receiptHTML),
'subject': "UQCS 2019 Membership Receipt",
'subject': "UQCS 2020 Membership Receipt",
})
except Exception as e:
logger.exception(e)