Modernise, refactor profile code; remove unused files

This commit is contained in:
Anna Sidwell 2019-08-20 01:06:20 +02:00
parent 1e6359489e
commit 0d65c3c7b9
8 changed files with 18 additions and 49 deletions

View File

@ -1,2 +0,0 @@
# from django.contrib import admin
# Register your models here.

View File

@ -1,7 +0,0 @@
from django.contrib.auth import get_user_model
from django.forms import ModelForm
class UpdateProfile(ModelForm):
class Meta:
model = get_user_model()

View File

@ -1,2 +0,0 @@
# from django.db import models
# Create your models here.

View File

@ -1,6 +0,0 @@
{% extends "base_page.html" %}
Profile page
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>

View File

@ -1,2 +0,0 @@
# from django.test import TestCase
# Create your tests here.

View File

@ -1,11 +1,11 @@
from django.conf.urls import url
from django.urls import path
from . import views
app_name = "profiles"
urlpatterns = [
url(regex=r"^~redirect/$", view=views.UserRedirectView.as_view(), name="redirect"),
url(regex=r"^$", view=views.UserDetailView.as_view(), name="detail"),
url(regex=r"^~update/$", view=views.UserUpdateView.as_view(), name="update"),
path("", view=views.UserDetailView.as_view(), name="detail"),
path("~redirect", view=views.UserRedirectView.as_view(), name="redirect"),
path("~update", view=views.UserUpdateView.as_view(), name="update"),
]

View File

@ -1,15 +1,11 @@
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.conf import settings
from django.views.generic import DetailView, ListView, RedirectView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import DetailView
from django.views.generic import ListView
from django.views.generic import RedirectView
from django.views.generic import UpdateView
from apps.map.models import CaseStudy
@ -21,13 +17,13 @@ class UserDetailView(LoginRequiredMixin, DetailView):
slug_url_kwarg = "username"
def get_object(self):
return get_user_model().objects.get(username=self.request.user.username)
return self.model.objects.get(username=self.request.user.username)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Get case studies for this user
case_studies = CaseStudy.objects.filter(author=self.request.user.id)
case_studies = CaseStudy.objects.filter(author=self.request.user)
context["case_studies"] = case_studies
context["enable_edit"] = settings.FF_CAN_EDIT
@ -37,25 +33,17 @@ class UserDetailView(LoginRequiredMixin, DetailView):
class UserRedirectView(LoginRequiredMixin, RedirectView):
permanent = False
def get_redirect_url(self):
return reverse("profile:detail")
url = reverse_lazy("profile:detail")
class UserUpdateView(LoginRequiredMixin, UpdateView):
fields = ["first_name", "last_name"]
# we already imported User in the view code above, remember?
model = get_user_model()
# send the user back to their own page after a successful update
def get_success_url(self):
return reverse("profile:detail")
fields = ["first_name", "last_name"]
success_url = reverse_lazy("profile:detail")
def get_object(self):
# Only get the User record for the user making the request
return get_user_model().objects.get(username=self.request.user.username)
return self.model.objects.get(username=self.request.user.username)
class UserListView(LoginRequiredMixin, ListView):

View File

@ -1,8 +1,8 @@
from django.urls import include
from django.conf.urls import url
from django.urls import path
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.urls import include
from django.urls import path
from .views import LanguageDropdownView