2020-11-29 19:22:55 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
|
|
from django.urls import reverse_lazy
|
2020-12-25 11:14:21 +01:00
|
|
|
from django.contrib import messages
|
2020-11-29 19:22:55 +01:00
|
|
|
|
|
|
|
from lostplaces.common import get_all_subclasses
|
|
|
|
from lostplaces.views.base_views import IsAuthenticatedMixin
|
|
|
|
from lostplaces.models.models import Explorer
|
|
|
|
from lostplaces.models.place import Place, PlaceAsset
|
2020-12-25 01:31:01 +01:00
|
|
|
from lostplaces.forms import ExplorerChangeForm, ExplorerUserChangeForm
|
2020-11-29 19:22:55 +01:00
|
|
|
|
|
|
|
class ExplorerProfileView(IsAuthenticatedMixin, View):
|
|
|
|
def get(self, request, explorer_id):
|
|
|
|
explorer = get_object_or_404(Explorer, pk=explorer_id)
|
|
|
|
place_list = Place.objects.filter(submitted_by=explorer)
|
|
|
|
place_count = place_list.count()
|
|
|
|
|
|
|
|
context={
|
|
|
|
'explorer': explorer,
|
|
|
|
'place_count': place_count,
|
|
|
|
'place_list': place_list,
|
|
|
|
'assets': {}
|
|
|
|
}
|
|
|
|
|
|
|
|
asset_count = 0
|
|
|
|
for subclass in get_all_subclasses(PlaceAsset): # kinda ugly, but PlaceAsset cannot be abstract according to django ORM
|
|
|
|
objects = subclass.objects.filter(submitted_by=explorer)
|
|
|
|
context['assets'][subclass.__name__.lower()+'s'] = objects
|
|
|
|
asset_count += objects.count()
|
|
|
|
|
|
|
|
context['asset_count'] = asset_count
|
2020-12-24 15:13:40 +01:00
|
|
|
|
2020-11-29 19:22:55 +01:00
|
|
|
return render(
|
|
|
|
request=request,
|
|
|
|
template_name='explorer/profile.html',
|
|
|
|
context=context
|
|
|
|
)
|
2020-12-25 01:31:01 +01:00
|
|
|
|
|
|
|
class ExplorerProfileUpdateView(IsAuthenticatedMixin, View):
|
|
|
|
success_message = ''
|
|
|
|
permission_denied_message = ''
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
'explorer_user_change_form': ExplorerUserChangeForm(instance=request.user),
|
|
|
|
'explorer_change_form': ExplorerChangeForm(instance=request.user.explorer)
|
|
|
|
}
|
2020-12-25 02:11:02 +01:00
|
|
|
if request.user.explorer.profile_image:
|
|
|
|
context['explorer_image_url'] = request.user.explorer.profile_image.url
|
2020-12-25 01:31:01 +01:00
|
|
|
return render(request, 'explorer/profile_update.html', context)
|
2020-12-25 02:11:02 +01:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2020-12-25 11:14:21 +01:00
|
|
|
explorer_user_change_form = ExplorerUserChangeForm(request.POST, instance=request.user)
|
|
|
|
explorer_change_form = ExplorerChangeForm(request.POST, instance=request.user.explorer)
|
2020-12-25 02:11:02 +01:00
|
|
|
|
2020-12-25 11:14:21 +01:00
|
|
|
if explorer_change_form.is_valid() and explorer_user_change_form.is_valid():
|
|
|
|
print(explorer_user_change_form)
|
|
|
|
explorer_user_change_form.save()
|
|
|
|
explorer_change_form.save()
|
2020-12-25 02:11:02 +01:00
|
|
|
|
2020-12-25 11:14:21 +01:00
|
|
|
#self.handle_place_images(request, explorer)
|
2020-12-25 02:11:02 +01:00
|
|
|
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2020-12-25 11:14:21 +01:00
|
|
|
_('Successfully updated Explorer profile')
|
2020-12-25 02:11:02 +01:00
|
|
|
)
|
2020-12-25 11:14:21 +01:00
|
|
|
return redirect(reverse_lazy('explorer_profile_update'))
|
2020-12-25 02:11:02 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Usually the browser should have checked the form before sending.
|
|
|
|
messages.error(
|
|
|
|
self.request,
|
|
|
|
_('Please fill in all required fields.')
|
|
|
|
)
|
2020-12-25 11:14:21 +01:00
|
|
|
return redirect(reverse_lazy('explorer_profile_update'))
|
|
|
|
|