<s> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . template import RequestContext <EOL> from django . shortcuts import render_to_response <EOL> from django . utils . translation import ugettext <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from models import InvitationError , Invitation , InvitationStats <EOL> from forms import InvitationForm , RegistrationFormInvitation <EOL> from registration . signals import user_registered <EOL> def apply_extra_context ( context , extra_context = None ) : <EOL> if extra_context is None : <EOL> extra_context = { } <EOL> for key , value in extra_context . items ( ) : <EOL> context [ key ] = callable ( value ) and value ( ) or value <EOL> return context <EOL> @ login_required <EOL> def invite ( request , success_url = None , <EOL> form_class = InvitationForm , <EOL> template_name = '<STR_LIT>' , <EOL> extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> invitation = Invitation . objects . invite ( <EOL> request . user , form . cleaned_data [ "<STR_LIT:email>" ] ) <EOL> except InvitationError : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> invitation . send_email ( request = request ) <EOL> return HttpResponseRedirect ( success_url or reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> form = form_class ( ) <EOL> context = apply_extra_context ( RequestContext ( request ) , extra_context ) <EOL> return render_to_response ( template_name , <EOL> { '<STR_LIT>' : form } , <EOL> context_instance = context ) <EOL> def register ( request , <EOL> invitation_key , <EOL> wrong_key_template = '<STR_LIT>' , <EOL> redirect_to_if_authenticated = '<STR_LIT:/>' , <EOL> success_url = None , <EOL> form_class = RegistrationFormInvitation , <EOL> template_name = '<STR_LIT>' , <EOL> extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( redirect_to_if_authenticated ) <EOL> try : <EOL> invitation = Invitation . objects . find ( invitation_key ) <EOL> except Invitation . DoesNotExist : <EOL> context = apply_extra_context ( RequestContext ( request ) , extra_context ) <EOL> return render_to_response ( wrong_key_template , <EOL> { '<STR_LIT>' : invitation_key } , <EOL> context_instance = context ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( invitation . email , request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> new_user = form . save ( ) <EOL> invitation . mark_accepted ( new_user ) <EOL> user_registered . send ( sender = "<STR_LIT>" , <EOL> user = new_user , <EOL> request = request ) <EOL> return HttpResponseRedirect ( success_url or reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> form = form_class ( invitation . email ) <EOL> context = apply_extra_context ( RequestContext ( request ) , extra_context ) <EOL> return render_to_response ( template_name , <EOL> { '<STR_LIT>' : form } , <EOL> context_instance = context ) <EOL> @ staff_member_required <EOL> def reward ( request ) : <EOL> """<STR_LIT>""" <EOL> rewarded_users , invitations_given = InvitationStats . objects . reward ( ) <EOL> if rewarded_users : <EOL> message = ugettext ( u'<STR_LIT>' u'<STR_LIT>' ) % { <EOL> '<STR_LIT>' : rewarded_users , <EOL> '<STR_LIT>' : invitations_given } <EOL> else : <EOL> message = ugettext ( u'<STR_LIT>' u'<STR_LIT>' ) <EOL> request . user . message_set . create ( message = message ) <EOL> return HttpResponseRedirect ( request . META . get ( '<STR_LIT>' , '<STR_LIT:/>' ) ) </s>
