2018-01-27 03:52:10 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import logout as logout_user
|
2018-03-04 20:45:25 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2018-01-27 03:52:10 +01:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
|
|
|
|
from events.models.profiles import Team, UserProfile, Member
|
2018-01-27 04:38:11 +01:00
|
|
|
from events.forms import TeamForm, NewTeamForm, DeleteTeamForm
|
2018-01-27 03:52:10 +01:00
|
|
|
|
|
|
|
from events.models.events import Event, Place, Attendee
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import simplejson
|
|
|
|
|
|
|
|
# Create your views here.
|
2018-03-04 20:45:25 +01:00
|
|
|
@login_required
|
2018-01-27 03:52:10 +01:00
|
|
|
def teams_list(request, *args, **kwargs):
|
2018-03-04 20:45:25 +01:00
|
|
|
teams = request.user.profile.memberships.all()
|
2018-01-27 03:52:10 +01:00
|
|
|
context = {
|
2018-03-04 20:45:25 +01:00
|
|
|
'teams': teams,
|
2018-01-27 03:52:10 +01:00
|
|
|
}
|
|
|
|
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)
|
2018-03-18 17:48:03 +01:00
|
|
|
upcoming_events = Event.objects.filter(team=team, end_time__gt=datetime.datetime.now()).order_by('start_time')
|
|
|
|
recent_events = Event.objects.filter(team=team, end_time__lte=datetime.datetime.now()).order_by('-start_time')[:5]
|
2018-01-27 03:52:10 +01:00
|
|
|
context = {
|
|
|
|
'team': team,
|
2018-03-18 17:49:43 +01:00
|
|
|
'upcoming_events': upcoming_events,
|
2018-03-18 17:48:03 +01:00
|
|
|
'recent_events': recent_events,
|
2018-01-27 03:52:10 +01:00
|
|
|
'is_member': request.user.profile in team.members.all(),
|
2018-03-20 23:35:02 +01:00
|
|
|
'member_list': Member.objects.filter(team=team).order_by('-role', 'joined_date'),
|
2018-01-27 03:52:10 +01:00
|
|
|
'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)
|
2018-01-29 16:06:46 +01:00
|
|
|
if form.is_valid():
|
2018-01-27 03:52:10 +01:00
|
|
|
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)
|
2018-01-29 16:06:46 +01:00
|
|
|
if form.is_valid():
|
2018-01-27 03:52:10 +01:00
|
|
|
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')
|
|
|
|
|
2018-01-27 04:38:11 +01:00
|
|
|
def delete_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)
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
form = DeleteTeamForm()
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'team': team,
|
|
|
|
'delete_form': form,
|
|
|
|
}
|
|
|
|
return render(request, 'get_together/teams/delete_team.html', context)
|
|
|
|
elif request.method == 'POST':
|
|
|
|
form = DeleteTeamForm(request.POST)
|
|
|
|
if form.is_valid() and form.cleaned_data['confirm']:
|
|
|
|
team.delete()
|
|
|
|
return redirect('teams')
|
|
|
|
else:
|
|
|
|
context = {
|
|
|
|
'team': team,
|
|
|
|
'delete_form': form,
|
|
|
|
}
|
|
|
|
return render(request, 'get_together/teams/delete_team.html', context)
|
|
|
|
else:
|
|
|
|
return redirect('home')
|
|
|
|
|
|
|
|
|