コード例 #1
0
ファイル: views.py プロジェクト: haihua-sysu/onlinejudge
def editProfile(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            if request.user.username != form.cleaned_data['username']:
                return printError('you can not modify this profile')
            username = form.cleaned_data['username']
            password = form.cleaned_data['orginalpassword']
            user = auth.authenticate(username=username, password=password)
            if user == None:
                return printError('your orignal password is wrong')
            password1 = request.POST['password1']
            password2 = request.POST['password2']
            if len(password1) > 0:
                if len(password1) <= 8:
                    return printError('passowrd is too short')
                if password1 != password2:
                    return printError('two password not match')
                user.password = hashlib.sha1(password1).hexdigest()

            user.email = form.cleaned_data['email']
            handle = Handle.objects.get(user=user)
            handle.signature = form.cleaned_data['signature']
            handle.headurl = form.cleaned_data['headurl']
            handle.school = form.cleaned_data['school']
            handle.grade = form.cleaned_data['grade']
            handle.realname = form.cleaned_data['realname']
            handle.save()
            return HttpResponseRedirect("/handle/profile/%s" % user.username)
    else:
        handle = Handle.objects.get(user=request.user)
        context = {'handle': handle}
        return render_to_response('editprofile.html',
                                  context,
                                  context_instance=RequestContext(request))
コード例 #2
0
def addContest(request):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )
    form = ContestForm()
    errors = []
    courseid = 0
    if request.method == "POST":
        courseid = request.POST['courseid']
        form = ContestForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            courseid = data['courseid']
            course = get_object_or_404(Course, courseid=courseid)
            if not hasPrivilageAddContest(request.user, courseid):
                return HttpResponseRedirect("/")

            contest = Contest(title=data['title'],
                              creater=request.user,
                              start_time=data['start_time'],
                              end_time=data['end_time'],
                              courseid=course)
            contest.save()

            problemList = data['problem_list'].split(',')
            for p in problemList:
                temp = Problem.objects.filter(pid=p)
                if len(temp) == 0:
                    return printError("problem ** %d ** is not exists" %
                                      int(p))
                relation = ContestProblem.objects.filter(
                    cid=contest, pid=Problem.objects.get(pid=int(p)))
                if len(relation) == 0:
                    relation = ContestProblem(
                        cid=contest, pid=Problem.objects.get(pid=int(p)))
                    relation.save()

            return HttpResponseRedirect("/courselist/course/%d" %
                                        int(courseid))

    elif request.method == 'GET':
        try:
            courseid = request.GET['courseid']
        except:
            return printError('You should add contest in course page')

        if not hasPrivilageAddContest(request.user, courseid):
            return HttpResponseRedirect("/")

    course = get_object_or_404(Course, courseid=courseid)
    context = {"form": form, "errors": errors, 'course': course}
    return render_to_response("addcontest.html",
                              context,
                              context_instance=RequestContext(request))
コード例 #3
0
def changeUserStatus(request, username):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )
    try:
        user = User.objects.get(username=username)
        if user.is_superuser:
            return printError(
                'You have not permission to change an Admin\' status')
        user.is_active = not user.is_active
        user.save()
    except:
        return printError('An Error Occurrence')
    return HttpResponseRedirect("/administrate/viewuserlist")
コード例 #4
0
def editProblem(request, pid):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )

    pid = int(pid)
    problem = get_object_or_404(Problem, pid=pid)
    title = problem.title
    if request.method == "GET":
        tags = problem.tags.all()
        tags = [str(x) for x in tags]
        tags = ','.join(tags)
        return render_to_response("edit.html",
                                  locals(),
                                  context_instance=RequestContext(request))
    elif request.method == "POST":
        form = ProblemForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            problem.title = data['title']
            problem.description = data['problem_description']
            problem.input_description = data['input_description']
            problem.output_description = data['output_description']
            problem.input_sample = data['input_sample']
            problem.output_sample = data['output_sample']
            problem.hint = data['hint']
            problem.time_limit = data['time_limit']
            problem.memory_limit = data['memory_limit']
            problem.source = data['source']
            problem.tags.clear()
            problem.save()
            addTag(problem, data['tag'])

            inputfiles = request.FILES.getlist('inputfiles')
            outputfiles = request.FILES.getlist('outputfiles')
            addDataFile(pid, inputfiles, outputfiles)

            return HttpResponseRedirect("/administrate/viewproblemlist/")
        else:
            return printError('form is invalid')

    context = {"pid": pid, "form": form, "title": title}
    return render_to_response("edit.html",
                              context,
                              context_instance=RequestContext(request))
コード例 #5
0
def changeStatus(request, pid):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )
    problem = get_object_or_404(Problem, pid=pid)
    problem.is_showed = not problem.is_showed
    problem.save()
    return HttpResponseRedirect("/administrate/viewproblemlist/")
コード例 #6
0
def viewUserlist(request):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )

    handle = Handle.objects.all().order_by('-user')
    context = {"handle": handle}
    return render_to_response("viewuserlist.html",
                              context,
                              context_instance=RequestContext(request))
コード例 #7
0
ファイル: views.py プロジェクト: haihua-sysu/onlinejudge
def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("/")

    if 'next' in request.GET:
        REDIRECT_URL = request.GET['next']
    else:
        REDIRECT_URL = '/'

    form = LoginForm()
    errors = []
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user == None:
                return printError('username or password not match')

            if not (user.is_active or user.is_superuser):
                return printError(
                    'your handle was unactivated, please contact the administration'
                )

            if user is not None:
                auth.login(request, user)
                handle = Handle.objects.get(user=user)
                handle.save()
                return HttpResponseRedirect(REDIRECT_URL)
            else:
                errors.append('username and password not match')

    context = {'form': form, 'errors': errors}
    return render_to_response('login.html',
                              context,
                              context_instance=RequestContext(request))
コード例 #8
0
def viewProblemlist(request, page_id):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )
    #page_id = int(page_id)
    #if page_id <= 0: page_id = 1
    #min_pid = (page_id - 1) * 20
    #max_pid = min(page_id * 20, Problem.objects.count())
    #if min_pid > max_pid: min_pid = max_pid
    #problem = Problem.objects.filter(pid__gte = min_pid, pid__lte = max_pid).order_by("-pid")
    problemset = Problem.objects.all().order_by("-pid")
    context = {"problemset": problemset}
    context.update(csrf(request))
    return render_to_response("viewproblemlist.html",
                              context,
                              context_instance=RequestContext(request))
コード例 #9
0
def addCourse(request):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )
    errors = []
    form = CourseForm()
    if request.method == 'POST':
        form = CourseForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            course = Course(title=data['title'], creater=request.user)
            course.save()
            return HttpResponseRedirect('/courselist/')
    context = {'form': form, 'errors': errors}
    return render_to_response('addcourse.html',
                              context,
                              context_instance=RequestContext(request))
コード例 #10
0
def addProblem(request):
    if not request.user.is_superuser:
        return printError(
            'You have no permission to this page, please contact administration'
        )

    form = ProblemForm()
    errors = []
    if request.method == "POST":
        inputfiles = request.FILES.getlist('inputfiles')
        outputfiles = request.FILES.getlist('outputfiles')
        form = ProblemForm(request.POST)
        pid = Problem.objects.all().aggregate(Max('pid'))['pid__max']
        if pid == None:
            pid = 1000
        else:
            pid = pid + 1
        if form.is_valid():
            data = form.cleaned_data
            problem = Problem(
                pid=pid,
                title=data['title'],
                problem_description=data['problem_description'],
                input_description=data['input_description'],
                output_description=data['output_description'],
                input_sample=data['input_sample'],
                output_sample=data['output_sample'],
                hint=data['hint'],
                source=data['source'],
                time_limit=data['time_limit'],
                memory_limit=data['memory_limit'],
                is_showed=True,
                creater=request.user,
            )
            problem.save()

            addTag(problem, data['tag'])

            #data_dir = OJ_DATA_DIR + str(problem.pid)
            #cmd = "mkdir " + data_dir
            #os.system(cmd)
            #sample_input_file = data_dir + '/sample.in'
            #sample_output_file = data_dir + '/sample.out'
            #f = open(sample_input_file, 'w')
            #f.write(data['input_sample'])
            #f.close()
            #f = open(sample_output_file, 'w')
            #f.write(data['output_sample'])
            #f.close()

            cmd = "mkdir %s/%d" % (_settings.TESTDATA_DIR, pid)
            os.system(cmd)
            addDataFile(pid, inputfiles, outputfiles)

            return HttpResponseRedirect("/administrate/viewproblemlist")

    context = {"form": form, "errors": errors}
    context.update(csrf(request))
    return render_to_response("addproblem.html",
                              context,
                              context_instance=RequestContext(request))