Beispiel #1
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!")
Beispiel #2
0
    def accept_invite(self, user):
        """
        When we have an invite with just a code and email,
        We can add the user to it.
        """
        assert user.email == self.email
        assert self.code is not None

        from hunger.utils import now
        self.user = user
        self.used = now()
        self.invited = now()
        self.save()
        self.code.num_invites -= 1
        self.code.save()
Beispiel #3
0
    def form_valid(self, form):
        valid_code = InvitationCode.objects.get(owner=self.request.user, num_invites__gt=0)
        form.instance.code = valid_code
        form.instance.invited = now()
        form.save()

        return super(InviteView, self).form_valid(form)
Beispiel #4
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
Beispiel #5
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()
Beispiel #6
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
Beispiel #7
0
    def form_valid(self, form):
        valid_code = InvitationCode.objects.get(owner=self.request.user,
                                                num_invites__gt=0)
        form.instance.code = valid_code
        form.instance.invited = now()
        form.save()

        return super(InviteView, self).form_valid(form)
Beispiel #8
0
    def form_valid(self, form):
        valid_code = InvitationCode.objects.get(owner=self.request.user,
                                                num_invites__gt=0)
        instance = form.save(commit=False)
        instance.code = valid_code
        instance.invited = now()
        instance.save(send_email=True, request=self.request)

        return super(InviteView, self).form_valid(form)
Beispiel #9
0
def invitation_code_used(sender, user, invitation_code, **kwargs):
    """Set the invitation code as used by user."""
    try:
        invitation_code = InvitationCode.objects.get(code=invitation_code)
    except InvitationCode.DoesNotExist:
        return

    invitation_code.user = user
    invitation_code.is_used = True
    invitation_code.used = now()
    invitation_code.save()
Beispiel #10
0
def invitation_code_used(sender, user, invitation_code, **kwargs):
    """Set the invitation code as used by user."""
    try:
        invitation_code = InvitationCode.objects.get(code=invitation_code)
    except InvitationCode.DoesNotExist:
        return

    invitation_code.user = user
    invitation_code.is_used = True
    invitation_code.used = now()
    invitation_code.save()
Beispiel #11
0
    def test_using_invite(self):
        cary = User.objects.create_user('cary', '*****@*****.**', 'secret')
        self.client.login(username='******', password='******')
        response = self.client.get(reverse('invited_only'))
        self.assertRedirects(response, reverse(self.redirect))

        response = self.client.get(reverse('invited_only'))
        self.assertRedirects(response, reverse(self.redirect))
        invitation = Invitation.objects.get(user=cary)
        invitation.invited = now()
        invitation.save()
        response = self.client.get(reverse('invited_only'))
        self.assertEqual(response.status_code, 200)
Beispiel #12
0
    def test_using_invite(self):
        cary = User.objects.create_user('cary', '*****@*****.**', 'secret')
        self.client.login(username='******', password='******')
        response = self.client.get(reverse('invited_only'))
        self.assertRedirects(response, reverse(self.redirect))

        response = self.client.get(reverse('invited_only'))
        self.assertRedirects(response, reverse(self.redirect))
        invitation = Invitation.objects.get(user=cary)
        invitation.invited = now()
        invitation.save()
        response = self.client.get(reverse('invited_only'))
        self.assertEqual(response.status_code, 200)
Beispiel #13
0
def invitation_code_sent(sender, email, invitation_code, **kwargs):
    """Send invitation code to user."""
    try:
        invitation_code = InvitationCode.objects.get(email=email)
    except InvitationCode.DoesNotExist:
        return

    invitation_code.is_invited = True
    invitation_code.invited = now()
    invitation_code.save()

    email_module_name = setting('BETA_EMAIL_MODULE', 'hunger.email')
    email_module = importlib.import_module(email_module_name)
    email_function_name = setting('BETA_EMAIL_INVITE_FUNCTION', 'beta_invite')
    email_function = getattr(email_module, email_function_name)
    email_function(email, invitation_code.code, **kwargs)
Beispiel #14
0
def invitation_code_sent(sender, email, invitation_code, **kwargs):
    """Send invitation code to user."""
    try:
        invitation_code = InvitationCode.objects.get(email=email)
    except InvitationCode.DoesNotExist:
        return

    invitation_code.is_invited = True
    invitation_code.invited = now()
    invitation_code.save()

    email_module_name = setting('BETA_EMAIL_MODULE', 'hunger.email')
    email_module = importlib.import_module(email_module_name)
    email_function_name = setting('BETA_EMAIL_INVITE_FUNCTION', 'beta_invite')
    email_function = getattr(email_module, email_function_name)
    email_function(email, invitation_code.code, **kwargs)
Beispiel #15
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()
Beispiel #16
0
def send_invite(modeladmin, request, queryset):
    for obj in queryset:
        if not obj.invited:
            obj.invited = now()
            obj.save(send_email=True, request=request)
Beispiel #17
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
Beispiel #18
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
Beispiel #19
0
 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
Beispiel #20
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)
Beispiel #21
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
Beispiel #22
0
def send_invite(modeladmin, request, queryset):
    for obj in queryset:
        if not obj.invited:
            obj.invited = now()
            obj.save(send_email=True, request=request)
Beispiel #23
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
Beispiel #24
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