Example #1
0
def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, user.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        registerLog('INFO','GET',user.username,'Login','AccountActivated',get_client_ip(request))
        return HttpResponseRedirect('/')
    else:
        return HttpResponse('Activation link is invalid!')
Example #2
0
def showProblemView(request, problem_id):
    problem_to_show = Question.objects.get(id=problem_id)
    creatorObject = User.objects.get(username=problem_to_show.creator)
    languages = Utils.fetchAvailableLanguages(None)
    registerLog('INFO', 'GET', request.user.username,
                'Problem', 'AccessProblemWithID_' + str(problem_id),
                get_client_ip(request))
    return render(
        request, 'problem.html', {
            'problem': problem_to_show,
            'languages': languages,
            'creatorObject': creatorObject
        })
Example #3
0
def loginView(request):
    
    username = request.POST['username']
    password = request.POST['pass']
    
    print(username," ",password)
    user = auth.authenticate(username = username, password = password)
    if user is not None and user.is_active:
        auth.login(request,user)
        registerLog('INFO','POST',user.username,'Login','LoginSuccessful',get_client_ip(request))
        return HttpResponseRedirect('/dashboard/')
    else:
        allusers = User.objects.all()
        whichMessageToShow = 1
        for user in allusers:
            if user.username == username and user.password!=password:
                messages.info(request,'Password did not match')
                whichMessageToShow = 2
                registerLog('ERROR','POST',user.username,'Login','IncorrectPassword',get_client_ip(request))
                break
        if whichMessageToShow == 1:
            registerLog('ERROR','POST',username,'Login','UsernameDoesNotExist',get_client_ip(request))
            messages.info(request,'Username does not exist')
        return HttpResponseRedirect('/')
Example #4
0
def problemsView(request):
    questions = Question.objects.all()
    allEntriesInUserPlaylist = UserPlaylist.objects.all()
    categories = []
    for entry in allEntriesInUserPlaylist:
        if entry.userId_id == request.user.id:
            categories.append(entry.playlistCategory)
    all_questions = []
    registerLog('INFO', 'GET', request.user.username, 'Problem',
                'ViewProblemList', get_client_ip(request))
    for ques in questions:
        if ques.access == 1 or ques.creator == request.user.username:
            all_questions.append(ques)
    return render(request, 'problems.html', {
        'all_problems': all_questions,
        'categories': categories
    })
Example #5
0
def submitProblemView(request, problem_id):
    if request.method == 'POST':
        usercode = request.POST['code']
        print(usercode)
        language = request.POST['language']
        registerLog('INFO', 'POST', request.user.username, 'Problem',
                    'SubmittedProblemWithID_' + str(problem_id),
                    get_client_ip(request))
        registerLog('INFO', 'POST', request.user.username, 'Problem', language,
                    get_client_ip(request))
        registerLog('INFO', 'POST', request.user.username, 'Problem',
                    'Problem' + str(problem_id) + '_Language' + language,
                    get_client_ip(request))
        problem = Question.objects.get(id=problem_id)
        utils = Utils()
        x = utils.submitProblem(request.user, usercode, language, problem)
        registerLog('INFO', 'POST', request.user.username, 'Problem',
                    'JudgeExecutedForProblemWithID_' + str(problem_id),
                    get_client_ip(request))
        problem.totalAttempts = problem.totalAttempts + 1
        if x[len(x) - 1][0] == "AC":
            problem.successfulAttempts = problem.successfulAttempts + 1
            registerLog('INFO', 'POST', request.user.username, 'Problem',
                        'ForProblemWithID_' + str(problem_id) + 'verdict_AC',
                        get_client_ip(request))
        problem.save()
        print(x)
        if (x[0] == 'CE'):
            temp = []
            errorMessage = " "
            temp.append("Compilation Error")
            registerLog('INFO', 'POST', request.user.username, 'Problem',
                        'ForProblemWithID_' + str(problem_id) + 'verdict_CE',
                        get_client_ip(request))
            for messageString in x[4]:
                errorMessage = errorMessage + messageString + "    "
            temp.append(errorMessage)
            x = []
            x.append(temp)
        languages = Utils.fetchAvailableLanguages(None)
        return render(
            request, 'problem.html', {
                'problem': problem,
                'languages': languages,
                'results': x,
                'usercode': usercode,
                'languageUsed': language,
                'resultLength': len(x)
            })
Example #6
0
def createProblemView(request):
    if request.method == 'POST':
        exists = 0
        fileValidity = True
        yml_file = request.FILES['uploadFiles']
        for f in request.FILES.getlist('uploadFiles'):
            filename = f.name
            reqname = "init.yml"
            if filename == reqname:
                print()
            else:
                registerLog('ERROR', 'POST', request.user.username, 'Problem',
                            'CreateProblem_Invalid_initYAML',
                            get_client_ip(request))
                print("not yml")

        for f in request.FILES.getlist('uploadedFiles'):
            filename = f.name
            length = len(filename)
            if filename[length -
                        1] != 't' or filename[length - 2] != 'x' or filename[
                            length - 3] != 't' or filename[length - 4] != '.':
                fileValidity = False
                registerLog('ERROR', 'POST', request.user.username, 'Problem',
                            'CreateProblem_Invalid_testCase',
                            get_client_ip(request))
                break
        if fileValidity == True:
            problem_code = request.POST['problemCode']
            problem_name = request.POST['problemName']
            problem_statement = request.POST['problemStatement']
            time_limit = request.POST['timeLimit']
            memory_limit = request.POST['memoryLimit']
            problemType = request.POST['problemType']
            print(int(problemType))
            if int(problemType) == 1:
                marking = 1
            else:
                marking = 2
            visibility = request.POST['visibility']
            print(int(visibility))
            if int(visibility) == 1:
                access = 1
            else:
                access = 2
            creator = request.user.username
            all_questions = Question.objects.all()
            for ques in all_questions:
                if ques.problemCode == problem_code:
                    exists = 1
                    break
            if exists == 1:
                return render(request, 'createproblem.html')

            all_files = request.FILES.getlist('uploadedFiles')
            utils = Utils()
            utils.saveProblem(yml_file, problem_code, all_files)
            registerLog('INFO', 'POST', request.user.username, 'Problem',
                        'CreateProblemSuccessful', get_client_ip(request))
            registerLog('INFO', 'POST', request.user.username,
                        'Problem', 'CreateProblem_' + problem_code,
                        get_client_ip(request))
            newProblem = Question(problemCode=problem_code,
                                  problemName=problem_name,
                                  problemStatement=problem_statement,
                                  timeLimit=time_limit,
                                  memoryLimit=memory_limit,
                                  marking=marking,
                                  access=access,
                                  creator=creator,
                                  editorialist=creator,
                                  totalAttempts=0,
                                  successfulAttempts=0)
            newProblem.save()
            return HttpResponseRedirect('/dashboard/problems/')
        else:
            print("Not valid input cases")

    return render(request, 'createproblem.html')
Example #7
0
def saveUserView(request):
    
    if(request.method == 'POST'):
        
        exists = False
        username = request.POST['username']
        firstname = request.POST['firstname']
        lastname = request.POST['lastname']

        email = request.POST['email']
        password1 = request.POST['password1']
        password2 = request.POST['password2']

        # Confirming passwords
        if(password1 != password2):
            messages.info(request,'Password did not match')
            exists = True
            registerLog('ERROR','POST',username,'Login','RegisterationPasswordMismatch',get_client_ip(request))

        # Checking if username or e-mail is already taken
        all_users = User.objects.all()
        for user in all_users:
            # Unique username
            if user.username == username:
                exists = True
                messages.info(request,'Username Taken')
                registerLog('ERROR','POST',username,'Login','RegisterationUsernameTaken',get_client_ip(request))
                break
            # Unique e-mail
            if user.email == email:
                exists = True
                registerLog('ERROR','POST',username,'Login','RegisterationEMailTaken',get_client_ip(request))
                messages.info(request,'Username with this e-mail account already exists')
                break

        #loop through all the users if at any point username matches set exists=1 and break

        if exists == False:
            mode = 0o777
            directory = username
            parentDirPath = os.path.dirname(__file__).rsplit('/',1)[0]
            parent_dir = os.path.join(parentDirPath,'media/submittedFiles')
            path = os.path.join(parent_dir, directory)
            os.mkdir(path,mode)
            shutil.copy("static/images/f.jpg",path)
            renamedPath = path + "/" + username + ".jpg"
            path=path + "/f.jpg"
            os.rename(path,renamedPath)
            new_user = User.objects.create_user(username=username,first_name=firstname,last_name=lastname,email=email,password=password1)
            registerLog('INFO','POST',username,'Login','NewUserCreated',get_client_ip(request))
            # Setting active status for the user false for the email verification
            new_user.is_active = False
            new_user.save()

            # E-mail Verfication 
            current_site = get_current_site(request)
            mail_subject = 'Activate your CallOJ account'
            message = render_to_string('verificationEmail.html', {
                    'user': new_user,
                    'domain': current_site.domain,
                    'uid':urlsafe_base64_encode(force_bytes(new_user.pk)).decode(),
                    'token':account_activation_token.make_token(new_user),
                })

            reciever_email = email
            msg = EmailMessage(mail_subject, message, to=[reciever_email])
            msg.send()
            messages.success(request, f'Your account has been created ! You are now able to log in')

            return HttpResponse('Please confirm your email address to complete the registration')
        
        else:
            # Redirects to registeration page in case of invalid inputs
            return HttpResponseRedirect('/registration/')
Example #8
0
def logoutView(request):
    registerLog('INFO','GET',request.user.username,'Login','UserLogsOut',get_client_ip(request))
    auth.logout(request)
    return HttpResponseRedirect('/')