def edit_profile(request, username): """Modifies user data and redirects to user profile upon success.""" user = get_object_or_404(UserModel, username__iexact=username) userdata = user.user_data if not can_modify(request, user.id): raise PermissionDenied if request.method == "POST": form1 = EditUserForm(request.POST, instance=user) form2 = EditUserDataForm( request.POST, request.FILES, instance=userdata ) if form1.is_valid() and form2.is_valid(): form1.save() form2.save() return redirect("profile", username=user) else: form1 = EditUserForm(instance=user) form2 = EditUserDataForm(instance=userdata) return render(request, "portfolio/edit_profile.html", context={ "form1": form1, "form2": form2, "userdata": userdata })
def messages(request, username): """Returns user messages. Uses get_messages() method of Userdata model instead of accessing messages directly.""" userdata = get_object_or_404( UserData, user=UserModel.objects.get(username__iexact=username) ) if not can_modify(request, userdata.id): raise PermissionDenied messages = userdata.get_messages() return render(request, "portfolio/messages.html", context={ "userdata": userdata, "messages": messages })
def del_certificate(request, pk): """Deletes giving certificate.""" cert = get_object_or_404(Certificate, pk=pk) if not can_modify(request, cert.user.id): raise PermissionDenied if request.method == "POST": cert.delete() return redirect("certificates", request.user) return render(request, "portfolio/confirm.html", context={ "title": "Delete Certificate", "userdata": cert.user, "desc": "You are about to delete this certificate with title: " f"<strong>{cert.title}</strong>. Proceed?", "btn_value": "Yes Delete!" })
def del_project(request, pk): """Deletes giving project.""" project = get_object_or_404(Project, pk=pk) if not can_modify(request, project.user.id): raise PermissionDenied if request.method == "POST": project.delete() return redirect("projects", request.user) return render(request, "portfolio/confirm.html", context={ "title": "Delete Project", "userdata": project.user, "desc": "You are about to delete this project with title: <strong>" f"{project.title}</strong>. Proceed?", "btn_value": "Yes Delete!" })
def del_message(request, pk): """Deletes giving message.""" msg = get_object_or_404(Message, pk=pk) if not can_modify(request, msg.user.id): raise PermissionDenied if request.method == "POST": msg.delete() return redirect("messages", request.user) return render(request, "portfolio/confirm.html", context={ "title": "Delete Message?", "userdata": msg.user, "desc": "You are about to delete this message from: <strong>" f"{msg.sender}</strong>. Proceed?", "btn_value": "Yes Delete!" })
def del_experience(request, pk): """Deletes giving experience.""" exp = get_object_or_404(Experience, pk=pk) if not can_modify(request, exp.user.id): raise PermissionDenied if request.method == "POST": exp.delete() return redirect("experiences", request.user) return render(request, "portfolio/confirm.html", context={ "title": "Delete Experience", "userdata": exp.user, "desc": "You are about to delete this experience with title: " f"<strong>{exp.title}</strong>. Proceed?", "btn_value": "Yes Delete!" })
def edit_certificate(request, pk): """View for modifying given certificate.""" cert = get_object_or_404(Certificate, pk=pk) userdata = cert.user if not can_modify(request, cert.user.id): raise PermissionDenied if request.method == "POST": form = CertificateForm(request.POST, request.FILES, instance=cert) if form.is_valid(): form.save() return redirect( f"""{reverse( 'certificates', kwargs={'username': userdata} )}#cert-{cert.pk}""" ) else: form = CertificateForm(instance=cert) return render(request, "portfolio/add.html", context={ "form": form, "userdata": userdata, "title": "Edit Certificate", "desc": f"Modify this certificate; {cert.title}", "btn_value": "Save Changes" })
def edit_experience(request, pk): """View for modifying given experience.""" exp = get_object_or_404(Experience, pk=pk) userdata = exp.user if not can_modify(request, exp.user.id): raise PermissionDenied if request.method == "POST": form = ExperienceForm(request.POST, request.FILES, instance=exp) if form.is_valid(): form.save() return redirect( f"""{reverse( 'experiences', kwargs={'username': userdata} )}#exp-{exp.pk}""" ) else: form = ExperienceForm(instance=exp) return render(request, "portfolio/add.html", context={ "form": form, "userdata": userdata, "title": "Edit Experience", "desc": f"Modify this experience; {exp.title}", "btn_value": "Save Changes" })
def edit_project(request, pk): """View for modifying given project.""" project = get_object_or_404(Project, pk=pk) userdata = project.user if not can_modify(request, project.user.id): raise PermissionDenied if request.method == "POST": form = ProjectForm(request.POST, request.FILES, instance=project) if form.is_valid(): form.save() return redirect( f"""{reverse( 'projects', kwargs={'username': userdata} )}#pro-{project.pk}""" ) else: form = ProjectForm(instance=project) return render(request, "portfolio/add.html", context={ "form": form, "userdata": userdata, "title": "Edit Project", "desc": f"Modify this project; {project.title}", "btn_value": "Save Changes" })