From 37f4ca78696407fe2dd07f229749e5d557f7cd32 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Fri, 26 Jan 2018 21:52:10 -0500 Subject: [PATCH] Refactor get_together views --- get_together/views.py | 215 --------------------------------- get_together/views/__init__.py | 65 ++++++++++ get_together/views/events.py | 95 +++++++++++++++ get_together/views/places.py | 17 +++ get_together/views/teams.py | 92 ++++++++++++++ get_together/views/user.py | 17 +++ 6 files changed, 286 insertions(+), 215 deletions(-) delete mode 100644 get_together/views.py create mode 100644 get_together/views/__init__.py create mode 100644 get_together/views/events.py create mode 100644 get_together/views/places.py create mode 100644 get_together/views/teams.py create mode 100644 get_together/views/user.py diff --git a/get_together/views.py b/get_together/views.py deleted file mode 100644 index 2659923..0000000 --- a/get_together/views.py +++ /dev/null @@ -1,215 +0,0 @@ -from django.utils.translation import ugettext_lazy as _ - -from django.contrib import messages -from django.contrib.auth import logout as logout_user -from django.shortcuts import render, redirect -from django.http import HttpResponse, JsonResponse - -from events.models.profiles import Team, UserProfile, Member -from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm - -from events.models.events import Event, Place, Attendee - -import datetime -import simplejson - -# Create your views here. - -def logout(request): - if request.user.is_authenticated: - logout_user(request) - return redirect('home') - -def home(request, *args, **kwards): - if request.user.is_authenticated: - user_teams = Team.objects.filter(owner_profile=request.user.profile) - if len(user_teams) > 0: - return redirect('events') - else: - return redirect('create-team') - else: - return render(request, 'get_together/index.html') - -def events_list(request, *args, **kwargs): - events = Event.objects.filter(end_time__gt=datetime.datetime.now()).order_by('start_time') - context = { - 'events_list': events, - } - return render(request, 'get_together/events/list_events.html', context) - -def create_team(request, *args, **kwargs): - if request.method == 'GET': - form = NewTeamForm() - - context = { - 'team_form': form, - } - return render(request, 'get_together/teams/create_team.html', context) - elif request.method == 'POST': - form = NewTeamForm(request.POST) - if form.is_valid: - new_team = form.save() - new_team.owner_profile = request.user.profile - new_team.save() - Member.objects.create(team=new_team, user=request.user.profile, role=Member.ADMIN) - return redirect('show-team', team_id=new_team.pk) - else: - context = { - 'team_form': form, - } - return render(request, 'get_together/teams/create_team.html', context) - else: - return redirect('home') - -def edit_team(request, team_id): - team = Team.objects.get(id=team_id) - if not request.user.profile.can_edit_team(team): - messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.')) - return redirect('show-team', team_id=team.pk) - - if request.method == 'GET': - form = TeamForm(instance=team) - - context = { - 'team': team, - 'team_form': form, - } - return render(request, 'get_together/teams/edit_team.html', context) - elif request.method == 'POST': - form = TeamForm(request.POST, instance=team) - if form.is_valid: - new_team = form.save() - new_team.owner_profile = request.user.profile - new_team.save() - return redirect('show-team', team_id=new_team.pk) - else: - context = { - 'team': team, - 'team_form': form, - } - return render(request, 'get_together/teams/edit_team.html', context) - else: - return redirect('home') - -def teams_list(request, *args, **kwargs): - teams = Team.objects.all() - context = { - 'all_teams': teams, - } - if request.user.is_authenticated: - context['my_teams'] = request.user.profile.memberships.all() - return render(request, 'get_together/teams/list_teams.html', context) - - -def show_team(request, team_id, *args, **kwargs): - team = Team.objects.get(id=team_id) - team_events = Event.objects.filter(team=team, end_time__gt=datetime.datetime.now()).order_by('start_time') - context = { - 'team': team, - 'events_list': team_events, - 'is_member': request.user.profile in team.members.all(), - 'member_list': Member.objects.filter(team=team), - 'can_create_event': request.user.profile.can_create_event(team), - 'can_edit_team': request.user.profile.can_edit_team(team), - } - return render(request, 'get_together/teams/show_team.html', context) - -def edit_event(request, event_id): - event = Event.objects.get(id=event_id) - - if not request.user.profile.can_edit_event(event): - messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.')) - return redirect(event.get_absolute_url()) - - if request.method == 'GET': - form = TeamEventForm(instance=event) - - context = { - 'team': event.team, - 'event': event, - 'event_form': form, - } - return render(request, 'get_together/events/edit_event.html', context) - elif request.method == 'POST': - form = TeamEventForm(request.POST,instance=event) - if form.is_valid: - new_event = form.save() - return redirect(new_event.get_absolute_url()) - else: - context = { - 'team': event.team, - 'event': event, - 'event_form': form, - } - return render(request, 'get_together/events/edit_event.html', context) - else: - return redirect('home') - -def create_event(request, team_id): - team = Team.objects.get(id=team_id) - if not request.user.profile.can_create_event(team): - messages.add_message(request, messages.WARNING, message=_('You can not create events for this team.')) - return redirect('show-team', team_id=team.pk) - - if request.method == 'GET': - form = NewTeamEventForm() - - context = { - 'team': team, - 'event_form': form, - } - return render(request, 'get_together/events/create_event.html', context) - elif request.method == 'POST': - form = NewTeamEventForm(request.POST) - if form.is_valid: - form.instance.team = team - form.instance.created_by = request.user.profile - new_event = form.save() - return redirect(new_event.get_absolute_url()) - else: - context = { - 'team': team, - 'event_form': form, - } - return render(request, 'get_together/events/create_event.html', context) - else: - return redirect('home') - -def places_list(request, *args, **kwargs): - places = Place.objects.all() - context = { - 'places_list': places, - } - return render(request, 'get_together/places/list_places.html', context) - -def create_place(request): - if request.method == 'GET': - form = NewPlaceForm() - - context = { - 'place_form': form, - } - return render(request, 'get_together/places/create_place.html', context) - elif request.method == 'POST': - form = NewPlaceForm(request.POST) - if form.is_valid: - new_place = form.save() - return redirect('places') - else: - context = { - 'place_form': form, - } - return render(request, 'get_together/places/create_place.html', context) - else: - return redirect('home') - -def show_event(request, event_id, event_slug): - event = Event.objects.get(id=event_id) - context = { - 'team': event.team, - 'event': event, - 'is_attending': request.user.profile in event.attendees.all(), - 'attendee_list': Attendee.objects.filter(event=event), - 'can_edit_event': request.user.profile.can_edit_event(event), - } - return render(request, 'get_together/events/show_event.html', context) diff --git a/get_together/views/__init__.py b/get_together/views/__init__.py new file mode 100644 index 0000000..f005b99 --- /dev/null +++ b/get_together/views/__init__.py @@ -0,0 +1,65 @@ +from django.utils.translation import ugettext_lazy as _ + +from django.contrib import messages +from django.contrib.auth import logout as logout_user +from django.shortcuts import render, redirect +from django.http import HttpResponse, JsonResponse + +from events.models.profiles import Team, UserProfile, Member +from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm + +from events.models.events import Event, Place, Attendee + +import datetime +import simplejson + +from .teams import * +from .events import * +from .places import * +from .user import * + +# Create your views here. + +def logout(request): + if request.user.is_authenticated: + logout_user(request) + return redirect('home') + +def home(request, *args, **kwards): + if request.user.is_authenticated: + user_teams = Team.objects.filter(owner_profile=request.user.profile) + if len(user_teams) > 0: + return redirect('events') + else: + return redirect('create-team') + else: + return render(request, 'get_together/index.html') + +def places_list(request, *args, **kwargs): + places = Place.objects.all() + context = { + 'places_list': places, + } + return render(request, 'get_together/places/list_places.html', context) + +def create_place(request): + if request.method == 'GET': + form = NewPlaceForm() + + context = { + 'place_form': form, + } + return render(request, 'get_together/places/create_place.html', context) + elif request.method == 'POST': + form = NewPlaceForm(request.POST) + if form.is_valid: + new_place = form.save() + return redirect('places') + else: + context = { + 'place_form': form, + } + return render(request, 'get_together/places/create_place.html', context) + else: + return redirect('home') + diff --git a/get_together/views/events.py b/get_together/views/events.py new file mode 100644 index 0000000..3827753 --- /dev/null +++ b/get_together/views/events.py @@ -0,0 +1,95 @@ +from django.utils.translation import ugettext_lazy as _ + +from django.contrib import messages +from django.contrib.auth import logout as logout_user +from django.shortcuts import render, redirect +from django.http import HttpResponse, JsonResponse + +from events.models.profiles import Team, UserProfile, Member +from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm + +from events.models.events import Event, Place, Attendee + +import datetime +import simplejson + +# Create your views here. +def events_list(request, *args, **kwargs): + events = Event.objects.filter(end_time__gt=datetime.datetime.now()).order_by('start_time') + context = { + 'events_list': events, + } + return render(request, 'get_together/events/list_events.html', context) + +def show_event(request, event_id, event_slug): + event = Event.objects.get(id=event_id) + context = { + 'team': event.team, + 'event': event, + 'is_attending': request.user.profile in event.attendees.all(), + 'attendee_list': Attendee.objects.filter(event=event), + 'can_edit_event': request.user.profile.can_edit_event(event), + } + return render(request, 'get_together/events/show_event.html', context) + +def create_event(request, team_id): + team = Team.objects.get(id=team_id) + if not request.user.profile.can_create_event(team): + messages.add_message(request, messages.WARNING, message=_('You can not create events for this team.')) + return redirect('show-team', team_id=team.pk) + + if request.method == 'GET': + form = NewTeamEventForm() + + context = { + 'team': team, + 'event_form': form, + } + return render(request, 'get_together/events/create_event.html', context) + elif request.method == 'POST': + form = NewTeamEventForm(request.POST) + if form.is_valid: + form.instance.team = team + form.instance.created_by = request.user.profile + new_event = form.save() + return redirect(new_event.get_absolute_url()) + else: + context = { + 'team': team, + 'event_form': form, + } + return render(request, 'get_together/events/create_event.html', context) + else: + return redirect('home') + +def edit_event(request, event_id): + event = Event.objects.get(id=event_id) + + if not request.user.profile.can_edit_event(event): + messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.')) + return redirect(event.get_absolute_url()) + + if request.method == 'GET': + form = TeamEventForm(instance=event) + + context = { + 'team': event.team, + 'event': event, + 'event_form': form, + } + return render(request, 'get_together/events/edit_event.html', context) + elif request.method == 'POST': + form = TeamEventForm(request.POST,instance=event) + if form.is_valid: + new_event = form.save() + return redirect(new_event.get_absolute_url()) + else: + context = { + 'team': event.team, + 'event': event, + 'event_form': form, + } + return render(request, 'get_together/events/edit_event.html', context) + else: + return redirect('home') + diff --git a/get_together/views/places.py b/get_together/views/places.py new file mode 100644 index 0000000..88a8476 --- /dev/null +++ b/get_together/views/places.py @@ -0,0 +1,17 @@ +from django.utils.translation import ugettext_lazy as _ + +from django.contrib import messages +from django.contrib.auth import logout as logout_user +from django.shortcuts import render, redirect +from django.http import HttpResponse, JsonResponse + +from events.models.profiles import Team, UserProfile, Member +from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm + +from events.models.events import Event, Place, Attendee + +import datetime +import simplejson + +# Create your views here. + diff --git a/get_together/views/teams.py b/get_together/views/teams.py new file mode 100644 index 0000000..734c8a9 --- /dev/null +++ b/get_together/views/teams.py @@ -0,0 +1,92 @@ +from django.utils.translation import ugettext_lazy as _ + +from django.contrib import messages +from django.contrib.auth import logout as logout_user +from django.shortcuts import render, redirect +from django.http import HttpResponse, JsonResponse + +from events.models.profiles import Team, UserProfile, Member +from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm + +from events.models.events import Event, Place, Attendee + +import datetime +import simplejson + +# Create your views here. +def teams_list(request, *args, **kwargs): + teams = Team.objects.all() + context = { + 'all_teams': teams, + } + if request.user.is_authenticated: + context['my_teams'] = request.user.profile.memberships.all() + return render(request, 'get_together/teams/list_teams.html', context) + +def show_team(request, team_id, *args, **kwargs): + team = Team.objects.get(id=team_id) + team_events = Event.objects.filter(team=team, end_time__gt=datetime.datetime.now()).order_by('start_time') + context = { + 'team': team, + 'events_list': team_events, + 'is_member': request.user.profile in team.members.all(), + 'member_list': Member.objects.filter(team=team), + 'can_create_event': request.user.profile.can_create_event(team), + 'can_edit_team': request.user.profile.can_edit_team(team), + } + return render(request, 'get_together/teams/show_team.html', context) + +def create_team(request, *args, **kwargs): + if request.method == 'GET': + form = NewTeamForm() + + context = { + 'team_form': form, + } + return render(request, 'get_together/teams/create_team.html', context) + elif request.method == 'POST': + form = NewTeamForm(request.POST) + if form.is_valid: + new_team = form.save() + new_team.owner_profile = request.user.profile + new_team.save() + Member.objects.create(team=new_team, user=request.user.profile, role=Member.ADMIN) + return redirect('show-team', team_id=new_team.pk) + else: + context = { + 'team_form': form, + } + return render(request, 'get_together/teams/create_team.html', context) + else: + return redirect('home') + +def edit_team(request, team_id): + team = Team.objects.get(id=team_id) + if not request.user.profile.can_edit_team(team): + messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.')) + return redirect('show-team', team_id=team.pk) + + if request.method == 'GET': + form = TeamForm(instance=team) + + context = { + 'team': team, + 'team_form': form, + } + return render(request, 'get_together/teams/edit_team.html', context) + elif request.method == 'POST': + form = TeamForm(request.POST, instance=team) + if form.is_valid: + new_team = form.save() + new_team.owner_profile = request.user.profile + new_team.save() + return redirect('show-team', team_id=new_team.pk) + else: + context = { + 'team': team, + 'team_form': form, + } + return render(request, 'get_together/teams/edit_team.html', context) + else: + return redirect('home') + diff --git a/get_together/views/user.py b/get_together/views/user.py new file mode 100644 index 0000000..3c71724 --- /dev/null +++ b/get_together/views/user.py @@ -0,0 +1,17 @@ +from django.utils.translation import ugettext_lazy as _ + +from django.contrib import messages +from django.contrib.auth import logout as logout_user +from django.shortcuts import render, redirect +from django.http import HttpResponse, JsonResponse + +from events.models.profiles import Team, UserProfile, Member +from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm + +from events.models.events import Event, Place, Attendee + +import datetime +import simplejson + +from .teams import * +from .events import *