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 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 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 krowdio_users_to_links(can_edit, current_role, krowdio_dict, filter=None): """ Translates KrowdIO users into a list of dicts about Spudder entities :param krowdio_dict: the 'users' part of a response from the KrowdIO API (intended for use with followers/following) :return: a list of dicts, where the dict contains the icon link, profile link, and name of each Spudder entity followed """ users = [] for user in krowdio_dict: krowdio_id = user['_id'] try: storage_obj = KrowdIOStorage.objects.get( krowdio_user_id=krowdio_id) except KrowdIOStorage.DoesNotExist: continue if storage_obj.role_type == RoleController.ENTITY_FAN and \ (filter == RoleController.ENTITY_FAN or filter is None): fan = FanPage.objects.get(id=storage_obj.role_id) if fan.avatar: icon_link = '/file/serve/%s' % fan.avatar.id else: icon_link = '/static/img/spudderfans/button-fans-tiny.png' users.append({ 'name': fan.name, 'profile': '/fan/%s' % fan.id, 'icon_link': icon_link, 'custom_tag': FanFollowingEntityTag.GetTag( fan=current_role.entity, entity_id=fan.id, entity_type=RoleController.ENTITY_FAN) if can_edit else None }) elif storage_obj.role_type == RoleController.ENTITY_SPONSOR and \ (filter == RoleController.ENTITY_SPONSOR or filter is None): sponsor = SponsorPage.objects.get(id=storage_obj.role_id) if sponsor.thumbnail: icon_link = '/file/serve/%s' % sponsor.thumbnail else: icon_link = '/static/img/spuddersponsors/button-sponsors-tiny.png' users.append({ 'name': sponsor.name, 'profile': '/sponsor/%s' % sponsor.id, 'icon_link': icon_link, 'custom_tag': FanFollowingEntityTag.GetTag( fan=current_role.entity, entity_id=sponsor.id, entity_type=RoleController.ENTITY_SPONSOR) if can_edit else None }) elif storage_obj.role_type == RoleController.ENTITY_STUDENT and \ (filter == RoleController.ENTITY_STUDENT or filter is None): stu = Student.objects.get(id=storage_obj.role_id) if stu.logo: icon_link = '/file/serve/%s' % stu.logo else: icon_link = 'static/img/spuddercern/button-cern-tiny.png' users.append({ 'name': user_name(stu.user), 'profile': '/cern/student/%s' % stu.id, 'icon_link': icon_link, 'custom_tag': FanFollowingEntityTag.GetTag( fan=current_role.entity, entity_id=stu.id, entity_type=RoleController.ENTITY_STUDENT) if can_edit else None }) elif storage_obj.venue and \ (filter == EntityController.ENTITY_VENUE or filter is None): if storage_obj.venue.logo: icon_link = '/file/serve/%s' % storage_obj.venue.logo.id else: icon_link = '/static/img/spudderspuds/button-spuds-tiny.png' users.append({ 'name': storage_obj.venue.aka_name, 'profile': '/venues/view/%s' % storage_obj.venue.id, 'icon_link': icon_link, 'custom_tag': FanFollowingEntityTag.GetTag( fan=current_role.entity, entity_id=storage_obj.venue.id, entity_type=EntityController.ENTITY_VENUE) if can_edit else None }) elif storage_obj.team and \ (filter == EntityController.ENTITY_TEAM or filter is None): if storage_obj.team.image: icon_link = '/file/serve/%s' % storage_obj.team.image.id else: icon_link = '/static/img/spudderspuds/button-teams-tiny.png' users.append({ 'name': storage_obj.team.name, 'profile': '/team/%s' % storage_obj.team.id, 'icon_link': icon_link, 'custom_tag': FanFollowingEntityTag.GetTag( fan=current_role.entity, entity_id=storage_obj.team.id, entity_type=EntityController.ENTITY_TEAM) if can_edit else None }) return users