diff --git a/core/localizations/messages.py b/core/localization/messages.py similarity index 71% rename from core/localizations/messages.py rename to core/localization/messages.py index f21c83a..90a4019 100644 --- a/core/localizations/messages.py +++ b/core/localization/messages.py @@ -2,3 +2,4 @@ MSG_SETTINGS_SAVED = "Die neuen Einstellungen wurden gespeichert." MSG_PASSWORDS_UNEQUAL = "Die eingegeben Passwörter stimmen nicht überein." MSG_OFM_PASSWORDS_UNEQUAL = "Die eingegeben OFM Passwörter stimmen nicht überein." +NEWER_OFM_VERSION_AVAILABLE = "Es ist eine neuere Version von OFM Helper verfügbar: %s. Du nutzt noch: %s." diff --git a/core/ofm_urls.py b/core/ofm_urls.py index 2394ade..b46e471 100644 --- a/core/ofm_urls.py +++ b/core/ofm_urls.py @@ -1,10 +1,10 @@ from django.conf.urls import url -from core.views.finance_views import FinanceDataView, FinancesAsJsonView, FinanceBalanceChartView, \ +from core.views.ofm.finance_views import FinanceDataView, FinancesAsJsonView, FinanceBalanceChartView, \ FinanceIncomeChartView, FinanceExpensesChartView -from core.views.match_views import MatchesView, MatchesAsJsonView, MatchesSummaryJsonView -from core.views.player_views import PlayerStatisticsView, PlayerStatisticsAsJsonView, PlayerDetailView, PlayerChartView -from core.views.stadium_views import StadiumStatisticsView, StadiumStatisticsAsJsonView, StadiumDetailView, \ +from core.views.ofm.match_views import MatchesView, MatchesAsJsonView, MatchesSummaryJsonView +from core.views.ofm.player_views import PlayerStatisticsView, PlayerStatisticsAsJsonView, PlayerDetailView, PlayerChartView +from core.views.ofm.stadium_views import StadiumStatisticsView, StadiumStatisticsAsJsonView, StadiumDetailView, \ StadiumStandStatisticsView, StadiumStandStatisticsChartView app_name = 'ofm' diff --git a/core/urls.py b/core/urls.py index 3ccf03c..6d52da5 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,23 +1,25 @@ from django.conf.urls import url, include from django.views.generic.base import TemplateView, RedirectView +import core.views.account_views +import core.views.settings_views import core.views.trigger_parsing_views -from core.views import base_views -from core.views.base_views import CreateChecklistItemView, DeleteChecklistItemView, GetChecklistItemsView, \ - UpdateChecklistItemView, GetChecklistItemsForTodayView, GetCurrentMatchdayView, UpdateChecklistPriorityView +from core.views.base_views import GetCurrentMatchdayView +from core.views.checklist_views import GetChecklistItemsView, GetChecklistItemsForTodayView, CreateChecklistItemView, \ + UpdateChecklistPriorityView, UpdateChecklistItemView, DeleteChecklistItemView app_name = 'core' urlpatterns = [ url(r'^favicon\.ico$', RedirectView.as_view(url='/static/core/img/OFM_favicon.png', permanent=True)), url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home'), - url(r'^register/?$', base_views.register_view, name='register'), - url(r'^login/?$', base_views.login_view, name='login'), - url(r'^account/?$', base_views.account_view, name='account'), - url(r'^logout/?$', base_views.logout_view, name='logout'), + url(r'^register/?$', core.views.account_views.register_view, name='register'), + url(r'^login/?$', core.views.account_views.login_view, name='login'), + url(r'^account/?$', core.views.account_views.account_view, name='account'), + url(r'^logout/?$', core.views.account_views.logout_view, name='logout'), url(r'^ofm/', include('core.ofm_urls'), name='ofm'), url(r'^get_current_matchday/?$', GetCurrentMatchdayView.as_view(), name='get_current_matchday'), - url(r'^settings/?$', base_views.settings_view, name='settings'), + url(r'^settings/?$', core.views.settings_views.settings_view, name='settings'), url(r'^settings_get_checklist_items/?$', GetChecklistItemsView.as_view(), name='settings_get_checklist_items'), url(r'^settings_get_checklist_items_for_today/?$', GetChecklistItemsForTodayView.as_view(), name='settings_get_checklist_items_for_today'), diff --git a/core/views/account_views.py b/core/views/account_views.py new file mode 100644 index 0000000..6dc3a11 --- /dev/null +++ b/core/views/account_views.py @@ -0,0 +1,92 @@ +from django.contrib import messages +from django.contrib.auth import authenticate, login, logout +from django.shortcuts import render, redirect + +from core.localization.messages import MSG_PASSWORDS_UNEQUAL, MSG_OFM_PASSWORDS_UNEQUAL, MSG_NOT_LOGGED_IN +from users.models import OFMUser + + +def register_view(request): + if request.user.is_authenticated(): + messages.error(request, "Du bist bereits eingeloggt. Du kannst dich im Menü ausloggen.") + return render(request, 'core/account/home.html') + if request.POST: + username = request.POST.get('username') + email = request.POST.get('email') + password = request.POST.get('password') + password2 = request.POST.get('password2') + ofm_username = request.POST.get('ofm_username') + ofm_password = request.POST.get('ofm_password') + ofm_password2 = request.POST.get('ofm_password2') + + if OFMUser.objects.filter(email=email).exists(): + messages.error(request, "Ein Account mit dieser E-Mail-Adresse existiert bereits.") + return redirect('core:register') + + if OFMUser.objects.filter(username=username).exists(): + messages.error(request, "Ein Account mit diesem Benutzernamen existiert bereits.") + return redirect('core:register') + + if password != password2: + messages.error(request, MSG_PASSWORDS_UNEQUAL) + return redirect('core:register') + + if OFMUser.objects.filter(ofm_username=ofm_username).exists(): + messages.error(request, "Es existiert bereits ein Account für diesen OFM Benutzernamen.") + return redirect('core:register') + + if ofm_password != ofm_password2: + messages.error(request, MSG_OFM_PASSWORDS_UNEQUAL) + return redirect('core:register') + + OFMUser.objects.create_user( + username=username, + email=email, + password=password, + ofm_username=ofm_username, + ofm_password=ofm_password, + ) + + messages.success(request, "Account wurde erstellt. Jetzt kannst du dich einloggen.") + return redirect('core:login') + + else: + return render(request, 'core/account/register.html') + + +def login_view(request): + if request.POST: + username = request.POST.get('username') + password = request.POST.get('password') + user = authenticate(username=username, password=password) + if user is not None: + if user.is_active: + login(request, user) + messages.success(request, "Login erfolgreich.") + return render(request, 'core/account/home.html') + else: + messages.error(request, "Login nicht möglich. Dein Account wurde deaktiviert.") + return redirect('core:login') + else: + messages.error(request, "Benutzername und/oder Passwort nicht korrekt.") + return redirect('core:login') + else: + if request.user.is_authenticated(): + return render(request, 'core/account/home.html') + else: + return render(request, 'core/account/login.html') + + +def logout_view(request): + if request.user.is_authenticated(): + logout(request) + messages.success(request, "Du wurdest abgemeldet.") + return redirect('core:home') + + +def account_view(request): + if request.user.is_authenticated(): + return render(request, 'core/account/home.html') + else: + messages.error(request, MSG_NOT_LOGGED_IN) + return redirect('core:login') diff --git a/core/views/base_views.py b/core/views/base_views.py index 104f201..e58bd29 100644 --- a/core/views/base_views.py +++ b/core/views/base_views.py @@ -1,133 +1,9 @@ from braces.views import CsrfExemptMixin, JsonRequestResponseMixin -from django.contrib import messages -from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required -from django.shortcuts import redirect, render from django.utils.decorators import method_decorator from django.views.generic import View -from core.localizations.messages import MSG_NOT_LOGGED_IN, MSG_SETTINGS_SAVED, MSG_PASSWORDS_UNEQUAL, \ - MSG_OFM_PASSWORDS_UNEQUAL -from core.models import ChecklistItem, Checklist, Matchday, Match -from users.models import OFMUser - - -def register_view(request): - if request.user.is_authenticated(): - messages.error(request, "Du bist bereits eingeloggt. Du kannst dich im Menü ausloggen.") - return render(request, 'core/account/home.html') - if request.POST: - username = request.POST.get('username') - email = request.POST.get('email') - password = request.POST.get('password') - password2 = request.POST.get('password2') - ofm_username = request.POST.get('ofm_username') - ofm_password = request.POST.get('ofm_password') - ofm_password2 = request.POST.get('ofm_password2') - - if OFMUser.objects.filter(email=email).exists(): - messages.error(request, "Ein Account mit dieser E-Mail-Adresse existiert bereits.") - return redirect('core:register') - - if OFMUser.objects.filter(username=username).exists(): - messages.error(request, "Ein Account mit diesem Benutzernamen existiert bereits.") - return redirect('core:register') - - if password != password2: - messages.error(request, MSG_PASSWORDS_UNEQUAL) - return redirect('core:register') - - if OFMUser.objects.filter(ofm_username=ofm_username).exists(): - messages.error(request, "Es existiert bereits ein Account für diesen OFM Benutzernamen.") - return redirect('core:register') - - if ofm_password != ofm_password2: - messages.error(request, MSG_OFM_PASSWORDS_UNEQUAL) - return redirect('core:register') - - OFMUser.objects.create_user( - username=username, - email=email, - password=password, - ofm_username=ofm_username, - ofm_password=ofm_password, - ) - - messages.success(request, "Account wurde erstellt. Jetzt kannst du dich einloggen.") - return redirect('core:login') - - else: - return render(request, 'core/account/register.html') - - -def login_view(request): - if request.POST: - username = request.POST.get('username') - password = request.POST.get('password') - user = authenticate(username=username, password=password) - if user is not None: - if user.is_active: - login(request, user) - messages.success(request, "Login erfolgreich.") - return render(request, 'core/account/home.html') - else: - messages.error(request, "Login nicht möglich. Dein Account wurde deaktiviert.") - return redirect('core:login') - else: - messages.error(request, "Benutzername und/oder Passwort nicht korrekt.") - return redirect('core:login') - else: - if request.user.is_authenticated(): - return render(request, 'core/account/home.html') - else: - return render(request, 'core/account/login.html') - - -def _handle_account_data_change(request, email, password, password2): - if email: - if OFMUser.objects.filter(email=email).exclude(id=request.user.id).exists(): - messages.error(request, "Ein anderer Account existiert bereits mit dieser E-Mail-Adresse.") - return - request.user.email = email - if password and password2: - if password != password2: - messages.error(request, MSG_PASSWORDS_UNEQUAL) - return - request.user.set_password(password) - request.user.save() - messages.success(request, MSG_SETTINGS_SAVED) - - -def _handle_ofm_data_change(request, ofm_password, ofm_password2): - if ofm_password != ofm_password2: - messages.error(request, MSG_OFM_PASSWORDS_UNEQUAL) - return redirect('core:register') - - request.user.ofm_password = ofm_password - request.user.save() - messages.success(request, MSG_SETTINGS_SAVED) - - -def settings_view(request): - if request.user.is_authenticated(): - if request.POST: - email = request.POST.get('email') - password = request.POST.get('password') - password2 = request.POST.get('password2') - ofm_password = request.POST.get('ofm_password') - ofm_password2 = request.POST.get('ofm_password2') - - if email or (password and password2): - _handle_account_data_change(request, email, password, password2) - elif ofm_password and ofm_password2: - _handle_ofm_data_change(request, ofm_password, ofm_password2) - else: - messages.error(request, "Die Daten waren nicht vollständig. Bitte überprüfe die Eingabe.") - - return render(request, 'core/account/settings.html') - else: - messages.error(request, MSG_NOT_LOGGED_IN) - return redirect('core:login') +from core.models import Matchday @method_decorator(login_required, name='dispatch') @@ -140,168 +16,3 @@ def get(self, request, *args, **kwargs): return self.render_json_response(matchday_json) -@method_decorator(login_required, name='dispatch') -class GetChecklistItemsView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def get(self, request, *args, **kwargs): - checklist_items = ChecklistItem.objects.filter(checklist__user=request.user) - - checklist_items_json = [_get_checklist_item_in_json(item) for item in checklist_items] - - return self.render_json_response(checklist_items_json) - - -@method_decorator(login_required, name='dispatch') -class GetChecklistItemsForTodayView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def get(self, request, *args, **kwargs): - current_matchday = Matchday.get_current() - home_match_tomorrow = Match.objects.filter( - user=request.user, - matchday__season__number=current_matchday.season.number, - matchday__number=current_matchday.number + 1, - is_home_match=True - ) - checklist_items = ChecklistItem.objects.filter(checklist__user=request.user) - checklist_items_everyday = checklist_items.filter( - to_be_checked_on_matchdays=None, - to_be_checked_on_matchday_pattern=None, - to_be_checked_if_home_match_tomorrow=False - ) - filtered_checklist_items = [] - filtered_checklist_items.extend(checklist_items_everyday) - checklist_items_this_matchday = checklist_items.filter( - to_be_checked_on_matchdays__isnull=False, - to_be_checked_on_matchday_pattern=None, - to_be_checked_if_home_match_tomorrow=False - ) - filtered_checklist_items.extend([c for c in checklist_items_this_matchday if - current_matchday.number in [int(x) for x in - c.to_be_checked_on_matchdays.split(',')]]) - if home_match_tomorrow: - checklist_items_home_match = checklist_items.filter( - to_be_checked_on_matchdays=None, - to_be_checked_on_matchday_pattern=None, - to_be_checked_if_home_match_tomorrow=True - ) - filtered_checklist_items.extend(checklist_items_home_match) - if current_matchday.number > 0: - checklist_items_matchday_pattern_pre = checklist_items.filter( - to_be_checked_on_matchdays=None, - to_be_checked_on_matchday_pattern__isnull=False, - to_be_checked_if_home_match_tomorrow=False - ) - checklist_items_matchday_pattern = [c for c - in checklist_items_matchday_pattern_pre - if current_matchday.number % c.to_be_checked_on_matchday_pattern == 0] - filtered_checklist_items.extend(checklist_items_matchday_pattern) - - sorted_checklist_items = sorted(filtered_checklist_items, key=lambda x: x.priority, reverse=False) - checklist_items_json = [_get_checklist_item_in_json(item) for item in sorted_checklist_items] - - return self.render_json_response(checklist_items_json) - - -def _get_checklist_item_in_json(checklist_item): - checklist_item_json = dict() - checklist_item_json['id'] = checklist_item.id - checklist_item_json['name'] = checklist_item.name - if checklist_item.to_be_checked_if_home_match_tomorrow: - checklist_item_json['type_home_match'] = checklist_item.to_be_checked_if_home_match_tomorrow - if checklist_item.to_be_checked_on_matchdays is not None: - checklist_item_json['type_matchdays'] = checklist_item.to_be_checked_on_matchdays - if checklist_item.to_be_checked_on_matchday_pattern is not None: - checklist_item_json['type_matchday_pattern'] = checklist_item.to_be_checked_on_matchday_pattern - checklist_item_json['checked'] = checklist_item.last_checked_on_matchday == Matchday.get_current() - - return checklist_item_json - - -@method_decorator(login_required, name='dispatch') -class CreateChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def get(self, request, *args, **kwargs): - checklist, _ = Checklist.objects.get_or_create(user=request.user) - new_checklist_item = ChecklistItem.objects.create(checklist=checklist, name='Neuer Eintrag') - - new_checklist_item_json = _get_checklist_item_in_json(new_checklist_item) - - return self.render_json_response(new_checklist_item_json) - - -@method_decorator(login_required, name='dispatch') -class UpdateChecklistPriorityView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def post(self, request, *args, **kwargs): - checklist_priority = request.POST.get('checklist_priority') - - priority = [int(x) for x in checklist_priority.split(',')] - for checklist_item_id in priority: - checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) - checklist_item.priority = priority.index(checklist_item_id) - checklist_item.save() - - return self.render_json_response({'success': True}) - - -@method_decorator(login_required, name='dispatch') -class UpdateChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def post(self, request, *args, **kwargs): - checklist_item_id = request.POST.get('checklist_item_id') - checklist_item_name = request.POST.get('checklist_item_name') - checklist_item_matchdays = request.POST.get('checklist_item_matchdays') - checklist_item_matchday_pattern = request.POST.get('checklist_item_matchday_pattern') - checklist_item_home_match = request.POST.get('checklist_item_home_match') - checklist_item_everyday = request.POST.get('checklist_item_everyday') - checklist_item_checked = request.POST.get('checklist_item_checked') - - checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) - - if checklist_item: - if checklist_item_name: - checklist_item.name = checklist_item_name - elif checklist_item_matchdays: - checklist_item.to_be_checked_on_matchdays = checklist_item_matchdays - checklist_item.to_be_checked_on_matchday_pattern = None - checklist_item.to_be_checked_if_home_match_tomorrow = False - elif checklist_item_matchday_pattern: - checklist_item.to_be_checked_on_matchdays = None - checklist_item.to_be_checked_on_matchday_pattern = checklist_item_matchday_pattern - checklist_item.to_be_checked_if_home_match_tomorrow = False - elif checklist_item_home_match: - checklist_item.to_be_checked_on_matchdays = None - checklist_item.to_be_checked_on_matchday_pattern = None - checklist_item.to_be_checked_if_home_match_tomorrow = True - elif checklist_item_everyday: - checklist_item.to_be_checked_on_matchdays = None - checklist_item.to_be_checked_on_matchday_pattern = None - checklist_item.to_be_checked_if_home_match_tomorrow = False - elif checklist_item_checked == 'true': - checklist_item.last_checked_on_matchday = Matchday.get_current() - elif checklist_item_checked == 'false': - checklist_item.last_checked_on_matchday = None - checklist_item.save() - return self.render_json_response({'success': True}) - return self.render_json_response({'success': False}) - - -@method_decorator(login_required, name='dispatch') -class DeleteChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): - def post(self, request, *args, **kwargs): - checklist_item_id = request.POST.get('checklist_item_id') - checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) - if checklist_item: - checklist_item.delete() - return self.render_json_response({'success': True}) - return self.render_json_response({'success': False}) - - -def logout_view(request): - if request.user.is_authenticated(): - logout(request) - messages.success(request, "Du wurdest abgemeldet.") - return redirect('core:home') - - -def account_view(request): - if request.user.is_authenticated(): - return render(request, 'core/account/home.html') - else: - messages.error(request, MSG_NOT_LOGGED_IN) - return redirect('core:login') diff --git a/core/views/checklist_views.py b/core/views/checklist_views.py new file mode 100644 index 0000000..15f323a --- /dev/null +++ b/core/views/checklist_views.py @@ -0,0 +1,158 @@ +from braces.views import CsrfExemptMixin, JsonRequestResponseMixin +from django.contrib.auth.decorators import login_required +from django.utils.decorators import method_decorator +from django.views import View + +from core.models import ChecklistItem, Matchday, Match, Checklist + + +@method_decorator(login_required, name='dispatch') +class GetChecklistItemsView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def get(self, request, *args, **kwargs): + checklist_items = ChecklistItem.objects.filter(checklist__user=request.user) + + checklist_items_json = [_get_checklist_item_in_json(item) for item in checklist_items] + + return self.render_json_response(checklist_items_json) + + +@method_decorator(login_required, name='dispatch') +class GetChecklistItemsForTodayView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def get(self, request, *args, **kwargs): + current_matchday = Matchday.get_current() + home_match_tomorrow = Match.objects.filter( + user=request.user, + matchday__season__number=current_matchday.season.number, + matchday__number=current_matchday.number + 1, + is_home_match=True + ) + checklist_items = ChecklistItem.objects.filter(checklist__user=request.user) + checklist_items_everyday = checklist_items.filter( + to_be_checked_on_matchdays=None, + to_be_checked_on_matchday_pattern=None, + to_be_checked_if_home_match_tomorrow=False + ) + filtered_checklist_items = [] + filtered_checklist_items.extend(checklist_items_everyday) + checklist_items_this_matchday = checklist_items.filter( + to_be_checked_on_matchdays__isnull=False, + to_be_checked_on_matchday_pattern=None, + to_be_checked_if_home_match_tomorrow=False + ) + filtered_checklist_items.extend([c for c in checklist_items_this_matchday if + current_matchday.number in [int(x) for x in + c.to_be_checked_on_matchdays.split(',')]]) + if home_match_tomorrow: + checklist_items_home_match = checklist_items.filter( + to_be_checked_on_matchdays=None, + to_be_checked_on_matchday_pattern=None, + to_be_checked_if_home_match_tomorrow=True + ) + filtered_checklist_items.extend(checklist_items_home_match) + if current_matchday.number > 0: + checklist_items_matchday_pattern_pre = checklist_items.filter( + to_be_checked_on_matchdays=None, + to_be_checked_on_matchday_pattern__isnull=False, + to_be_checked_if_home_match_tomorrow=False + ) + checklist_items_matchday_pattern = [c for c + in checklist_items_matchday_pattern_pre + if current_matchday.number % c.to_be_checked_on_matchday_pattern == 0] + filtered_checklist_items.extend(checklist_items_matchday_pattern) + + sorted_checklist_items = sorted(filtered_checklist_items, key=lambda x: x.priority, reverse=False) + checklist_items_json = [_get_checklist_item_in_json(item) for item in sorted_checklist_items] + + return self.render_json_response(checklist_items_json) + + +def _get_checklist_item_in_json(checklist_item): + checklist_item_json = dict() + checklist_item_json['id'] = checklist_item.id + checklist_item_json['name'] = checklist_item.name + if checklist_item.to_be_checked_if_home_match_tomorrow: + checklist_item_json['type_home_match'] = checklist_item.to_be_checked_if_home_match_tomorrow + if checklist_item.to_be_checked_on_matchdays is not None: + checklist_item_json['type_matchdays'] = checklist_item.to_be_checked_on_matchdays + if checklist_item.to_be_checked_on_matchday_pattern is not None: + checklist_item_json['type_matchday_pattern'] = checklist_item.to_be_checked_on_matchday_pattern + checklist_item_json['checked'] = checklist_item.last_checked_on_matchday == Matchday.get_current() + + return checklist_item_json + + +@method_decorator(login_required, name='dispatch') +class CreateChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def get(self, request, *args, **kwargs): + checklist, _ = Checklist.objects.get_or_create(user=request.user) + new_checklist_item = ChecklistItem.objects.create(checklist=checklist, name='Neuer Eintrag') + + new_checklist_item_json = _get_checklist_item_in_json(new_checklist_item) + + return self.render_json_response(new_checklist_item_json) + + +@method_decorator(login_required, name='dispatch') +class UpdateChecklistPriorityView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def post(self, request, *args, **kwargs): + checklist_priority = request.POST.get('checklist_priority') + + priority = [int(x) for x in checklist_priority.split(',')] + for checklist_item_id in priority: + checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) + checklist_item.priority = priority.index(checklist_item_id) + checklist_item.save() + + return self.render_json_response({'success': True}) + + +@method_decorator(login_required, name='dispatch') +class UpdateChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def post(self, request, *args, **kwargs): + checklist_item_id = request.POST.get('checklist_item_id') + checklist_item_name = request.POST.get('checklist_item_name') + checklist_item_matchdays = request.POST.get('checklist_item_matchdays') + checklist_item_matchday_pattern = request.POST.get('checklist_item_matchday_pattern') + checklist_item_home_match = request.POST.get('checklist_item_home_match') + checklist_item_everyday = request.POST.get('checklist_item_everyday') + checklist_item_checked = request.POST.get('checklist_item_checked') + + checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) + + if checklist_item: + if checklist_item_name: + checklist_item.name = checklist_item_name + elif checklist_item_matchdays: + checklist_item.to_be_checked_on_matchdays = checklist_item_matchdays + checklist_item.to_be_checked_on_matchday_pattern = None + checklist_item.to_be_checked_if_home_match_tomorrow = False + elif checklist_item_matchday_pattern: + checklist_item.to_be_checked_on_matchdays = None + checklist_item.to_be_checked_on_matchday_pattern = checklist_item_matchday_pattern + checklist_item.to_be_checked_if_home_match_tomorrow = False + elif checklist_item_home_match: + checklist_item.to_be_checked_on_matchdays = None + checklist_item.to_be_checked_on_matchday_pattern = None + checklist_item.to_be_checked_if_home_match_tomorrow = True + elif checklist_item_everyday: + checklist_item.to_be_checked_on_matchdays = None + checklist_item.to_be_checked_on_matchday_pattern = None + checklist_item.to_be_checked_if_home_match_tomorrow = False + elif checklist_item_checked == 'true': + checklist_item.last_checked_on_matchday = Matchday.get_current() + elif checklist_item_checked == 'false': + checklist_item.last_checked_on_matchday = None + checklist_item.save() + return self.render_json_response({'success': True}) + return self.render_json_response({'success': False}) + + +@method_decorator(login_required, name='dispatch') +class DeleteChecklistItemView(CsrfExemptMixin, JsonRequestResponseMixin, View): + def post(self, request, *args, **kwargs): + checklist_item_id = request.POST.get('checklist_item_id') + checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id) + if checklist_item: + checklist_item.delete() + return self.render_json_response({'success': True}) + return self.render_json_response({'success': False}) diff --git a/core/localizations/__init__.py b/core/views/ofm/__init__.py similarity index 100% rename from core/localizations/__init__.py rename to core/views/ofm/__init__.py diff --git a/core/views/finance_views.py b/core/views/ofm/finance_views.py similarity index 100% rename from core/views/finance_views.py rename to core/views/ofm/finance_views.py diff --git a/core/views/match_views.py b/core/views/ofm/match_views.py similarity index 100% rename from core/views/match_views.py rename to core/views/ofm/match_views.py diff --git a/core/views/player_views.py b/core/views/ofm/player_views.py similarity index 100% rename from core/views/player_views.py rename to core/views/ofm/player_views.py diff --git a/core/views/stadium_views.py b/core/views/ofm/stadium_views.py similarity index 100% rename from core/views/stadium_views.py rename to core/views/ofm/stadium_views.py diff --git a/core/views/settings_views.py b/core/views/settings_views.py new file mode 100644 index 0000000..7dc34e4 --- /dev/null +++ b/core/views/settings_views.py @@ -0,0 +1,53 @@ +from django.contrib import messages +from django.shortcuts import redirect, render + +from core.localization.messages import MSG_PASSWORDS_UNEQUAL, MSG_SETTINGS_SAVED, MSG_OFM_PASSWORDS_UNEQUAL, \ + MSG_NOT_LOGGED_IN +from users.models import OFMUser + + +def _handle_account_data_change(request, email, password, password2): + if email: + if OFMUser.objects.filter(email=email).exclude(id=request.user.id).exists(): + messages.error(request, "Ein anderer Account existiert bereits mit dieser E-Mail-Adresse.") + return + request.user.email = email + if password and password2: + if password != password2: + messages.error(request, MSG_PASSWORDS_UNEQUAL) + return + request.user.set_password(password) + request.user.save() + messages.success(request, MSG_SETTINGS_SAVED) + + +def _handle_ofm_data_change(request, ofm_password, ofm_password2): + if ofm_password != ofm_password2: + messages.error(request, MSG_OFM_PASSWORDS_UNEQUAL) + return redirect('core:register') + + request.user.ofm_password = ofm_password + request.user.save() + messages.success(request, MSG_SETTINGS_SAVED) + + +def settings_view(request): + if request.user.is_authenticated(): + if request.POST: + email = request.POST.get('email') + password = request.POST.get('password') + password2 = request.POST.get('password2') + ofm_password = request.POST.get('ofm_password') + ofm_password2 = request.POST.get('ofm_password2') + + if email or (password and password2): + _handle_account_data_change(request, email, password, password2) + elif ofm_password and ofm_password2: + _handle_ofm_data_change(request, ofm_password, ofm_password2) + else: + messages.error(request, "Die Daten waren nicht vollständig. Bitte überprüfe die Eingabe.") + + return render(request, 'core/account/settings.html') + else: + messages.error(request, MSG_NOT_LOGGED_IN) + return redirect('core:login') diff --git a/core/views/trigger_parsing_views.py b/core/views/trigger_parsing_views.py index 115aeb4..dc67c60 100644 --- a/core/views/trigger_parsing_views.py +++ b/core/views/trigger_parsing_views.py @@ -1,7 +1,7 @@ from django.contrib import messages from django.shortcuts import redirect -from core.localizations.messages import MSG_NOT_LOGGED_IN +from core.localization.messages import MSG_NOT_LOGGED_IN, NEWER_OFM_VERSION_AVAILABLE from core.managers.parser_manager import ParserManager from core.managers.site_manager import SiteManager @@ -19,8 +19,7 @@ def trigger_parsing(request): with open('version', 'r') as version_file: own_version = version_file.read().replace('\n', '') if own_version != "null" and own_version != remote_version: - messages.info(request, "Es ist eine neuere Version von OFM Helper verfügbar: %s. Du nutzt noch: %s." % ( - remote_version, own_version)) + messages.info(request, NEWER_OFM_VERSION_AVAILABLE % (remote_version, own_version)) except IOError: pass