Example #1
0
async def create_event(request):
    body = request.json

    if 'uid' not in body or 'token' not in body or 'title' not in body or \
        'location' not in body or 'start' not in body or 'end' not in body \
        or 'description' not in body or 'image' not in body:
        return json_response({'error': 'Bad request'}, status=400)

    title = body['title']
    repeat, end_repeat, repeat_days = None, None, None

    if 'repeat' in body:
        repeat = body['repeat']
    if 'end_repeat' in body:
        end_repeat = body['end_repeat']
    if 'repeat_days' in body:
        repeat_days = dumps(body['repeat_days'])

    res = db.create_event(body['uid'], body['token'], title, body['location'],
                          body['start'], body['end'], body['description'],
                          repeat, end_repeat, repeat_days, body['image'])

    if 'error' in res:
        return json_response({'error': res['error']}, status=res['status'])

    msg = 'A new event ({}) has been added! Check it out!'.format(title)
    rejected_tokens = pusher.send_notifications(db.get_event_apn_tokens(),
                                                msg, 'event')

    for apn_token in rejected_tokens:
        db.remove_apn_token(apn_token)

    return json_response(res, status=201)
Example #2
0
async def update_team(request):
    body = request.json

    if 'uid' not in body or 'token' not in body or 'tid' not in body or \
        'name' not in body or 'description' not in body or \
        'leaders' not in body or 'meetings' not in body or \
        'groupme' not in body or 'members' not in body or 'admins' not in body:
        return json_response({'error': 'Bad Request'}, status=400)

    name = body['name']
    res = db.update_team(body['uid'], body['token'], body['tid'], name,
                         body['description'], body['leaders'],
                         body['meetings'], body['groupme'], body['members'],
                         body['admins'])

    if 'error' in res:
        return json_response({'error': res['error']}, status=res['status'])

    msg = 'You\'ve been added to {} Team!'.format(name)
    tokens = db.get_team_apn_tokens(res['new_members'])
    rejected_tokens = pusher.send_notifications(tokens, msg, 'team')

    for apn_token in rejected_tokens:
        db.remove_apn_token(apn_token)

    return json_response({}, status=200)
Example #3
0
async def complete_request(request):
    form = request.form

    if 'uid' not in form or 'tid' not in form or 'email' not in form or \
        'password' not in form or 'token' not in form:
        return redirect('{}/request/completed?msg={}'.\
            format(baseURI, 'Error: Bad Request'))

    uid, tid, token = form['uid'][0], form['tid'][0], form['token'][0]

    res = db.complete_team_request(uid, tid, token, form['email'][0],
                                   form['password'][0])
    team = db.get_team_info(tid)

    error = None
    if 'error' in res:
        error = res['error']
    elif team is None:
        error = 'Ministry team no longer exists'

    if error:
        return redirect('{}/request?uid={}&tid={}&token={}&error={}'.\
            format(baseURI, uid, tid, token, error))

    msg = 'You\'ve been added to {} Team!'.format(team[0])
    rejected_tokens = pusher.send_notifications(db.get_team_apn_tokens([uid]),
                                                msg, 'team')

    for apn_token in rejected_tokens:
        db.remove_apn_token(apn_token)

    return redirect('{}/request/completed?msg={}'.\
        format(baseURI, 'Success! User was added to the team.'))
Example #4
0
async def update_event(request):
    body = request.json

    if 'uid' not in body or 'token' not in body or 'eid' not in body or \
        'title' not in body or 'location' not in body or \
        'start' not in body or 'end' not in body or \
        'description' not in body or 'image' not in body:
        return json_response({'error': 'Bad request'}, status=400)

    title, start, end = body['title'], body['start'], body['end']
    repeat, end_repeat, repeat_days = None, None, None

    if 'repeat' in body:
        repeat = body['repeat']
    if 'end_repeat' in body:
        end_repeat = body['end_repeat']
    if 'repeat_days' in body:
        repeat_days = dumps(body['repeat_days'])

    res = db.update_event(body['uid'], body['token'], body['eid'], title,
                          body['location'], start, end, body['description'],
                          repeat, end_repeat, repeat_days, body['image'])

    if 'error' in res:
        return json_response({'error': res['error']}, status=res['status'])

    start_dt = datetime.strptime(start, '%Y-%m-%d %H:%M:%S')
    end_dt = datetime.strptime(end, '%Y-%m-%d %H:%M:%S')

    start_dt_old = datetime.strptime(res['start_old'], '%Y-%m-%d %H:%M:%S')
    end_dt_old = datetime.strptime(res['end_old'], '%Y-%m-%d %H:%M:%S')

    if start_dt != start_dt_old or end_dt != end_dt_old:

        msg = 'The time of {} has been changed. It will now '.format(title)
        start_time_str = start_dt.strftime('%I:%M%p').lstrip('0').lower()
        end_time_str = end_dt.strftime('%I:%M%p').lstrip('0').lower()

        if start_dt.date() == end_dt.date():
            day = start_dt.strftime('%A, %B ')
            day += number.ordinal(start_dt.day)
            msg += 'be from {} to {} on {}.'.format(start_time_str,
                                                    end_time_str, day)
        else:
            start_day = start_dt.strftime('%A, %B ')
            start_day += number.ordinal(start_dt.day)
            end_day = end_dt.strftime('%A, %B ')
            end_day += number.ordinal(end_dt.day)

            msg += 'start on {} at {} and end on {} at {}.'.\
                format(start_day, start_time_str, end_day, end_time_str)

        rejected_tokens = pusher.send_notifications(db.get_event_apn_tokens(),
                                                    msg, 'event')
        for apn_token in rejected_tokens:
            db.remove_apn_token(apn_token)

    return json_response({}, status=201)
Example #5
0
async def complete_request(request):
    form = request.form

    if 'uid' not in form or 'cid' not in form or 'email' not in form or \
        'password' not in form or 'token' not in form:
        return redirect('{}/request/completed?msg={}'.\
            format(baseURI, 'Error: Bad Request'))

    uid, cid, token = form['uid'][0], form['cid'][0], form['token'][0]

    res = db.complete_course_request(uid, cid, token, form['email'][0],
                                     form['password'][0])
    course = db.get_course_info(cid)

    error = None
    if 'error' in res:
        error = res['error']
    elif course is None:
        error = 'Bible course no longer exists'

    if error:
        return redirect('{}/request?uid={}&cid={}&token={}&error={}'.\
            format(baseURI, uid, cid, token, error))

    leader, year, gender = course
    if leader.endswith('s'):
        leader += "'"
    else:
        leader += "'s"
    msg = 'You\'ve been added to {} {} {}!'.format(leader, year, gender)

    rejected_tokens = pusher.send_notifications(db.get_course_apn_tokens([uid]),
                                                msg, 'course')

    for apn_token in rejected_tokens:
        db.remove_apn_token(apn_token)

    return redirect('{}/request/completed?msg={}'.\
        format(baseURI, 'Success! User was added to the course.'))
Example #6
0
async def update_course(request):
    body = request.json

    if 'uid' not in body or 'token' not in body or 'cid' not in body or \
        'leader_first' not in body or 'leader_last' not in body or \
        'year' not in body or 'gender' not in body or \
        'location' not in body or 'material' not in body or \
        'meetings' not in body or 'abcls' not in body or \
        'groupme' not in body or 'members' not in body or 'admins' not in body:
        return json_response({'error': 'Bad request'}, status=400)

    leader, year, gender = body['leader_first'], body['year'], body['gender']

    res = db.update_course(body['uid'], body['token'], body['cid'],
                           leader, body['leader_last'], year, gender,
                           body['location'], body['material'], body['meetings'],
                           body['abcls'], body['groupme'], body['members'],
                           body['admins'])

    if 'error' in res:
        return json_response({'error': res['error']}, status=res['status'])

    if leader.endswith('s'):
        leader += "'"
    else:
        leader += "'s"

    tokens = db.get_course_apn_tokens(res['new_members'])

    msg = 'You\'ve been added to {} {} {}!'.format(leader, year, gender)
    rejected_tokens = pusher.send_notifications(tokens, msg, 'course')

    for apn_token in rejected_tokens:
        db.remove_apn_token(apn_token)

    return json_response({}, status=201)