def setUp(self): self.user1 = User.objects.create(username='******') self.user2 = User.objects.create(username='******') self.sponsor_page = SponsorPage(sponsor=self.user2) self.sponsor_page.save() self.venue = Venue(user=self.user1) self.venue.save()
class IsVenueAvailable(FormattedOutputTestCase): def setUp(self): self.user1 = User.objects.create(username='******') self.user2 = User.objects.create(username='******') self.sponsor_page = SponsorPage(sponsor=self.user2) self.sponsor_page.save() self.venue = Venue(user=self.user1) self.venue.save() def tearDown(self): self.user1.delete() self.user2.delete() def test_price_is_not_specified(self): self.venue.price = 0.0 self.assertFalse(self.venue.is_available()) def test_price_is_set_and_nobody_rented_it(self): self.venue.price = 2.0 self.assertTrue(self.venue.is_available()) def test_somebody_rented_venue_but_transaction_is_pending(self): self.venue.price = 2.0 PendingVenueRental(venue=self.venue).save() self.assertFalse(self.venue.is_available()) def test_price_is_set_and_somebody_rented_it(self): self.venue.price = 2 self.venue.renter = self.sponsor_page self.assertFalse(self.venue.is_available())
def _process_amazon_login(access_token, amazon_user_email, amazon_user_id, request, amazon_user_name=None): # Check to see if we need to accomodate pre V1.1.0 accounts _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email, amazon_user_id) # Check to see if these credential are tied to a user role via an authentication service user_role = select_role_by_authentication_service(LinkedServiceController.SERVICE_AMAZON, amazon_user_id) next_url = None # If there is a role then get the user, else get the currently authenticate user else create a new user if user_role and user_role.user: # Case - Amazon Login to existing account user = user_role.user # Get the role controller for this user role_controller = RoleController(user) else: # First get the user or create one if request.user.is_authenticated(): # Case - Add new amazon login based role to existing account user = request.user # Make a note on the spudder users that as there are multiple roles a password needs to be set for this user user.spudder_user.mark_as_password_required() else: # Case - first time registration user = User.objects.create_user(amazon_user_email, amazon_user_email, amazon_user_id) user.save() # If there is a school id then this is a student registration school_id = request.GET.get('school_id', None) if school_id: # Create the student student = create_student(user, school_id, request.GET.get('referrer', None)) # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_STUDENT, student.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is sponsor, create a SponsorPage account_type = get_request_param(request, 'account_type') if account_type == 'sponsor': # Create sponsor sponsor = SponsorPage(sponsor=user) sponsor.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_SPONSOR, sponsor.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_SPONSOR)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is fan, create a FanPage if account_type == 'fan': # Create fan fan, _ = FanPage.objects.get_or_create(fan=user) fan.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is club, create a Club entity in first stage (before registration as recipient) if account_type == 'club': inv = None if request.session['invitation_id']: inv = Invitation.objects.get(id=request.session['invitation_id']) if str(amazon_user_name) != str(TempClub.objects.get(id=inv.target_entity_id).name): return HttpResponseRedirect('/spudderaffiliates/invitation/%s/incorrect_name' % inv.id) club, _ = Club.objects.get_or_create( name=amazon_user_name, amazon_email=amazon_user_email, amazon_id=amazon_user_id ) if inv: club.affiliate = Affiliate.objects.get(name=inv.extras['affiliate_name']) club.save() club_admin, _ = ClubAdministrator.objects.get_or_create(admin=user, club=club) club_admin.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_CLUB_ADMIN, club_admin.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_CLUB_ADMIN)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) next_url = '/club/register/recipient' # Get and update the amazon auth record with the latest access token # Note that we don't use this at the moment but will in future - MG:20140708 amazon_auth = get_authentication_wrapper(user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id) amazon_auth.update_amazon_access_token(access_token) if not next_url: next_url = request.GET.get('next', None) or user_role.home_page_path if not request.user.is_authenticated(): if user.spudder_user.has_set_password: return redirect('/users/account/signin/%s?next=%s' % (user.id, next_url)) user = authenticate( username=user_role.user.username, password=amazon_auth.user_password) if user: django.contrib.auth.login(request, user) else: raise Http404 logging.info('%s?next=%s',change_role_url(user_role),next_url) return redirect('%s?next=%s' % ( change_role_url(user_role), next_url))
def _process_amazon_login(access_token, amazon_user_email, amazon_user_id, request, amazon_user_name=None): # Check to see if we need to accomodate pre V1.1.0 accounts _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email, amazon_user_id) # Check to see if these credential are tied to a user role via an authentication service user_role = select_role_by_authentication_service( LinkedServiceController.SERVICE_AMAZON, amazon_user_id) next_url = None # If there is a role then get the user, else get the currently authenticate user else create a new user if user_role and user_role.user: # Case - Amazon Login to existing account user = user_role.user # Get the role controller for this user role_controller = RoleController(user) else: # First get the user or create one if request.user.is_authenticated(): # Case - Add new amazon login based role to existing account user = request.user # Make a note on the spudder users that as there are multiple roles a password needs to be set for this user user.spudder_user.mark_as_password_required() else: # Case - first time registration user = User.objects.create_user(amazon_user_email, amazon_user_email, amazon_user_id) user.save() # If there is a school id then this is a student registration school_id = request.GET.get('school_id', None) if school_id: # Create the student student = create_student(user, school_id, request.GET.get('referrer', None)) # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_STUDENT, student.id, RoleBase.RoleWrapperByEntityType( RoleController.ENTITY_STUDENT)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is sponsor, create a SponsorPage account_type = get_request_param(request, 'account_type') if account_type == 'sponsor': # Create sponsor sponsor = SponsorPage(sponsor=user) sponsor.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_SPONSOR, sponsor.id, RoleBase.RoleWrapperByEntityType( RoleController.ENTITY_SPONSOR)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is fan, create a FanPage if account_type == 'fan': # Create fan fan, _ = FanPage.objects.get_or_create(fan=user) fan.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) # If the account_type is club, create a Club entity in first stage (before registration as recipient) if account_type == 'club': inv = None if request.session['invitation_id']: inv = Invitation.objects.get( id=request.session['invitation_id']) if str(amazon_user_name) != str( TempClub.objects.get(id=inv.target_entity_id).name): return HttpResponseRedirect( '/spudderaffiliates/invitation/%s/incorrect_name' % inv.id) club, _ = Club.objects.get_or_create( name=amazon_user_name, amazon_email=amazon_user_email, amazon_id=amazon_user_id) if inv: club.affiliate = Affiliate.objects.get( name=inv.extras['affiliate_name']) club.save() club_admin, _ = ClubAdministrator.objects.get_or_create(admin=user, club=club) club_admin.save() # Get the Role controller for the current user role_controller = RoleController(user) # Get the user_role for this user/student combo user_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_CLUB_ADMIN, club_admin.id, RoleBase.RoleWrapperByEntityType( RoleController.ENTITY_CLUB_ADMIN)) # Store the amazon linked authentication service details create_linked_authentication_service( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id, { 'amazon_user_email': amazon_user_email, 'amazon_user_id': amazon_user_id, 'amazon_access_token': access_token }) next_url = '/club/register/recipient' # Get and update the amazon auth record with the latest access token # Note that we don't use this at the moment but will in future - MG:20140708 amazon_auth = get_authentication_wrapper( user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id) amazon_auth.update_amazon_access_token(access_token) if not next_url: next_url = request.GET.get('next', None) or user_role.home_page_path if not request.user.is_authenticated(): if user.spudder_user.has_set_password: return redirect('/users/account/signin/%s?next=%s' % (user.id, next_url)) user = authenticate(username=user_role.user.username, password=amazon_auth.user_password) if user: django.contrib.auth.login(request, user) else: raise Http404 logging.info('%s?next=%s', change_role_url(user_role), next_url) return redirect('%s?next=%s' % (change_role_url(user_role), next_url))