示例#1
0
文件: views.py 项目: khng300/hackman
def payment_submit(request, r=False):
    if request.method != 'POST':
        return http.HttpResponseBadRequest('Request not POST')

    form = forms.PaymentForm(request.POST,
                             year_month_choices=_get_month_choices())
    if not form.is_valid():
        return http.HttpResponseBadRequest('Form error')

    year, month = form.cleaned_data['year_month']
    payment_api.payment_submit(request.user.id, year, month)

    notification_api.notify_subject(
        b'door_event',
        json.dumps({
            'event': "PAYMENT_CLAIM",
            'until': "{}-{}".format(year, month),
            'user_id': request.user.id,
        }))

    paid = (payment_api.has_paid(request.user.id) !=
            payment_enums.PaymentGrade.NOT_PAID)

    # FIXME - we /just/ set the valid until above.  Switch to using
    # date objects everywhere and then just use the input to this function
    valid_until = payment_api.get_valid_until(request.user.id)
    if valid_until is not None:
        valid_until = valid_until.strftime('%Y-%m')

    return shortcuts.render(request,
                            'payment_submit.jinja2',
                            context={
                                'paid': paid,
                                'valid_until': valid_until,
                            })
示例#2
0
    def handle(self, *args, **kwargs):
        p = subprocess.run([
            'git', '-C', 'dsl-accounts/', 'pull', '-f', 'origin', 'master'
        ], stdout=subprocess.PIPE)
        if p.returncode != 0:
            sys.stderr.write('Git exited with non-zero exit code: {}\n'.format(
                p.returncode))

        p = subprocess.run([
            './dsl-accounts/balance.py',
            '--split',
            'json_payments'], stdout=subprocess.PIPE)
        if not p.returncode == 0:
            raise RuntimeError(p)

        data = json.loads(p.stdout)

        tags = models.PaymentTag.objects.all()
        tags = tags.prefetch_related('user')

        r = redis_pipe = get_redis_connection('default')
        pipe = r.pipeline()
        for t in tags:
            last_payment = data[t.make_key()]
            year, month = map(int, last_payment.split('-'))
            payment_api.payment_submit(t.user.id,
                                       year,
                                       month,
                                       _redis_pipe=redis_pipe)

        pipe.execute()
示例#3
0
def paid_user():
    from hackman_payments import api as payment_api

    next_month = datetime.utcnow() + timedelta(days=32)

    user = get_user_model().objects.create_user(username='******',
                                                password='******')
    payment_api.payment_submit(user.id, next_month.year, next_month.month)
    yield user
    get_redis_connection('default').flushall()
示例#4
0
    def handle(self, *args, **kwargs):
        try:
            res = urlopen(URL)
            text = res.read().decode()
            cache = open('db/payments.json.tmp', 'w')
            cache.write(text)
            cache.close()
            os.rename('db/payments.json.tmp', 'db/payments.json')
        except Exception as e:  # noqa - go jump in a lake pyflakes
            sys.stderr.write(repr(e)+"\n")
            sys.stderr.write("Attempting to fallback to cached local file\n")

            # any error at all, we try to fall back to the cached data
            cache = open('db/payments.json', 'r')
            text = cache.read()

        data = json.loads(text)

        import_tags = set(
            k for k in data.keys()
            if k.split(':', 1)[0] == 'dues')
        db_tags = set(t.make_key() for t in PaymentTag.objects.all())

        tags = models.PaymentTag.objects.all()
        tags = tags.prefetch_related('user')

        r = redis_pipe = get_redis_connection('default')
        pipe = r.pipeline()
        for t in tags:
            try:
                last_payment = data[t.make_key()]
            except KeyError:
                continue

            year, month = map(int, last_payment.split('-'))
            if not t.user_id:
                continue

            payment_api.payment_submit(
                t.user.id,
                year,
                month,
                _redis_pipe=redis_pipe)

        pipe.execute()

        for missed_tag in (import_tags-db_tags):
            hashtag, tag = missed_tag.split(':', 1)
            PaymentTag.objects.create(
                hashtag=hashtag,
                tag=tag)
示例#5
0
def payment_submit(request, r=False):
    if request.method != 'POST':
        return http.HttpResponseBadRequest('Request not POST')

    form = forms.PaymentForm(request.POST,
                             year_month_choices=_get_month_choices())
    if not form.is_valid():
        return http.HttpResponseBadRequest('Form error')

    year, month = form.cleaned_data['year_month']
    payment_api.payment_submit(request.user.id, year, month)

    try:
        messages.add_message(request, messages.SUCCESS, 'Payment submitted')
    except MessageFailure:  # pragma: no cover
        pass

    return shortcuts.redirect('/')
示例#6
0
def user_paid_grace():
    from hackman_payments import api as payments_api
    user = get_user_model().objects.create(username='******')
    payments_api.payment_submit(user.id, 2017, 1)
    yield user
    get_redis_connection('default').flushall()