示例#1
0
    def setUp(self):
        """Creates a few basic users.

        Alice is registered but not in beta
        Bob is registered and in beta (self-signup)
        Charlie is in beta and has one invite
        """
        self.alice = User.objects.create_user('alice', '*****@*****.**',
                                              'secret')
        self.bob = User.objects.create_user('bob', '*****@*****.**', 'secret')
        right_now = now()
        invitation = Invitation(user=self.bob,
                                invited=right_now,
                                used=right_now)
        invitation.save()

        self.charlie = User.objects.create_user('charlie',
                                                '*****@*****.**',
                                                'secret')
        invitation = Invitation(user=self.charlie,
                                invited=right_now,
                                used=right_now)
        invitation.save()
        code = InvitationCode(owner=self.charlie)
        code.save()
示例#2
0
 def create_code(self, private=True, email=''):
     code = InvitationCode(private=private)
     code.save()
     if private:
         invitation = Invitation(code=code, email=email, invited=now())
         invitation.save()
     return code
示例#3
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        if not self.enable_beta:
            return

        if (request.path in self.allow_flatpages
                or (getattr(settings, 'APPEND_SLASH', True)
                    and '%s/' % request.path in self.allow_flatpages)):
            from django.contrib.flatpages.views import flatpage
            return flatpage(request, request.path_info)

        whitelisted_modules = [
            'django.contrib.auth.views', 'django.contrib.admin.sites',
            'django.views.static', 'django.contrib.staticfiles.views'
        ]

        # All hunger views, except NotBetaView, are off limits until in beta
        whitelisted_views = [
            'hunger.views.NotBetaView', 'hunger.views.verify_invite',
            'hunger.views.InvalidView'
        ]

        short_name = view_func.__class__.__name__
        if short_name == 'function':
            short_name = view_func.__name__
        view_name = self._get_view_name(request)

        full_view_name = '%s.%s' % (view_func.__module__, short_name)

        if self.always_allow_modules:
            whitelisted_modules += self.always_allow_modules

        if '%s' % view_func.__module__ in whitelisted_modules:
            return

        if self.always_allow_views:
            whitelisted_views += self.always_allow_views

        if (full_view_name in whitelisted_views
                or view_name in whitelisted_views):
            return

        if not request.user.is_authenticated():
            # Ask anonymous user to log in if trying to access in-beta view
            return redirect(setting('LOGIN_URL'))

        if request.user.is_staff:
            return

        # Prevent queries by caching in_beta status in session
        if request.session.get('hunger_in_beta'):
            return

        cookie_code = request.COOKIES.get('hunger_code')
        invitations = Invitation.objects.filter(
            Q(user=request.user)
            | Q(email=request.user.email)).select_related('code')

        # User already in the beta - cache in_beta in session
        if any([i.used for i in invitations if i.invited]):
            request.session['hunger_in_beta'] = True
            return

        # User has been invited - use the invitation and place in beta.
        activates = [i for i in invitations if i.invited and not i.used]

        # Check for matching cookie code if available.
        if cookie_code:
            for invitation in activates:
                if invitation.code.code == cookie_code:
                    # Invitation may be attached to email
                    invitation.user = request.user
                    invitation.used = now()
                    invitation.save()
                    request.session['hunger_in_beta'] = True
                    request._hunger_delete_cookie = True
                    return

        # No cookie - let's just choose the first invitation if it exists
        if activates:
            invitation = activates[0]
            # Invitation may be attached to email
            invitation.user = request.user
            invitation.used = now()
            invitation.save()
            request.session['hunger_in_beta'] = True
            return

        if not cookie_code:
            if not invitations:
                invitation = Invitation(user=request.user)
                invitation.save()
            return redirect(self.redirect)

        # No invitation, all we have is this cookie code
        try:
            code = InvitationCode.objects.get(code=cookie_code,
                                              num_invites__gt=0)
        except InvitationCode.DoesNotExist:
            request._hunger_delete_cookie = True
            return redirect(reverse('hunger-invalid', args=(cookie_code, )))

        right_now = now()
        if code.private:
            # If we got here, we're trying to fix up a previous private
            # invitation to the correct user/email.
            invitation = Invitation.objects.filter(code=code)[0]
            invitation.user = request.user
            invitation.invited = right_now
            invitation.used = right_now
            code.num_invites = 0
        else:
            invitation = Invitation(user=request.user,
                                    code=code,
                                    invited=right_now,
                                    used=right_now)
            code.num_invites -= 1
        invitation.save()
        code.save()
        return
示例#4
0
 def create_invite(self, email):
     code = InvitationCode(num_invites=0)
     code.save()
     invitation = Invitation(code=code, email=email, invited=now())
     invitation.save()
     return invitation