예제 #1
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
예제 #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 invite_email(request):
    email = request.POST['email']
    code = InvitationCode.objects.filter(owner=request.user)
    if code:
        code = code[0]
        if code.num_invites > 0:
            user = User.objects.filter(email=email)
            if user:
                user = user[0]
                inv = Invitation.objects.filter(user=user)
                if inv:
                    inv = inv[0]
                    if not inv.invited:
                        inv.invited = now()
                        inv.save()
                        code.num_invites -= 1
                        code.save()
            else:
                right_now = now()
                inv = Invitation(email=email, invited=right_now, created=right_now)
                inv.save()
                code.num_invites -= 1
                code.save()
            return HttpResponse("Invited!")    
    return HttpResponse("Not invited!")
예제 #4
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()
예제 #5
0
파일: views.py 프로젝트: skyl/django-hunger
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            verified_redirect = redirect(setting("HUNGER_VERIFIED_REDIRECT"))
            invitations = request.user.invitation_set.all()
            if any(i.used or i.invited for i in invitations):
                return verified_redirect

            elif invite_from_cookie_and_email(request):
                return verified_redirect

            elif not invitations:
                invitation = Invitation(
                    user=request.user,
                    email=request.user.email
                )
                invitation.save()

        return super(TemplateView, self).dispatch(request, *args, **kwargs)
예제 #6
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()
예제 #7
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
예제 #8
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
예제 #9
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
예제 #10
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 and 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:
            # User is trying to use a valid private code, but has no
            # authority to use it (neither via username nor email)
            request._hunger_delete_cookie = True
            return redirect(reverse('hunger-invalid', args=(cookie_code,)))
        else:
            invitation = Invitation(user=request.user,
                                    code=code,
                                    invited=right_now,
                                    used=right_now)
            code.num_invites -= 1
        invitation.save()
        code.save()
        return
예제 #11
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        #print 0
        if not self.enable_beta:
            return

        #print 1
        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
            #print "returning flatpage!"
            return flatpage(request, request.path_info)

        #print 2
        whitelisted_modules = ['django.contrib.auth.views',
                               'django.contrib.admin.sites',
                               'django.views.static',
                               'django.contrib.staticfiles.views',
                               'hunger.views']

        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:
            #print "whitelisted"
            return

        #print 3
        if (full_view_name in self.always_allow_views or
                view_name in self.always_allow_views):
            return

        #print 4
        if not request.user.is_authenticated():
            return redirect(self.redirect)

        #print 5
        if request.user.is_staff:
            return
        #print 6

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

        #print 7

        invitations = request.user.invitation_set.select_related('code')
        #print "USER", request.user, request.user.email
        #print "INVITATIONS", invitations

        if not invitations and not request.COOKIES.get('hunger_code'):
            #print "no invitations, no code for logged in user,"
            #print "make one and redirect"
            invitation = Invitation(
                user=request.user,
                email=request.user.email
            )
            invitation.save()
            return redirect(self.redirect)

        #print 8

        if any([i.used for i in invitations]):
            #print "some are used, therefore we are in Beta"
            request.session['hunger_in_beta'] = True
            return

        #print 9

        # User has been invited - use the invitation and place in beta.
        activates = [i for i in invitations if i.invited and not i.used]
        for invitation in activates:
            #print "let's activate"
            invitation.used = now()
            invitation.save()
            request.session['hunger_in_beta'] = True
            return

        #print 10
        # get from cookie, assume is authenticated and has email.
        invite = invite_from_cookie_and_email(request)
        if invite:
            invite.accept_invite(request.user)
            return
        else:
            return redirect(self.redirect)
예제 #12
0
파일: tests.py 프로젝트: skyl/django-hunger
 def create_invite(self, email, code_str="foobar"):
     code = InvitationCode(num_invites=100, code=code_str)
     code.save()
     invitation = Invitation(code=code, email=email, invited=now())
     invitation.save()
     return invitation
예제 #13
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