Example #1
0
def start_following_view(request):
    """
    Current role (fan) starts following entity in request
    :param request: a POST request
    :return: the response from the KrowdIO API on success,
        or an HttpResponseNotAllowed response
    """
    if request.method == 'POST':
        origin = str(request.POST.get('origin', ''))
        entity_id = entity_type = None
        tag = str(request.POST.get('tag', None)) or request.POST['base_tag']  # if empty use the entity name
        if re.match(r'/venues/view/\d+', origin):
            entity_type = EntityController.ENTITY_VENUE
            entity_id = str.split(origin, '/')[-1]
        elif re.match(r'/fan/\d+/edit', origin):
            invitation_id = request.session.pop('invitation_id')
            if invitation_id:
                try:
                    invitation = Invitation.objects.get(id=invitation_id)
                    if invitation.invitation_type == Invitation.REGISTER_AND_ADMINISTRATE_TEAM_INVITATION:
                        entity_id = invitation.target_entity_id
                        entity_type = EntityController.ENTITY_TEAM
                    else:
                        raise Invitation.DoesNotExist()
                except Invitation.DoesNotExist:
                    return HttpResponseBadRequest()
        elif re.match(r'/fan/\d+', origin):
            entity_type = RoleController.ENTITY_FAN
            entity_id = str.split(origin, '/')[-1]
        elif re.match(r'/team/\d+', origin):
            entity_type = EntityController.ENTITY_TEAM
            entity_id = str.split(origin, '/')[2]
            team = TeamPage.objects.get(id=entity_id)
            admin = TeamAdministrator.objects.get(team_page=team,
                                                  entity_id=request.current_role.entity.id,
                                                  entity_type=request.current_role.entity_type)
            if admin.entity_type == 'student':
                stu = Student.objects.get(id=admin.entity_id)
                team_gained_follower(stu)

        entity_tag = FanFollowingEntityTag(
            fan=request.current_role.entity,
            tag=tag,
            entity_id=entity_id,
            entity_type=entity_type)
        entity_tag.save()

        json = start_following(request.current_role, entity_type, entity_id)
        return HttpResponse(simplejson.dumps(json))
    else:
        return HttpResponseNotAllowed(['POST'])
Example #2
0
def register_club(request):
    initial_data = dict(request.GET.iteritems())
    if request.current_role.entity_type == RoleController.ENTITY_FAN:
        initial_data['state'] = request.current_role.entity.state
    form = RegisterCreateClubForm(initial=initial_data)
    if request.method == 'POST':
        form = RegisterCreateClubForm(data=request.POST)
        if form.is_valid():
            data = form.cleaned_data
            name = data.get('name')
            at_name = data.get('at_name')
            sport = data.get('sport')
            description = data.get('description')
            state = data.get('state')
            address = data.get('address')
            club = Club(name=name, address=address, description=description, state=state)
            club.save()
            club_admin = ClubAdministrator(club=club, admin=request.user)
            club_admin.save()
            change_current_role(request, RoleController.ENTITY_CLUB_ADMIN, club_admin.id)
            team = TeamPage(name=name, at_name=at_name, free_text=description, state=state, sport=sport)
            team.save()
            team_admin = TeamAdministrator(
                entity_type=request.current_role.entity_type,
                entity_id=request.current_role.entity.id,
                team_page=team)
            team_admin.save()
            tca = TeamClubAssociation(team_page=team, club=club)
            tca.save()
            following_tag = FanFollowingEntityTag(
                fan=request.current_role.entity,
                tag=at_name,
                entity_id=team.id,
                entity_type=EntityController.ENTITY_TEAM)
            following_tag.save()
            start_following(request.current_role, EntityController.ENTITY_TEAM, team.id)
            return redirect('/club/dashboard?message=just_registered')
    return render(
        request,
        'spudderspuds/challenges/pages/register_create_club.html',
        {'form': form})