Add user-based case study listing

This commit is contained in:
Anna Sidwell 2019-03-04 23:39:27 +11:00
parent 57ac8eab86
commit eb7eabb7c7
2 changed files with 42 additions and 15 deletions

View File

@ -9,6 +9,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from apps.map.models import CaseStudy
class UserDetailView(LoginRequiredMixin, DetailView): class UserDetailView(LoginRequiredMixin, DetailView):
model = get_user_model() model = get_user_model()
@ -19,6 +21,15 @@ class UserDetailView(LoginRequiredMixin, DetailView):
def get_object(self): def get_object(self):
return get_user_model().objects.get(username=self.request.user.username) return get_user_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)
context['case_studies'] = case_studies
return context
class UserRedirectView(LoginRequiredMixin, RedirectView): class UserRedirectView(LoginRequiredMixin, RedirectView):
permanent = False permanent = False

View File

@ -7,7 +7,6 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
@ -15,23 +14,40 @@
{% if object.first_name and object.last_name %} {% if object.first_name and object.last_name %}
<h2>{{ object.first_name }} {{ object.last_name }} <i>({{ object.username }})</i></h2> <h2>{{ object.first_name }} {{ object.last_name }} <i>({{ object.username }})</i></h2>
{% else %}
<h2>{{ object.username }}</h2>
{% endif %} {% endif %}
{% if object == request.user %}
<a class="btn btn-primary" href="{% url 'profile:update' %}">{% trans 'Change Name' %}</a>
<a class="btn btn-primary mx-1" href="{% url 'avatar_change' %}">Change Avatar</a>
<h3>Your case studies</h3>
{% if case_studies %}
<table class="table">
<thead>
<tr>
<th>Name
<th>Approved
<th>Actions
</thead>
<tbody>
{% for case in case_studies %}
<tr>
<td>{{ case.entry_name }}
<td>{% if case.approved %}Yes{% else %}No{% endif %}
<td><a href="{% url 'detail' case.slug %}">View</a> | Edit
{% endfor %}
</tbody>
</table>
{% else %}
<p>You have no case studies.</p>
{% endif %}
{% endif %}
</div> </div>
</div> </div>
{% if object == request.user %}
<!-- Action buttons -->
<div class="row">
<div class="col-sm-12">
<a class="btn btn-primary" href="{% url 'profile:update' %}">{% trans 'Change Name' %}</a>
<a class="btn btn-primary mx-1" href="{% url 'avatar_change' %}">Change Avatar</a>
</div>
</div>
{% endif %}
</div> </div>
{% endblock content %} {% endblock content %}