<s> """<STR_LIT>""" <EOL> import mimetools <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . contrib . auth . decorators import user_passes_test <EOL> from django . contrib . auth . views import redirect_to_login <EOL> from django . contrib . sites . models import Site <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . mail import EmailMessage , SMTPConnection <EOL> from django . http import Http404 , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template import Context , loader , RequestContext <EOL> from django . utils . html import strip_tags <EOL> from brubeck . core . decorators import is_editor <EOL> from brubeck . core . emailing . forms import * <EOL> def render_email_and_send ( context = None , message_template = '<STR_LIT>' , subject = '<STR_LIT>' , recipients = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> t = loader . get_template ( message_template ) <EOL> c = Context ( context ) <EOL> message = t . render ( c ) <EOL> mimetools . _prefix = '<STR_LIT:x>' <EOL> if not sender : <EOL> sender = settings . SERVER_EMAIL <EOL> connection = SMTPConnection ( fail_silently = True ) <EOL> email_to_send = EmailMessage ( <EOL> subject = subject , <EOL> body = message , <EOL> from_email = sender , <EOL> to = recipients , <EOL> connection = connection <EOL> ) <EOL> email_to_send . send ( fail_silently = True ) <EOL> connection . close ( ) <EOL> def handle_form_and_email ( request , form = None , form_template = '<STR_LIT>' , message_template = '<STR_LIT>' , subject = '<STR_LIT>' , recipients = None , redirect_to = '<STR_LIT>' , sender = None , uses_captcha = True , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if uses_captcha : <EOL> remote_ip = request . META [ '<STR_LIT>' ] <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if uses_captcha : <EOL> form = form ( remote_ip , request . POST ) <EOL> else : <EOL> form = form ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> if form . cleaned_data [ '<STR_LIT>' ] : <EOL> recipients . append ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> if sender == '<STR_LIT>' : <EOL> sender = form . cleaned_data [ '<STR_LIT>' ] <EOL> except : <EOL> pass <EOL> render_email_and_send ( message_template = message_template , context = form . cleaned_data , subject = subject , recipients = recipients , sender = sender ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> if uses_captcha : <EOL> form = form ( remote_ip ) <EOL> else : <EOL> form = form ( ) <EOL> page = { <EOL> '<STR_LIT>' : form <EOL> } <EOL> return render_to_response ( form_template , page , context_instance = RequestContext ( request ) ) <EOL> from brubeck . articles . models import Article <EOL> def captcha_form_and_email ( request , form = None , form_template = '<STR_LIT>' , message_template = '<STR_LIT>' , subject = '<STR_LIT>' , recipients = None , redirect_to = '<STR_LIT>' , sender = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> remote_ip = request . META [ '<STR_LIT>' ] <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form ( remote_ip , request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> if form . cleaned_data [ '<STR_LIT>' ] : <EOL> recipients . append ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> if sender == '<STR_LIT>' : <EOL> sender = form . cleaned_data [ '<STR_LIT>' ] <EOL> except : <EOL> pass <EOL> render_email_and_send ( message_template = message_template , context = form . cleaned_data , subject = subject , recipients = recipients , sender = sender ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = form ( remote_ip ) <EOL> page = { <EOL> '<STR_LIT>' : form <EOL> } <EOL> return render_to_response ( form_template , page , context_instance = RequestContext ( request ) ) <EOL> @ user_passes_test ( lambda u : u . is_staff or u . is_superuser ) <EOL> def restricted_email ( request , form = None , form_template = '<STR_LIT>' , message_template = '<STR_LIT>' , subject = '<STR_LIT>' , recipients = None , redirect_to = '<STR_LIT>' , uses_captcha = False , editor_required = False ) : <EOL> """<STR_LIT>""" <EOL> if is_editor ( request . user ) or not editor_required : <EOL> return handle_form_and_email ( request , form = form , form_template = form_template , message_template = message_template , subject = subject , recipients = recipients , redirect_to = redirect_to , uses_captcha = uses_captcha ) <EOL> else : <EOL> redirect_to_login ( next = request . META [ '<STR_LIT>' ] ) <EOL> def email_content ( request , content_type = None , content_id = None ) : <EOL> """<STR_LIT>""" <EOL> site = Site . objects . get_current ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ContentEmailForm ( request . POST ) <EOL> else : <EOL> form = ContentEmailForm ( { <EOL> '<STR_LIT>' : content_type , <EOL> '<STR_LIT>' : content_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> if form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> object = Article . get_published . filter ( section__publication__site = site ) . get ( id = form . cleaned_data [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise Http404 <EOL> except ObjectDoesNotExist : <EOL> raise Http404 <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> form = ContentEmailForm ( { <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] <EOL> } ) <EOL> elif form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> form = ContentEmailForm ( { <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] <EOL> } ) <EOL> else : <EOL> email_context = { <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:object>' : object <EOL> } <EOL> render_email_and_send ( context = email_context , message_template = '<STR_LIT>' , subject = '<STR_LIT>' % strip_tags ( object . title ) , recipients = [ email_context [ '<STR_LIT>' ] ] , sender = email_context [ '<STR_LIT>' ] ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> page = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:object>' : object <EOL> } <EOL> return render_to_response ( '<STR_LIT>' , page , context_instance = RequestContext ( request ) ) </s>
