Ejemplo n.º 1
0
def join(request,event_id,team_id=None,*args,**kwargs):
    show_teams = kwargs.get('show_teams')
    event = get_object_or_404(Event,pk=event_id)
    user = request.user
    is_team_admin = False
    context = {'tid' : team_id or 0}
    #Check is a team admin
    if show_teams is None:
        team_admin = Team.objects.filter(admin=user).values('id')
        is_team_admin = (len(team_admin) != 0)
        members = None
    else:
        team_admin = Team.objects.filter(admin=user).values('id','name')
        context.update({'team_select':team_admin})
        if team_id is None:
            team_id = team_admin[0]['id']
            team_name = team_admin[0]['name']
        if team_id is not None:
            team = get_object_or_404(Team,pk=team_id,admin=request.user)
            context.update({'tid':team_id})
            context.update({'tname':team.name})
            members = team.members.all().values('id','first_name','last_name')
            members = [(i['id'], "{} {}".format(i['first_name'],i['last_name'])) for i in members]
    
    if request.method == "POST":
        se = ServiceEnrollment(user=user,event=event)
        form = ServiceEnrollmentForm(request.POST.copy(),instance=se,prefix="time")
        team_form = TeamForm(request.POST.copy(),members=members,prefix="team")
        
        # Single User
        if team_id is None and form.is_valid():
            form.save()
            return redirect("/account/events/")
        
        #Team
        if team_id and form.is_valid() and team_form.is_valid():
            for (member_id) in team_form.get_selected_members():
                se = ServiceEnrollment(user_id=member_id,event=event,team=team)
                member_form = ServiceEnrollmentForm(request.POST.copy(),instance=se,prefix="time")
                if member_form.is_valid():
                    member_form.save()
            return redirect(team)
    else:
        form = ServiceEnrollmentForm(prefix="time")
        team_form = None
        if team_id:
            team_form = TeamForm(prefix="team",members=members)
    context.update({'tid':int(context['tid'])})
    context.update({'event':event, 'user':user, 'form':form, 'team_form':team_form, 'is_team_admin':is_team_admin})
    return render(request,'join_event.djhtml',context)
Ejemplo n.º 2
0
def join(request,event_id,team_id=None,*args,**kwargs):
    """
    Whean a team wants to join an event
    """
    show_teams = kwargs.get('show_teams')
    event = get_object_or_404(Event,pk=event_id)
    user = request.user
    is_team_admin = False
    context = {'tid' : team_id or 0}
    #Check is a team admin
    if show_teams is None:
        #Get the Team ids that this user administers
        """
        SELECT `team_team`.`id` FROM `team_team` WHERE `team_team`.`admin_id` = 1 
        """
        team_admin = Team.objects.filter(admin=user).values('id')
        print team_admin.query.__str__()
        
        is_team_admin = (len(team_admin) != 0)
        members = None
    else:
        team_admin = Team.objects.filter(admin=user).values('id','name')
        context.update({'team_select':team_admin})
        if team_id is None:
            team_id = team_admin[0]['id']
            team_name = team_admin[0]['name']
        if team_id is not None:
            team = get_object_or_404(Team,pk=team_id,admin=request.user)
            context.update({'tid':team_id})
            context.update({'tname':team.name})
            
            #Get the names and ids of all the members
            """
            SELECT `auth_user`.`id`, `auth_user`.`first_name`, `auth_user`.`last_name` FROM `auth_user`
            INNER JOIN `team_teammembership` ON (`auth_user`.`id` = `team_teammembership`.`member_id`)
            WHERE `team_teammembership`.`team_id` = 1 
            """
            members = team.members.all().values('id','first_name','last_name')
            print members.query.__str__()
            members = [(i['id'], "{} {}".format(i['first_name'],i['last_name'])) for i in members]
    
    if request.method == "POST":
        se = ServiceEnrollment(user=user,event=event)
        form = ServiceEnrollmentForm(request.POST.copy(),instance=se,prefix="time")
        team_form = TeamForm(request.POST.copy(),members=members,prefix="team")
        
        # Single User
        if team_id is None and form.is_valid():
            form.save()
            return redirect("/account/events/")
        
        #Team
        # Insert an enrollment for each team member
        if team_id and form.is_valid() and team_form.is_valid():
            for (member_id) in team_form.get_selected_members():
                se = ServiceEnrollment(user_id=member_id,event=event,team=team)
                member_form = ServiceEnrollmentForm(request.POST.copy(),instance=se,prefix="time")
                if member_form.is_valid():
                    member_form.save()
            return redirect(team)
    else:
        form = ServiceEnrollmentForm(prefix="time")
        team_form = None
        if team_id:
            team_form = TeamForm(prefix="team",members=members)
    context.update({'tid':int(context['tid'])})
    context.update({'event':event, 'user':user, 'form':form, 'team_form':team_form, 'is_team_admin':is_team_admin})
    return render(request,'join_event.djhtml',context)