Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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()
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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))
Ejemplo n.º 10
0
    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()
Ejemplo n.º 11
0
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')
Ejemplo n.º 12
0
    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()
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
def migrate():
    Entrant.create_table()
    Invitation.create_table()
Ejemplo n.º 16
0
 def create_entrant(number):
     return Entrant.create(
         email='kyle_{}@cocoapods.org'.format(number),
         name='Kyle', github_username='******'.format(number)
     )
Ejemplo n.º 17
0
def migrate():
    Entrant.create_table()
    Invitation.create_table()
Ejemplo n.º 18
0
 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()
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 def create_entrant(number):
     return Entrant.create(email='kyle_{}@cocoapods.org'.format(number),
                           name='Kyle',
                           github_username='******'.format(number))