<s> import datetime <EOL> import hashlib <EOL> from django . views . generic . detail import DetailView <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . core . mail import send_mail <EOL> from django . contrib import messages <EOL> from django . core . exceptions import PermissionDenied <EOL> from e_cidadania import settings <EOL> from core . spaces . models import Space , Intent <EOL> from helpers . cache import get_or_insert_object_in_cache <EOL> @ login_required <EOL> def add_intent ( request , space_url ) : <EOL> """<STR_LIT>""" <EOL> space = get_object_or_404 ( Space , url = space_url ) <EOL> admins = space . admins . all ( ) <EOL> mails = [ ] <EOL> for m in space . admins . all ( ) : <EOL> mails . append ( m . email ) <EOL> try : <EOL> intent = Intent . objects . get ( user = request . user , space = space ) <EOL> heading = _ ( "<STR_LIT>" ) <EOL> except Intent . DoesNotExist : <EOL> token = hashlib . md5 ( "<STR_LIT>" % ( request . user , space , <EOL> datetime . datetime . now ( ) ) ) . hexdigest ( ) <EOL> intent = Intent ( user = request . user , space = space , token = token ) <EOL> intent . save ( ) <EOL> subject = _ ( "<STR_LIT>" ) <EOL> body = _ ( "<STR_LIT>" ) . format ( <EOL> request . user . username , space . name , intent . get_approve_url ( ) ) <EOL> heading = _ ( "<STR_LIT>" ) <EOL> send_mail ( subject , body , settings . DEFAULT_FROM_EMAIL , mails ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : space . name , <EOL> '<STR_LIT>' : heading } , context_instance = RequestContext ( request ) ) <EOL> class ValidateIntent ( DetailView ) : <EOL> """<STR_LIT>""" <EOL> context_object_name = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> status = _ ( "<STR_LIT>" ) <EOL> def dispatch ( self , reques , * args , ** kwargs ) : <EOL> if request . user . is_authenticated : <EOL> return super ( ValidateIntent , self ) . dispatch ( request , * args , ** kwargs ) <EOL> else : <EOL> raise PermissionDenied <EOL> def get_object ( self ) : <EOL> space_url = self . kwargs [ '<STR_LIT>' ] <EOL> space_object = get_or_insert_object_in_cache ( Space , space_url , <EOL> url = space_url ) <EOL> if has_space_permission ( self . request . user , space_object , <EOL> allow = [ '<STR_LIT>' , '<STR_LIT>' ] ) or has_all_permissions ( self . request . user ) : <EOL> try : <EOL> intent = Intent . objects . get ( token = self . kwargs [ '<STR_LIT>' ] ) <EOL> intent . space . users . add ( intent . user ) <EOL> self . status = _ ( "<STR_LIT>" % space_object . name ) <EOL> messages . success ( self . request , _ ( "<STR_LIT>" ) ) <EOL> except Intent . DoesNotExist : <EOL> self . status = _ ( "<STR_LIT>" ) <EOL> return space_object <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ValidateIntent , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT:status>' ] = self . status <EOL> context [ '<STR_LIT>' ] = Intent . objects . get ( <EOL> token = self . kwargs [ '<STR_LIT>' ] ) . user <EOL> return context </s>
