Ejemplo n.º 1
0
def crn_search(request):
    if request.method == 'GET':
        context = None
    if request.method == 'POST':
        class_name = request.POST.get('classname')
        term = request.POST.get('term')
        if not term:
            term = settings.CURRENT_TERM 
        else:
            term = convert_term_to_code(term)
        if class_name:
            try:
                sub, cnbr = convert_classname(class_name)
                classes = get_all_secs_by_class(sub, cnbr, term)
            except ParserException as e:
                context = {'error': e.message}
                return render(request, 'crn_search.html', context)
            classes = sorted(classes, key = lambda cl: cl['class_time'].start_time)
            context = {
                'classes': classes,
                'error': ''
            }
        else:
            context = {'error': 'Please use right class name'}
    return render(request, 'crn_search.html', context)
Ejemplo n.º 2
0
def dashboard(request):
    user = request.user
    if request.method == 'GET':
        my_user = MyUser.objects.get(user=user)
        context = {
            'username' : user.username,
            'email' : user.email,
            'sections' :  my_user.sections.all()
        }
        return render(request, 'dashboard.html', context)
    elif request.method == 'POST':
        my_user = MyUser.objects.get(user=user)
        param = request.POST
        crn = param.get('crn')
        term = param.get('term')
        restrict = param.get('send_restrict')
        if not term:
            term = settings.CURRENT_TERM
        else:
            term = convert_term_to_code(term)
        context = {
            'email' : user.email,
            'sections' :  my_user.sections.all(),
            'error' : '',
        }
        try:
            sec = my_user.add_section(crn, term, restrict)
            if sec:
                msg = "You successfully subscribe section:%s \n" % sec
                try:
                    send_email.delay([my_user.user.email,], msg)
                except ImportError as e: 
                    pass
                    #send_email([my_user.user.email,], msg)
        except ParserException as e:
            context['error'] = e.message
            return render(request, 'dashboard.html', context) 
        return render(request, 'dashboard.html', context)
Ejemplo n.º 3
0
def seats_check(request, class_crn = None):
    result = None
    exists = True

    #GET
    if request.method == 'GET':
        term = request.GET.get('term', settings.CURRENT_TERM)
        try:
            term = convert_term_to_code(term)
        except:
            term = settings.CURRENT_TERM

        try:
            sec = Section.objects.get(crn = class_crn, term = term)
        except:
            exists = False

        if not exists:
            try:
                max_num, curr_num, name, code, number = get_all(class_crn, term)
                result = json.dumps(
                    {'code': 1,
                        'content': {'max_num' : max_num,
                                    'curr_num' : curr_num,
                                    'rem_num' : (max_num - curr_num),
                                    'name' : name,
                                    'code' : code,
                                    'number' : number,
                                   }
                    }
                )
            except Exception as e:
                result = json.dumps({'code' : 0, 'content': str(e)})
        else:
            max_num = sec.max_seats_num
            curr_num = sec.current_seats_num
            rem_num = sec.remain_seats_num
            name = sec.name
            code = sec.code
            number = sec.number
            result = json.dumps(
                {'code': 1,
                    'content': {'max_num' : max_num,
                                'curr_num' : curr_num,
                                'rem_num' : rem_num,
                                'name' : name,
                                'code' : code,
                                'number' : number,
                               }
                }
            )

    # POST
    elif request.method == 'POST':
        post_json = json.loads(request.body)
        term = post_json['term']
        if not term:
            term = settings.CURRENT_TERM
        crns = post_json['content']
        result = []
        for crn in crns:
            try:
                sec = Section.objects.get(crn = class_crn, term = term)
            except:
                exists = False
            if not exists:
                try:
                    max_num, curr_num, name, code, number = get_all(crn, term)
                    result += json.dumps(
                        {'code': 1,
                            'content': {'max_num' : max_num,
                                        'curr_num' : curr_num,
                                        'rem_num' : (max_num - curr_num),
                                        'name' : name,
                                        'code' : code,
                                        'number' : number,
                                       }
                        }
                    )
                except Exception as e:
                    result += json.dumps({'code' : 0, 'content': str(e)})
            else:
                max_num = sec.max_seats_num
                curr_num = sec.current_seats_num
                rem_num = sec.remain_seats_num
                name = sec.name
                code = sec.code
                number = sec.number
                result += json.dumps(
                    {'code': 1,
                        'content': {'max_num' : max_num,
                                    'curr_num' : curr_num,
                                    'rem_num' : rem_num,
                                    'name' : name,
                                    'code' : code,
                                    'number' : number,
                                   }
                    }
                )

    return HttpResponse(result, content_type="application/json")