Private
Public Access
1
0

Team create, delete, leave, create invite

This commit is contained in:
Sander Roosendaal
2017-02-09 17:50:32 +01:00
parent f459cea2bd
commit 40e18fee21
10 changed files with 473 additions and 60 deletions

View File

@@ -7,21 +7,26 @@ from datetime import timedelta
import time
from django.db import IntegrityError
import uuid
from rowers.tasks import handle_sendemail_invite
from django.conf import settings
from rowers.models import (
Rower, Workout, Team, TeamInvite,User
)
from rowers.tasks import (
handle_remove_workouts_team,handle_sendemail_invite,
handle_add_workouts_team
)
# Low level functions - to be called by higher level methods
inviteduration = 14 # days
def create_team(name,manager,notes=''):
def create_team(name,manager,private='open',notes=''):
# needs some error testing
try:
t = Team(name=name,manager=manager,notes=notes)
t = Team(name=name,manager=manager,notes=notes,
private=private)
t.save()
r = Rower.objects.get(user=manager)
r.team.add(t)
@@ -52,8 +57,12 @@ def add_member(id,rower):
rower.team.add(t)
# code to add all workouts
ws = Workout.objects.filter(user=rower)
for w in ws:
w.team.add(t)
if settings.DEBUG:
res = handle_add_workouts_team(ws,t)
else:
res = queuehigh.enqueue(handle_add_workouts_team,ws,t)
set_teamplanexpires(rower)
@@ -63,10 +72,13 @@ def remove_member(id,rower):
t = Team.objects.get(id=id)
rower.team.remove(t)
# remove the team from rower's workouts:
ws = Workout.objects.filter(user=rower)
for w in ws:
w.team.remove(t)
ws = Workout.objects.filter(user=rower,team=t)
if settings.DEBUG:
res = handle_remove_workouts_team(ws,t)
else:
res = queuehigh.enqueue(handle_remove_workouts_team,ws,t)
set_teamplanexpires(rower)
return (1,'Member removed')
@@ -110,7 +122,7 @@ def get_team_workouts(id):
# Medium level functionality
def create_invite(team,manager,user=None):
def create_invite(team,manager,user=None,email=''):
r = Rower.objects.get(user=manager)
if team.manager != manager:
return (0,'Not the team manager')
@@ -121,6 +133,9 @@ def create_invite(team,manager,user=None):
return (0,'Rower does not exist')
if r2 in Rower.objects.filter(team=team):
return (0,'Already member of that team')
elif email==None or email=='':
return (0,'Invalid request - missing email or user')
if count_club_members(team.manager)+count_invites(team.manager) < r.clubsize:
codes = [i.code for i in TeamInvite.objects.all()]
code = uuid.uuid4().hex[:10].upper()
@@ -128,7 +143,7 @@ def create_invite(team,manager,user=None):
while code in codes:
code = uuid.uuid4().hex[:10].upper()
invite = TeamInvite(team=team,code=code,user=user)
invite = TeamInvite(team=team,code=code,user=user,email=email)
invite.save()
return (invite.id,'Invitation created')
@@ -144,6 +159,7 @@ def revoke_invite(id):
return (1,'Invitation revoked')
def send_invite_email(id):
invitation = TeamInvite.objects.get(id=id)
if invitation.user:
@@ -157,15 +173,14 @@ def send_invite_email(id):
manager = invitation.team.manager.first_name+' '+invitation.team.manager.last_name
if settings.DEBUG:
res = handle_sendemail_invite(email,name,code,teamname,manager)
res = handle_sendemail_invite.delay(email,name,code,teamname,manager)
else:
res = queue.enqueue(handle_sendemail_invite(email,name,code,
teamname,
manager))
queue.enqueue(handle_sendemail_invite,email,name,code,teamname,manager)
return (1,'Invitation email sent')
def process_invite_code(user,code):
code = code.upper()
try:
invitation = TeamInvite.objects.get(code=code)
except TeamInvite.DoesNotExist: