Пример #1
0
def current_group(request, user, course, week=None):
    """ Returns the current group for this course and assistant 
    
    Arguments:
    user        -- user making the request
    course      -- id of the course used
    week        -- (optional) week for which the data is requested. If ommited 
                   the current week is used

    """
    assistant = request.assistant
    now = datetime.now().time()  # Atentie: nu da valori corecte (timezone)

    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error": "No such course"}, failed=True)

    if week is None:
        week = get_week(course)
    else:
        week = int(week)

    if week < 1 or week > course.max_weeks:
        return json_response({"error": "The selected week is invalid"},
                             failed=True)

    today = datetime.today().weekday()
    # get all the where the user is an assistant for this course
    for group in assistant.groups.filter(course=course):
        for act in group.activity_set.all():
            # see if the group activity is taking place now
            start = time(act.time_hour_start, act.time_minute_start)
            end = time(act.time_hour_end, act.time_minute_end)

            if today == act.day and start <= now and now <= end:
                # This is the activity I have to return
                return _group(request, course, group, act, week)

    return json_response({"error": "no current group"}, failed=True)
Пример #2
0
def group(request, user, name, course, activity_id, week=None):
    """ Returns a certain group from a certain course 

    Arguments:
    user        -- user making the request
    name        -- name of the group requested
    course      -- id of the course used
    activity_id -- id of the activity for which the group data is requested
    week        -- (optional) week for which the data is requested. If ommited 
                   the current week is used
    """

    assistant = request.assistant

    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error": "No such course"}, failed=True)

    # week checks
    if week is None:
        week = get_week(course)
    else:
        week = int(week)

    if week < 1 or week > course.max_weeks:
        return json_response({"error": "The selected week is invalid"},
                             failed=True)

    try:
        act = Activity.objects.get(id=activity_id)
    except Activity.DoesNotExist:
        return json_response({"error": "No such activity"}, failed=True)

    try:
        group = Group.objects.get(name=name, course=course)
    except Group.DoesNotExist:
        return json_response({"error": "No such group"}, failed=True)

    return _group(request, course, group, act, week)
Пример #3
0
def current_group(request, user, course, week = None):
    """ Returns the current group for this course and assistant 
    
    Arguments:
    user        -- user making the request
    course      -- id of the course used
    week        -- (optional) week for which the data is requested. If ommited 
                   the current week is used

    """
    assistant = request.assistant
    now = datetime.now().time() # Atentie: nu da valori corecte (timezone)
    
    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error":"No such course"}, failed = True)
    
    if week is None:
        week = get_week(course)
    else:
        week = int(week)
    
    if week < 1 or week > course.max_weeks:
        return json_response({"error":"The selected week is invalid"}, failed = True)
    
    
    today = datetime.today().weekday()
    # get all the where the user is an assistant for this course
    for group in assistant.groups.filter(course = course):
        for act in group.activity_set.all():
            # see if the group activity is taking place now
            start = time(act.time_hour_start, act.time_minute_start)
            end = time(act.time_hour_end, act.time_minute_end)
    
            if today == act.day and start <= now and now <= end:
                # This is the activity I have to return
                return _group(request, course, group, act, week)
        
    return json_response({"error":"no current group"}, failed = True)
Пример #4
0
def group(request, user, name, course, activity_id, week = None):
    """ Returns a certain group from a certain course 

    Arguments:
    user        -- user making the request
    name        -- name of the group requested
    course      -- id of the course used
    activity_id -- id of the activity for which the group data is requested
    week        -- (optional) week for which the data is requested. If ommited 
                   the current week is used
    """

    assistant = request.assistant
    
    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error":"No such course"}, failed = True)
    
    # week checks
    if week is None:
        week = get_week(course)
    else:
        week = int(week)
   
    if week < 1 or week > course.max_weeks:
        return json_response({"error":"The selected week is invalid"}, failed = True)
    
    try:
        act = Activity.objects.get(id = activity_id)
    except Activity.DoesNotExist:
        return json_response({"error":"No such activity"}, failed = True)
    
    try:
        group = Group.objects.get(name=name, course = course)
    except Group.DoesNotExist:
        return json_response({"error": "No such group"}, failed = True)

    return _group(request, course, group, act, week)
Пример #5
0
def post_data(request):
    """ Handles the POST requests 

    Expects the folowing in POST data:
    user        -- user making the post
    course      -- current course id
    hash        -- hash to check validity of data
    type        -- type of post request (currently only 'group' is supported)
    contents    -- JSON encoded dictionary containing:
        week        -- optional week number, otherwise current week is used
        name        -- group name
        activity_id -- current activity id for which the grades are saved
        students    -- list of dictionary elements containing
            id          -- id of the student
            grade       -- new grade of the student
    """
    try:
        user = int(request.POST['user'])
        hash = request.POST['hash']
        course = int(request.POST['course'])
    except:
        return json_response({"error": "Malformed post request"}, failed=True)

    try:
        assistant = Assistant.objects.get(pk=user)
    except Assistant.DoesNotExist:
        return json_response({"error": "No such user"}, failed=True)

    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error": "No such course"}, failed=True)

    try:
        data = json.loads(request.POST['contents'])
        if request.POST['type'] == 'group':
            try:
                week = int(data['week'])
            except:
                week = get_week(course)

            hash_list = map(str, [
                request.POST['user'], request.POST['course'],
                request.POST['type'], request.POST['contents'], assistant.code
            ])
            post_check_hash = ''.join(hash_list)

            computed_hash = hashlib.md5(post_check_hash).hexdigest()
            if computed_hash != hash:
                return json_response(
                    {
                        "error":
                        "Invalid check hash, expected: %s got %s" %
                        (computed_hash, hash)
                    },
                    failed=True)

            if week > course.max_weeks:
                return json_response(
                    {
                        "error":
                        "The selected week is larger than the number of weeks for this course"
                    },
                    failed=True)

            if week in course.inactive_as_list:
                return json_response(
                    {"error": "This week is during the holiday"}, failed=True)

            try:
                act = Activity.objects.get(id=int(data['activity_id']))
            except Activity.DoesNotExist:
                return json_response({"error": "Activity not found"},
                                     failed=True)
            #now we try to save the data for each student
            for student in data['students']:
                try:
                    current_student = Student.objects.get(
                        id=int(student['id']))
                    student['grade'] = float(student['grade'])
                    attendance, created = Attendance.objects.get_or_create(
                        course=course,
                        student=current_student,
                        activity=act,
                        week=week,
                        defaults={'grade': student['grade']})
                    attendance.grade = student['grade']
                    attendance.save()
                except Student.DoesNotExist:
                    return json_response({"error": "Student not found"},
                                         failed=True)
            #if we succeded then we return status ok
            return json_response({})
        else:
            return json_response({"error": "Wrong query type"}, failed=True)
    except Exception as e:
        return json_response({"error": "Malformed query %s" % e}, failed=True)
Пример #6
0
def post_data(request):
    """ Handles the POST requests 

    Expects the folowing in POST data:
    user        -- user making the post
    course      -- current course id
    hash        -- hash to check validity of data
    type        -- type of post request (currently only 'group' is supported)
    contents    -- JSON encoded dictionary containing:
        week        -- optional week number, otherwise current week is used
        name        -- group name
        activity_id -- current activity id for which the grades are saved
        students    -- list of dictionary elements containing
            id          -- id of the student
            grade       -- new grade of the student
    """
    try:
        user = int(request.POST['user'])
        hash = request.POST['hash']
        course = int(request.POST['course'])
    except:
        return json_response({"error":"Malformed post request"}, failed = True)

    try:
        assistant = Assistant.objects.get(pk=user)
    except Assistant.DoesNotExist:
        return json_response({"error":"No such user"}, failed = True)
        
    try:
        course = Course.objects.get(id=course)
    except Course.DoesNotExist:
        return json_response({"error":"No such course"}, failed = True)
    
    try:
        data = json.loads(request.POST['contents'])
        if request.POST['type'] == 'group':
            try:
                week = int(data['week'])
            except:
                week = get_week(course)
            
            hash_list = map(str,[request.POST['user'], request.POST['course'], request.POST['type'], request.POST['contents'], assistant.code])
            post_check_hash =''.join(hash_list)

            computed_hash = hashlib.md5(post_check_hash).hexdigest()
            if computed_hash != hash:
                return json_response({"error":"Invalid check hash, expected: %s got %s" % (computed_hash, hash)}, failed = True)

            if week > course.max_weeks:
                return json_response({"error":"The selected week is larger than the number of weeks for this course"}, failed = True)
            
            if week in course.inactive_as_list:
                return json_response({"error":"This week is during the holiday"}, failed = True)
            
    
            try:
                act = Activity.objects.get(id = int(data['activity_id']))
            except Activity.DoesNotExist:
                return json_response({"error":"Activity not found"}, failed = True)
            #now we try to save the data for each student
            for student in data['students']:
                try: 
                    current_student = Student.objects.get(id = int(student['id']))
                    student['grade'] = float(student['grade'])
                    attendance, created = Attendance.objects.get_or_create(course = course, student = current_student, activity = act, week = week, defaults={'grade': student['grade']})
                    attendance.grade = student['grade']
                    attendance.save()
                except Student.DoesNotExist:
                    return json_response({"error":"Student not found"}, failed = True)
            #if we succeded then we return status ok
            return json_response({})
        else:
            return json_response({"error":"Wrong query type"}, failed = True)
    except Exception as e:
        return json_response({"error":"Malformed query %s" % e}, failed = True)