def setUp(self): self.client = TestClient(middleware) self.entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******') self.invitation = self.entrant.invite() self.path = '/invitation/{}'.format(self.invitation.code)
def test_successfully_authenticates_user_from_github(self): response = self.client.get('/callback', {'code': 5}) self.assertEqual(response.status_code, 200) entrant = Entrant.select().where( Entrant.email == '*****@*****.**').get() self.assertEqual(entrant.name, 'kylef') entrant.delete_instance()
def callback(request): code = request.GET.get('code') if not code: error = request.GET.get('error') error_description = request.GET.get('error_description', 'Unknown Error, please try again') return JinjaResponse( request, template_names=['error.html'], context={'reason': '{} ({})'.format(error_description, error)}) access_token = github.retrieve_access_token(code) if not access_token: return ResponseRedirect('https://sotu.cocoapods.org/') user = github.retrieve_account(access_token) email = github.retrieve_email(access_token) username = user['login'] name = user.get('name', username) if name is None or len(name) == 0: name = username avatar = user.get('avatar_url', None) try: entrant = Entrant.select().where( Entrant.github_username == username).get() except Entrant.DoesNotExist: entrant = Entrant.create(github_username=username, name=name, email=email) try: invitation = entrant.invitation_set.get() except Invitation.DoesNotExist: invitation = None if invitation: if invitation.state == Invitation.ACCEPTED_STATE: return ResponseRedirect(invitation.accept_url) elif invitation.state == Invitation.REJECTED_STATE: return ResponseRedirect(invitation.reject_url) elif invitation.state == Invitation.INVITED_STATE: return ResponseRedirect(invitation.invited_url) return EntrantView.as_view(entrant=entrant, avatar=avatar)(request)
def invite_remaining(): entrants = Entrant.select().order_by(peewee.fn.Random()) entrants = filter(lambda e: e.invitation_set.count() == 0, entrants) for entrant in entrants: print(entrant.github_username) invitation = entrant.invite() send_remaining_invite(invitation)
def lottery(amount): amount = int(amount) entrants = Entrant.select().order_by(peewee.fn.Random()).join(Invitation, peewee.JOIN.LEFT_OUTER).group_by(Entrant).having(peewee.fn.COUNT(Invitation.id) == 0).limit(amount) print('Inviting {} entrants.'.format(entrants.count())) for entrant in entrants: print(entrant.github_username) invitation = entrant.invite() send_invitation(invitation)
def status(): entrants = Entrant.select().count() invited = Invitation.select().where(Invitation.state == Invitation.INVITED_STATE).count() accepted = Invitation.select().where(Invitation.state == Invitation.ACCEPTED_STATE).count() rejected = Invitation.select().where(Invitation.state == Invitation.REJECTED_STATE).count() print('Entrants: {}\n---'.format(entrants)) print('Invited: {}'.format(invited)) print('Accepted: {}'.format(accepted)) print('Rejected: {}'.format(rejected))
def lottery(amount): amount = int(amount) entrants = Entrant.select().order_by(peewee.fn.Random()).join( Invitation, JOIN.LEFT_OUTER).group_by(Entrant).having( fn.COUNT(Invitation.id) == 0).limit(amount) print('Inviting {} entrants.'.format(len(entrants))) for entrant in entrants: print(entrant.github_username) invitation = entrant.invite() send_invitation(invitation)
def status(): entrants = Entrant.select().count() invited = Invitation.select().where( Invitation.state == Invitation.INVITED_STATE).count() accepted = Invitation.select().where( Invitation.state == Invitation.ACCEPTED_STATE).count() rejected = Invitation.select().where( Invitation.state == Invitation.REJECTED_STATE).count() print('Entrants: {}\n---'.format(entrants)) print('Invited: {}'.format(invited)) print('Accepted: {}'.format(accepted)) print('Rejected: {}'.format(rejected))
def test_with_invitation(self): entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******') invitation = entrant.invite() invitation.save() response = self.client.get('/callback', {'code': 5}) self.assertEqual(response.status_code, 302) self.assertEqual( response.headers['Location'], 'https://sotu.cocoapods.org/invitation/{}'.format(invitation.code) ) entrant.delete_instance() invitation.delete_instance()
def status_view(request): status = { 'entrants': Entrant.select().count(), 'invited': Invitation.select().where( Invitation.state == Invitation.INVITED_STATE).count(), 'accepted': Invitation.select().where( Invitation.state == Invitation.ACCEPTED_STATE).count(), 'rejected': Invitation.select().where( Invitation.state == Invitation.REJECTED_STATE).count(), } return Response(json.dumps(status), content_type='application/json')
def test_with_invitation(self): entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******') invitation = entrant.invite() invitation.save() response = self.client.get('/callback', {'code': 5}) self.assertEqual(response.status_code, 302) self.assertEqual( response.headers['Location'], 'https://sotu.cocoapods.org/invitation/{}'.format(invitation.code)) entrant.delete_instance() invitation.delete_instance()
def invite(username, force=False): try: entrant = Entrant.select().where(Entrant.github_username == username).get() except Entrant.DoesNotExist: print('Username {} does not exist.'.format(username)) return if entrant.invitation_set.exists() and not force: print('{} already has an invitation.'.format(username)) return if force: invitation = entrant.invitation_set.get() else: invitation = entrant.invite() send_invitation(invitation)
def invite(username, force=False): try: entrant = Entrant.select().where( Entrant.github_username == username).get() except Entrant.DoesNotExist: print('Username {} does not exist.'.format(username)) return if entrant.invitation_set.exists() and not force: print('{} already has an invitation.'.format(username)) return if force: invitation = entrant.invitation_set.get() else: invitation = entrant.invite() send_invitation(invitation)
def migrate(): Entrant.create_table() Invitation.create_table()
def create_entrant(number): return Entrant.create( email='kyle_{}@cocoapods.org'.format(number), name='Kyle', github_username='******'.format(number) )
def test_successfully_authenticates_user_from_github(self): response = self.client.get('/callback', {'code': 5}) self.assertEqual(response.status_code, 200) entrant = Entrant.select().where(Entrant.email == '*****@*****.**').get() self.assertEqual(entrant.name, 'kylef') entrant.delete_instance()
def create_entrant(number): return Entrant.create(email='kyle_{}@cocoapods.org'.format(number), name='Kyle', github_username='******'.format(number))