def account_reset_password(request): if request.method == 'GET': password_form = ResetPasswordForm(initial=request.GET) if not password_form.is_valid_token(): return HttpResponseNotFound() if request.method == 'POST': password_form = ResetPasswordForm(request.POST) if not password_form.is_valid_token(): return HttpResponseNotFound() if password_form.is_valid(): role = RoleController.GetRoleForEntityTypeAndID( password_form.notification.target_entity_type, password_form.notification.target_entity_id, RoleBase.RoleWrapperByEntityType( password_form.notification.target_entity_type)) user = role.user user.set_password(password_form.cleaned_data.get('password_1')) user.save() user.spudder_user.mark_password_as_done() messages.success(request, "Your new password was saved!") # remove notification password_form.notification.delete() return redirect('/challenges/signin') return render_to_response('spudderaccounts/pages/create_password.html', { 'password_form': password_form, 'reset_password': True }, context_instance=RequestContext(request))
def send_message(request, page_id): """ Sends a message to the team manager :param request: a POST request with message body :param team_id: a valid ID of a TeamPage object :return: a blank HttpResponse on success """ if request.method == 'POST': team = TeamPage.objects.get(id=page_id) admin = TeamAdministrator.objects.filter(team_page=team)[0] entity = RoleController.GetRoleForEntityTypeAndID( admin.entity_type, admin.entity_id, RoleBase.RoleWrapperByEntityType(admin.entity_type)) email = entity.user.email details = team.contact_details if details and re.match(r'[\w\.]+\@[\w\.]+\.com$', details): email = details message = request.POST.get('message', '') if message: to = ['*****@*****.**', email] mail.send_mail(subject='Message from Spudder about Team: %s' % team.name, body=message, sender=settings.SERVER_EMAIL, to=to) return HttpResponse() else: return HttpResponseNotAllowed(['POST'])
def accounts_activate_role(request, entity_type, entity_id): role = RoleController.GetRoleForEntityTypeAndID( entity_type, entity_id, RoleBase.RoleWrapperByEntityType(entity_type)) change_current_role(request, entity_type, entity_id) next_url = request.GET.get('next', None) if next_url is None: next_url = role.home_page_path return redirect(next_url or '/users')
def select_role_by_authentication_service(service_type, unique_service_id): service_wrapper = LinkedServiceController.LinkedServiceByTypeAndID( service_type, unique_service_id, AuthenticationServiceBase.RoleWrapperByEntityType(service_type)) if not service_wrapper: return None return RoleController.GetRoleForEntityTypeAndID( service_wrapper.linked_service.role_type, service_wrapper.linked_service.role_id, RoleBase.RoleWrapperByEntityType( service_wrapper.linked_service.role_type))
def challenge_view(request, challenge_id): challenge = get_object_or_404(Challenge, id=challenge_id) template = challenge.template template_data = { 'challenge': challenge, 'template': template, 'owner': RoleController.GetRoleForEntityTypeAndID( challenge.creator_entity_type, challenge.creator_entity_id, RoleBase.EntityWrapperByEntityType(challenge.creator_entity_type)) } return render(request, 'spudderspuds/challenges/pages/challenge_view.html', template_data)
def change_current_role(request, entity_type=None, entity_id=None): if not entity_type or not entity_id: role_controller = RoleController(request.user) role = select_most_appropriate_user_role(role_controller) entity_type = role.entity_type entity_id = role.entity.id else: role = RoleController.GetRoleForEntityTypeAndID( entity_type, entity_id, RoleBase.EntityWrapperByEntityType(entity_type)) request.session['current_role'] = { 'entity_type': entity_type, 'entity_id': entity_id } return role
def get_entity_base_instanse_by_id_and_type(entity_id, entity_type): from spudderaccounts.wrappers import RoleBase from spudderdomain.controllers import RoleController, EntityController from spudderdomain.wrappers import EntityBase if entity_type in RoleController.ENTITY_TYPES: entity = RoleController.GetRoleForEntityTypeAndID( entity_type, entity_id, RoleBase.RoleWrapperByEntityType(entity_type)) elif entity_type in EntityController.ENTITY_TYPES: entity = EntityController.GetWrappedEntityByTypeAndId( entity_type, entity_id, EntityBase.EntityWrapperByEntityType(entity_type)) else: raise NotImplementedError("Entity type %s is not supported" % entity_type) return entity
def NotifyEntity(cls, target_entity_id, target_entity_type, notification_type, extras={}, notification_channels=[NOTIFY_BY_EMAIL]): notifications = Notification.objects.filter( target_entity_id=target_entity_id, target_entity_type=target_entity_type, notification_type=notification_type) created = False notify_after = extras.get('notify_after') for notification in notifications: if notification.extras.get('notify_after') == notify_after: created = True break if not created: notification = Notification(target_entity_id=target_entity_id, target_entity_type=target_entity_type, notification_type=notification_type, extras=extras) notification.save() for notification_channel in notification_channels: if notification_channel in cls.NOTIFICATION_CHANNELS: if notification_channel == cls.NOTIFY_BY_EMAIL: if notification_type == Notification.COMPLETE_CHALLENGE_NOTIFICATION and not created: entity = RoleController.GetRoleForEntityTypeAndID( target_entity_type, target_entity_id, RoleBase.RoleWrapperByEntityType( target_entity_type)) kwargs = {'notification': notification} CommunicationController.CommunicateWithEmail( emails=entity.contact_emails, **kwargs) else: raise NotImplementedError( 'Notification channel %s not implemented' % notification_channel) else: raise NotImplementedError( 'Notification channel %s not supported' % notification_channel)
def challenge_challenge_thanks(request, participation_id): participation = get_object_or_404(ChallengeChallengeParticipation, id=participation_id) creator = RoleController.GetRoleForEntityTypeAndID( participation.participating_entity_type, participation.participating_entity_id, RoleBase.EntityWrapperByEntityType( participation.participating_entity_type)) beneficiary = EntityController.GetWrappedEntityByTypeAndId( participation.recipient_entity_type, participation.recipient_entity_id, EntityBase.EntityWrapperByEntityType( participation.recipient_entity_type)) template_data = { 'participation': participation, 'creator': creator, 'beneficiary': beneficiary, 'just_submitted': request.GET.get('just_submitted') } return render( request, 'spudderspuds/challenges/pages/challenge_challenge_thanks.html', template_data)
def get_following_entities_by_entity_type(self, entity_type=None): tags_and_entities = [] for tag in FanFollowingEntityTag.objects.filter(fan=self.entity): if entity_type and not entity_type == tag.entity_type: continue if tag.entity_type == EntityController.ENTITY_TEAM: tags_and_entities.append({ 'tag': tag.tag, 'entity': EntityController.GetWrappedEntityByTypeAndId( tag.entity_type, tag.entity_id, EntityTeam) }) if tag.entity_type == RoleController.ENTITY_FAN: tags_and_entities.append({ 'tag': tag.tag, 'entity': RoleController.GetRoleForEntityTypeAndID( tag.entity_type, tag.entity_id, RoleFan) }) return tags_and_entities
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)