Exemple #1
0
def register(request):
    if not get_bool('REGISTRATION_OPEN', True):
        return HttpResponseRedirect(reverse('index'))

    if request.method == 'POST':
        course_code = request.POST.get('course', None)
        email_address = request.POST.get('email', None)
        phone = request.POST.get('phone', None)

        res = register_for_course(course_code, email_address, phone)

        if res == RegStatus.SUCCESS:
            return homepage_with_msg(
                request, 'success',
                'Your registration for %s was successful!' % course_code)
        elif res == RegStatus.OPEN_REG_EXISTS:
            return homepage_with_msg(
                request, 'warning',
                "You've already registered to get alerts for %s!" %
                course_code)
        elif res == RegStatus.NO_CONTACT_INFO:
            return homepage_with_msg(
                request, 'danger',
                'Please enter either a phone number or an email address.')
        else:
            return homepage_with_msg(
                request, 'warning',
                'There was an error on our end. Please try again!')
    else:
        raise Http404('GET not accepted')
Exemple #2
0
def should_send_pca_alert(course_term, course_status):
    if get_current_semester() != course_term:
        return False
    add_drop_period = get_or_create_add_drop_period(course_term)
    return (get_bool("SEND_FROM_WEBHOOK", False)
            and (course_status == "O" or course_status == "C")
            and (add_drop_period.end is None or datetime.utcnow().replace(
                tzinfo=gettz(TIME_ZONE)) < add_drop_period.end))
Exemple #3
0
def pca_registration_open():
    """
    Returns True iff PCA should be accepting new registrations.
    """
    current_adp = get_or_create_add_drop_period(
        semester=get_current_semester())
    return get_bool("REGISTRATION_OPEN", True) and (
        current_adp.end is None or
        datetime.utcnow().replace(tzinfo=gettz(TIME_ZONE)) < current_adp.end)
Exemple #4
0
 def test_get_bool_no_exists(self):
     self.assertIsNone(get_bool("invalid"))
Exemple #5
0
 def test_get_bool_exists_is_bool(self):
     self.option.value_type = Option.TYPE_BOOL
     self.option.value = "TruE"
     self.option.save()
     self.assertTrue(get_bool(self.key))
Exemple #6
0
 def test_get_bool_exists_not_bool(self):
     self.assertIsNone(get_bool(self.key))
Exemple #7
0
def index(request):
    if not get_bool('REGISTRATION_OPEN', True):
        return homepage_closed(request)

    return render_homepage(request, [])
Exemple #8
0
def render_homepage(request, notifications):
    return render(
        request, 'index.html', {
            'notifications': notifications,
            'recruiting': get_bool('RECRUITING', False)
        })
Exemple #9
0
def accept_webhook(request):
    auth_header = request.META.get('Authorization',
                                   request.META.get('HTTP_AUTHORIZATION', ''))

    username, password = extract_basic_auth(auth_header)

    if username != settings.WEBHOOK_USERNAME or \
            password != settings.WEBHOOK_PASSWORD:
        return HttpResponse('''Your credentials cannot be verified. 
        They should be placed in the header as &quot;Authorization-Bearer&quot;,  
        YOUR_APP_ID and &quot;Authorization-Token&quot; , YOUR_TOKEN"''',
                            status=401)

    if request.method != 'POST':
        return HttpResponse('Methods other than POST are not allowed',
                            status=405)

    if 'json' not in request.content_type.lower():
        return HttpResponse('Request expected in JSON', status=415)

    try:
        data = json.loads(request.body)
    except json.JSONDecodeError:
        return HttpResponse('Error decoding JSON body', status=400)

    course_id = data.get('course_section', None)
    if course_id is None:
        return HttpResponse('Course ID could not be extracted from response',
                            status=400)

    course_status = data.get('status', None)
    if course_status is None:
        return HttpResponse(
            'Course Status could not be extracted from response', status=400)

    course_term = data.get('term', None)
    if course_term is None:
        return HttpResponse('Course Term could not be extracted from response',
                            status=400)

    prev_status = data.get('previous_status', None)
    if prev_status is None:
        return HttpResponse(
            'Previous Status could not be extracted from response', status=400)

    should_send_alert = get_bool('SEND_FROM_WEBHOOK', False) and \
        course_status == 'O' and get_value('SEMESTER') == course_term

    u = record_update(course_id, course_term, prev_status, course_status,
                      should_send_alert, request.body)

    update_course_from_record(u)

    if should_send_alert:
        try:
            alert_for_course(course_id, semester=course_term, sent_by='WEB')
            return JsonResponse({'message': 'webhook recieved, alerts sent'})
        except ValueError:
            return JsonResponse({'message': 'course code could not be parsed'})

    else:
        return JsonResponse({'message': 'webhook recieved'})