def modify_task(request): """ This function modifies the task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): task_id = request.GET.get("task_id") if task_id is not None: task = models.get_task(task_id) task_form = forms.CreateTaskForm( initial={ 'task_id': task_id, 'task_type': task.task_type, 'description': task.description, 'deadline': task.deadline }) # task_form.fields['task_id'].widget.attrs['disabled'] = True task_form.fields['task_id'].widget.attrs['readonly'] = True user = models.get_user(handle) context = {'modify_task_form': task_form, 'user': user} return render(request, 'janta/modify-task.html', context) return redirect("/janta/index/") return redirect("/janta/login/") if request.method == 'POST': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): form = forms.CreateTaskForm(request.POST) message = "Invalid Form please fill again" if form.is_valid(): fd = form.cleaned_data status, message = viewhelper.validate_modify_task(fd) if status: models.modify_task(task_id=fd.get("task_id"), owner=handle, description=fd.get("description"), task_type=fd.get("task_type"), deadline=fd.get("deadline")) return redirect('/janta/moderator/') form.fields['task_id'].widget.attrs['readonly'] = True user = models.get_user(handle) context = { 'modify_task_form': form, 'message': message, 'user': user } return render(request, 'janta/modify-task.html', context) return redirect("/janta/login/")
def admin_del_moderator(request): """ This function is used to delete the moderator by admin. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'POST': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_admin(user_type): user = models.get_user(handle=handle) fd = request.POST del_moderator_handle = fd.get("del_moderator_handle") message = "Invalid handle" if del_moderator_handle is not None: message = "Moderator does not exists" del_moderator = models.get_user(del_moderator_handle) if del_moderator is not None and viewhelper.is_moderator( del_moderator.user_type): models.admin_del_moderator(handle=del_moderator_handle) message = "Moderator Deleted" context = {'user': user, 'message': message} return render(request, 'janta/admin.html', context) return redirect("/janta/index/") return redirect("/janta/login/")
def login(request): """ This function deals with the login of user by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'POST': form = forms.LoginForm(request.POST) message = 'Invalid form please try again' if form.is_valid(): login_data = form.cleaned_data handle = login_data.get("handle") password = login_data.get("password") status, message = viewhelper.validate_login(handle, password) if status: user = models.get_user(handle) viewhelper.set_session(request, user) if viewhelper.is_admin(user.user_type): return redirect("/janta/admin/") if viewhelper.is_moderator(user.user_type): return redirect("/janta/moderator/") if viewhelper.is_user(user.user_type): return redirect("/janta/user/") message = 'Session creation failed please try again' context = {'login_form': form, 'message': message} return render(request, 'janta/login.html', context) elif request.method == 'GET': # do not redirect here otherwise it will fall in loop or change url pattern instead form = forms.LoginForm() context = {'login_form': form} return render(request, 'janta/login.html', context)
def moderator_home(request): """ This function moderates the form request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): recent_tasks = models.get_my_tasks(owner=handle, recent=True) user = models.get_user(handle) assign_task_form = forms.AddUserForm() delete_task_form = forms.DeleteTaskForm() remove_user_form = forms.RemoveUserForm() context = { 'recent_tasks': recent_tasks, 'user': user, 'add_user_form': assign_task_form, 'delete_task_form': delete_task_form, 'remove_user_form': remove_user_form } return render(request, 'janta/moderator.html', context) return redirect("/janta/index/") return redirect("/janta/login/")
def reject_submission(request): """ This function rejects the submitted task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): task_id = request.GET.get("task_id") user_handle = request.GET.get("handle") if task_id is not None and user_handle is not None: task = models.get_task(task_id) user_task_map = models.get_user_task_map( task_id=task_id, handle=user_handle) if task is not None and task.owner == handle and user_task_map is not None: models.reject_submission(task_id=task_id, handle=user_handle) return redirect('/janta/task-status/?task_id=' + task_id) return redirect("/janta/index/") return redirect("/janta/login/")
def task_status(request): """ This function keeps track of the status of task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): task_id = request.GET.get("task_id") if task_id is not None: task = models.get_task(task_id) if task is not None and task.owner == handle: user = models.get_user(handle) sub_status = models.get_submission_status( task_id=task_id, moderator=handle) submissions = models.get_not_accepted_submissions( task_id=task_id) context = { 'task': task, 'user': user, 'sub_status': sub_status, 'submissions': submissions } return render(request, 'janta/task-status.html', context) return redirect("/janta/index/") return redirect("/janta/login/")
def create_task(request): """ This function creates the task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): task_form = forms.CreateTaskForm() user = models.get_user(handle) context = {'create_task_form': task_form, 'user': user} return render(request, 'janta/create-task.html', context) return redirect("/janta/index/") return redirect("/janta/login/") if request.method == 'POST': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): form = forms.CreateTaskForm(request.POST) message = "Invalid Form please fill again" if form.is_valid(): fd = form.cleaned_data status, message = viewhelper.validate_create_task(fd) if status: models.insert_task(task_id=fd.get("task_id"), owner=handle, description=fd.get("description"), task_type=fd.get("task_type"), deadline=fd.get("deadline")) return redirect('/janta/moderator/') context = {'create_task_form': form, 'message': message} return render(request, 'janta/create-task.html', context) return redirect("/janta/login/")
def my_tasks(request): """ This function handles the functionality of the task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'GET': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): tasks = models.get_my_tasks(owner=handle) user = models.get_user(handle) context = {'tasks': tasks, 'user': user} return render(request, 'janta/my-tasks.html', context) return redirect("/janta/index/") return redirect("/janta/login/") if request.method == 'POST': return redirect("/janta/index/")
def delete_task(request): """ This function deletes the task by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'POST': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): delete_task_form = forms.DeleteTaskForm(request.POST) recent_tasks = models.get_my_tasks(owner=handle, recent=True) user = models.get_user(handle) assign_task_form = forms.AddUserForm() delete_form_message = "Invalid Form please fill again" if delete_task_form.is_valid(): fd = delete_task_form.cleaned_data status, delete_form_message = viewhelper.validate_delete_task( moderator=handle, form_data=fd) if status: models.delete_task(task_id=fd.get("task_id")) delete_form_message = "Task Deleted" delete_task_form = forms.DeleteTaskForm() recent_tasks = models.get_my_tasks(owner=handle, recent=True) context = { 'recent_tasks': recent_tasks, 'user': user, 'add_user_form': assign_task_form, 'delete_task_form': delete_task_form, 'delete_form_message': delete_form_message } return render(request, 'janta/moderator.html', context) return redirect('/janta/index/') return redirect("/janta/login/")
def remove_user(request): """ This function removes the user by request. :type request: http request :param request: the request sent for the task :return: redirecting to the page according to http request """ if request.method == 'POST': if request.session.keys(): handle = request.session.get("handle") user_type = request.session.get("user_type") if viewhelper.is_moderator(user_type): add_user_form = forms.AddUserForm() delete_task_form = forms.DeleteTaskForm() remove_user_form = forms.RemoveUserForm(request.POST) remove_user_message = "Invalid Form please fill again" if remove_user_form.is_valid(): fd = remove_user_form.cleaned_data status, remove_user_message, user_handle = viewhelper.validate_remove_user( form_data=fd, moderator=handle) if status: models.remove_user(user=user_handle, moderator=handle) remove_user_form = forms.RemoveUserForm() remove_user_message = "Handle removed" recent_tasks = models.get_my_tasks(owner=handle, recent=True) user = models.get_user(handle) context = { 'add_user_form': add_user_form, 'remove_user_message': remove_user_message, 'recent_tasks': recent_tasks, 'user': user, 'delete_task_form': delete_task_form, 'remove_user_form': remove_user_form } return render(request, 'janta/moderator.html', context) return redirect("/janta/index/") return redirect("/janta/login/")