#NEWLINE# @ route ( 'mailing-list' , method = ( 'post' , 'options' ) ) #NEWLINE# def mailing_list( self , request ) : #NEWLINE# #INDENT# 'Add a given email to a mailing list\n        ' #NEWLINE# form_class = get_form_class ( 'mailing-list' ) #NEWLINE# if ( not form_class ) : #NEWLINE# #INDENT# raise Http404 #NEWLINE# #UNINDENT# if ( request . method == 'OPTIONS' ) : #NEWLINE# #INDENT# request . app . fire ( 'on_preflight' , request , methods = [ 'POST' ] ) #NEWLINE# return request . response #NEWLINE# #UNINDENT# user = request . cache . user #NEWLINE# data = request . body_data ( ) #NEWLINE# if user . is_authenticated ( ) : #NEWLINE# #INDENT# raise MethodNotAllowed #NEWLINE# #UNINDENT# form = form_class ( request , data = data ) #NEWLINE# if form . is_valid ( ) : #NEWLINE# #INDENT# email = form . cleaned_data [ 'email' ] #NEWLINE# odm = request . app . odm ( ) #NEWLINE# topic = request . config [ 'GENERAL_MAILING_LIST_TOPIC' ] #NEWLINE# with odm . begin ( ) as session : #NEWLINE# #INDENT# q = session . query ( odm . mailinglist ) . filter_by ( email = email , topic = topic ) #NEWLINE# if ( not q . count ( ) ) : #NEWLINE# #INDENT# entry = odm . mailinglist ( email = email , topic = topic ) #NEWLINE# session . add ( entry ) #NEWLINE# request . response . status_code = 201 #NEWLINE# result = json_message ( request , ( 'Email %s added to mailing list' % data ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result = json_message ( request , ( 'Email %s already in mailing list' % email ) , level = 'warning' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# result = form . tojson ( ) #NEWLINE# #UNINDENT# return Json ( result ) . http_response ( request )