def AssociateTeamWithVenue(cls, team, venue): """ Start a two way following between team and venue """ team_entity = EntityTeam(team) venue_entity = EntityVenue(venue) start_following(team_entity, venue_entity.entity_type, venue_entity.entity.id) start_following(venue_entity, team_entity.entity_type, team_entity.entity.id)
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})
def system_teams(request): template_data = {} delete_teams_form = PasswordAndActionForm( initial={'action': "teams_delete"}) following_teams_form = PasswordAndActionForm( initial={'action': 'following_teams'}) if request.method == "POST": action = request.POST['action'] if action == "teams_delete": delete_teams_form = PasswordAndActionForm(request.POST) if delete_teams_form.is_valid(): for team in TeamPage.objects.all(): TeamAdministrator.objects.filter(team_page=team).delete() TeamVenueAssociation.objects.filter( team_page=team).delete() TeamPage.objects.all().delete() messages.success(request, 'Teams deleted') if action == "following_teams": following_teams_form = PasswordAndActionForm(request.POST) if following_teams_form.is_valid(): for team in TeamPage.objects.all(): for admin in TeamAdministrator.objects.filter( team_page=team): if admin.entity_type == RoleController.ENTITY_FAN: try: fan = FanPage.objects.get(id=admin.entity_id) if not FanFollowingEntityTag.objects.filter( fan=fan).count(): tag = team.at_name or ''.join( [c for c in team.name if c.isalnum()]) FanFollowingEntityTag( fan=fan, tag=tag, entity_id=team.id, entity_type=EntityController. ENTITY_TEAM).save() start_following( RoleFan(fan), EntityController.ENTITY_TEAM, team.id) except FanPage.DoesNotExist: continue messages.success(request, 'Team followings up to date') template_data['delete_teams_form'] = delete_teams_form template_data['following_teams_form'] = following_teams_form return render_to_response('spudderadmin/pages/system/teams.html', template_data, context_instance=RequestContext(request))
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})
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'])
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'])
def create_and_activate_fan_role(request, user): # get or create fan role fan, created = FanPage.objects.get_or_create(fan=user) fan.save() # set new users to follow [email protected] fan if feature_is_enabled('all_fans_auto_follow_main_spudder_fan') and created: main_account = User.objects.get( email=settings.MAIN_SPUDDER_FAN_ACCOUNT_EMAIL) main_fan = FanPage.objects.get(fan=main_account) current_entity = RoleController.GetRoleForEntityTypeAndID( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) start_following(current_entity, RoleController.ENTITY_FAN, main_fan.id) # activate role role_controller = RoleController(user) role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) change_current_role(request, RoleController.ENTITY_FAN, fan.id) return RoleFan(fan)
def system_teams(request): template_data = {} delete_teams_form = PasswordAndActionForm(initial={'action': "teams_delete"}) following_teams_form = PasswordAndActionForm(initial={'action': 'following_teams'}) if request.method == "POST": action = request.POST['action'] if action == "teams_delete": delete_teams_form = PasswordAndActionForm(request.POST) if delete_teams_form.is_valid(): for team in TeamPage.objects.all(): TeamAdministrator.objects.filter(team_page=team).delete() TeamVenueAssociation.objects.filter(team_page=team).delete() TeamPage.objects.all().delete() messages.success(request, 'Teams deleted') if action == "following_teams": following_teams_form = PasswordAndActionForm(request.POST) if following_teams_form.is_valid(): for team in TeamPage.objects.all(): for admin in TeamAdministrator.objects.filter(team_page=team): if admin.entity_type == RoleController.ENTITY_FAN: try: fan = FanPage.objects.get(id=admin.entity_id) if not FanFollowingEntityTag.objects.filter(fan=fan).count(): tag = team.at_name or ''.join([c for c in team.name if c.isalnum()]) FanFollowingEntityTag( fan=fan, tag=tag, entity_id=team.id, entity_type=EntityController.ENTITY_TEAM).save() start_following(RoleFan(fan), EntityController.ENTITY_TEAM, team.id) except FanPage.DoesNotExist: continue messages.success(request, 'Team followings up to date') template_data['delete_teams_form'] = delete_teams_form template_data['following_teams_form'] = following_teams_form return render_to_response( 'spudderadmin/pages/system/teams.html', template_data, context_instance=RequestContext(request))
def create_and_activate_fan_role(request, user): # get or create fan role fan, created = FanPage.objects.get_or_create(fan=user) fan.save() # set new users to follow [email protected] fan if feature_is_enabled('all_fans_auto_follow_main_spudder_fan') and created: main_account = User.objects.get(email=settings.MAIN_SPUDDER_FAN_ACCOUNT_EMAIL) main_fan = FanPage.objects.get(fan=main_account) current_entity = RoleController.GetRoleForEntityTypeAndID( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) start_following(current_entity, RoleController.ENTITY_FAN, main_fan.id) # activate role role_controller = RoleController(user) role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) change_current_role(request, RoleController.ENTITY_FAN, fan.id) return RoleFan(fan)