def dashboard(request): # authenticate if not check_logged_in(request): return redirect('access_denied') # get user user = User.objects.get(id=request.session.get("user_id")) # get requests context = {} if user.role == "user": context = { 'full_name': User.objects.get(id=request.session['user_id']).fullname, 'requests': user.request_set.all(), 'active_requests': user.request_set.filter(active="True"), 'inactive_requests': user.request_set.filter(active="False"), } elif user.role == "admin": context = { 'full_name': User.objects.get(id=request.session['user_id']).fullname, 'requests': Request.objects.all(), 'active_requests': Request.objects.filter(active="True"), 'inactive_requests': Request.objects.filter(active="False"), } return render(request, "request/dashboard.html", context)
def about(request): if check_logged_in(request): context = { 'full_name': User.objects.get(id=request.session['user_id']).fullname } return render(request, "request/about.html", context) else: return redirect('access_denied')
def delete_request(request): # authenticate if not check_logged_in(request): return redirect('access_denied') # get user user = User.objects.get(id=request.session.get("user_id")) # find the existing request request_object = Request.objects.get(id=request.POST.get('request_id', None)) # delete existing request request_object.delete() return redirect('dashboard')
def edit_request(request): # authenticate if not check_logged_in(request): return redirect('access_denied') # get user user = User.objects.get(id=request.session.get("user_id")) # get the existing request request_object = Request.objects.get(id=request.GET.get('request_id', None)) if request.method == "GET": # Populate the form context = { 'group_id': user.group.name, 'tags': [x.name for x in Tag.objects.all()], 'full_name': User.objects.get(id=request.session['user_id']).fullname, 'request': request_object, 'request_tags_names': [x.name for x in request_object.tags.all()] } return render(request, "request/edit_request.html", context) elif request.method == "POST": try: # Update all request fields request_object.title = request.POST.get("request-title", None) request_object.status = request.POST.get("requestStatus", None) request_object.request_message = request.POST.get("request-message", None) request_object.response_message = request.POST.get("response-message", None) new_tags = [Tag.objects.get(name=tag_name) for tag_name in request.POST.getlist("details-tags", None)] request_object.tags.set(new_tags) request_object.clean() request_object.save() return redirect('dashboard') except ValidationError as error: context = { 'error_message': error.message, } return render(request, "request/edit_request.html", context)
def create_request(request): # authenticate if not check_logged_in(request): return redirect('access_denied') # get user user = User.objects.get(id=request.session.get("user_id")) # handle new requests if request.method == 'GET': context = { 'group_id': user.group.name, 'tags': [x.name for x in Tag.objects.all()], } return render(request, "request/create_request.html", context) if request.method == 'POST': try: new_request = Request( status=request.POST.get("requestStatus", None), title=request.POST.get("request-title", None), request_message=request.POST.get("request-message", None), requester=user, group=user.group, response_message=request.POST.get("response-message", None), ) new_request.clean() new_request.save() new_tags = [Tag.objects.get(name=tag_name) for tag_name in request.POST.getlist("details-tags", None)] new_request.tags.set(new_tags) return redirect('dashboard') except ValidationError as error: context = { 'group_id': user.group.name, 'tags': [x.name for x in Tag.objects.all()], 'error_message': error.message } return render(request, "request/create_request.html", context)
def logout(request): if check_logged_in(request): do_logout(request) return render(request, "request/logout.html") else: return redirect('access_denied')