Created
June 10, 2010 22:30
-
-
Save pagenoare/433731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from django import forms | |
class Invite(forms.Form): | |
email = forms.EmailField(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from django.db import models | |
from django.contrib.auth.models import User | |
class Key(models.Model): | |
user = models.ForeignKey(User) | |
key = models.CharField(max_length=125) | |
email = models.EmailField() | |
expired = models.BooleanField(default=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from django.shortcuts import render_to_response | |
from django.template import RequestContext | |
def render(template): | |
def render_with_decorator(view_func): | |
def wrapper(*args, **kwargs): | |
request = args[0] | |
context = view_func(*args, **kwargs) | |
if isinstance(context, dict): | |
return render_to_response( | |
template, | |
context, | |
context_instance = RequestContext(request), | |
) | |
else: | |
return context | |
return wrapper | |
return render_with_decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from django.core.mail import EmailMessage | |
from uuid import uuid4 | |
from utils import render | |
import forms | |
from models import Key | |
def gen_key(user): | |
key = uuid4().hex | |
while Key.objects.filter(user=user, key=key): | |
key = uuid4().hex | |
return key | |
@login_required | |
@render('users/invite.html') | |
def invite(request): | |
if request.method == 'POST': | |
form = forms.Invite(request.POST) | |
invites_count = request.user.get_profile().invites | |
if invites_count == 0: | |
return redirect('/') | |
if form.is_valid(): | |
email = form.cleaned_data['email'] | |
if User.objects.filter(email=email) or Key.objects.filter(email=email): | |
return redirect('/') | |
key = Key(user=request.user, key=gen_key(request.user), email=email) | |
current_site = Site.objects.get_current() | |
content = render_to_string('users/invite_msg.html', dict(user=request.user, key=key, site=current_site)) | |
key.save() | |
msg = EmailMessage('Zaproszenie do serialowo.tv', content, '[email protected]', [email]) | |
msg.content_subtype = "html" | |
msg.send() | |
request.user.get_profile().invites = invites_count - 1 | |
request.user.get_profile().save() | |
return redirect('/') | |
else: | |
form = forms.Invite() | |
return dict(form=form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment