<s> """<STR_LIT>""" <EOL> from pages . widgets_registry import get_widget <EOL> from pages import settings <EOL> from pages . models import Content <EOL> from pages . widgets import ImageInput , FileInput <EOL> from django import forms <EOL> from django . core . mail import send_mail <EOL> from django import template <EOL> from django . template import TemplateSyntaxError <EOL> from django . core . files . storage import default_storage <EOL> from django . forms import Textarea , ImageField , CharField , FileField <EOL> from django . forms import TextInput <EOL> from django . conf import settings as global_settings <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import unescape_string_literal <EOL> from django . template . loader import render_to_string <EOL> from django . template import RequestContext <EOL> from django . core . files . uploadedfile import UploadedFile <EOL> import logging <EOL> import os <EOL> import time <EOL> import six <EOL> logging . basicConfig ( ) <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> PLACEHOLDER_ERROR = _ ( "<STR_LIT>" ) <EOL> def parse_placeholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> count = len ( bits ) <EOL> error_string = '<STR_LIT>' % bits [ <NUM_LIT:0> ] <EOL> if count <= <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( error_string ) <EOL> try : <EOL> name = unescape_string_literal ( bits [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> name = bits [ <NUM_LIT:1> ] <EOL> remaining = bits [ <NUM_LIT:2> : ] <EOL> params = { } <EOL> simple_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> param_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> all_options = simple_options + param_options <EOL> while remaining : <EOL> bit = remaining [ <NUM_LIT:0> ] <EOL> if bit not in all_options : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % bit ) <EOL> if bit in param_options : <EOL> if len ( remaining ) < <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % bit ) <EOL> if bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = remaining [ <NUM_LIT:1> ] <EOL> if bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = remaining [ <NUM_LIT:1> ] <EOL> if bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = remaining [ <NUM_LIT:1> ] <EOL> remaining = remaining [ <NUM_LIT:2> : ] <EOL> elif bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = True <EOL> remaining = remaining [ <NUM_LIT:1> : ] <EOL> elif bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = True <EOL> remaining = remaining [ <NUM_LIT:1> : ] <EOL> elif bit == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = True <EOL> remaining = remaining [ <NUM_LIT:1> : ] <EOL> return name , params <EOL> class PlaceholderNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> field = CharField <EOL> widget = TextInput <EOL> def __init__ ( self , name , page = None , widget = None , parsed = False , <EOL> as_varname = None , inherited = False , untranslated = False , has_revision = True ) : <EOL> """<STR_LIT>""" <EOL> self . page = page or '<STR_LIT>' <EOL> self . name = name <EOL> self . ctype = name . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> if widget : <EOL> self . widget = widget <EOL> self . parsed = parsed <EOL> self . inherited = inherited <EOL> self . untranslated = untranslated <EOL> self . as_varname = as_varname <EOL> self . found_in_block = None <EOL> def get_widget ( self , page , language , fallback = Textarea ) : <EOL> """<STR_LIT>""" <EOL> is_str = isinstance ( self . widget , six . string_types ) <EOL> if is_str : <EOL> widget = get_widget ( self . widget ) <EOL> else : <EOL> widget = self . widget <EOL> try : <EOL> return widget ( page = page , language = language ) <EOL> except : <EOL> pass <EOL> return widget ( ) <EOL> def get_extra_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for key in list ( data . keys ( ) ) : <EOL> if key . startswith ( self . ctype + '<STR_LIT:->' ) : <EOL> new_key = key . replace ( self . ctype + '<STR_LIT:->' , '<STR_LIT>' ) <EOL> result [ new_key ] = data [ key ] <EOL> return result <EOL> def get_field ( self , page , language , initial = None ) : <EOL> """<STR_LIT>""" <EOL> if self . parsed : <EOL> help_text = _ ( '<STR_LIT>' ) <EOL> else : <EOL> help_text = '<STR_LIT>' <EOL> widget = self . get_widget ( page , language ) <EOL> return self . field ( widget = widget , initial = initial , <EOL> help_text = help_text , required = False ) <EOL> def save ( self , page , language , data , change , extra_data = None ) : <EOL> """<STR_LIT>""" <EOL> if self . untranslated : <EOL> language = settings . PAGE_DEFAULT_LANGUAGE <EOL> if change : <EOL> if ( settings . PAGE_CONTENT_REVISION and self . name <EOL> not in settings . PAGE_CONTENT_REVISION_EXCLUDE_LIST ) : <EOL> Content . objects . create_content_if_changed ( <EOL> page , <EOL> language , <EOL> self . ctype , <EOL> data <EOL> ) <EOL> else : <EOL> Content . objects . set_or_create_content ( <EOL> page , <EOL> language , <EOL> self . ctype , <EOL> data <EOL> ) <EOL> else : <EOL> Content . objects . set_or_create_content ( <EOL> page , <EOL> language , <EOL> self . ctype , <EOL> data <EOL> ) <EOL> def get_content ( self , page_obj , lang , lang_fallback = True ) : <EOL> if self . untranslated : <EOL> lang = settings . PAGE_DEFAULT_LANGUAGE <EOL> lang_fallback = False <EOL> content = Content . objects . get_content ( page_obj , lang , self . ctype , <EOL> lang_fallback ) <EOL> if self . inherited and not content : <EOL> for ancestor in page_obj . get_ancestors ( ) : <EOL> content = Content . objects . get_content ( ancestor , lang , <EOL> self . ctype , lang_fallback ) <EOL> if content : <EOL> break <EOL> return content <EOL> def get_content_from_context ( self , context ) : <EOL> if not self . page in context : <EOL> return '<STR_LIT>' <EOL> if not context [ self . page ] : <EOL> return '<STR_LIT>' <EOL> if self . untranslated : <EOL> lang_fallback = False <EOL> lang = settings . PAGE_DEFAULT_LANGUAGE <EOL> else : <EOL> lang_fallback = True <EOL> lang = context . get ( '<STR_LIT>' , settings . PAGE_DEFAULT_LANGUAGE ) <EOL> return self . get_content ( context [ self . page ] , lang , lang_fallback ) <EOL> def get_render_content ( self , context ) : <EOL> return mark_safe ( self . get_content_from_context ( context ) ) <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> content = self . get_render_content ( context ) <EOL> if not content : <EOL> return '<STR_LIT>' <EOL> if self . parsed : <EOL> try : <EOL> t = template . Template ( content , name = self . name ) <EOL> content = mark_safe ( t . render ( context ) ) <EOL> except TemplateSyntaxError as error : <EOL> if global_settings . DEBUG : <EOL> content = PLACEHOLDER_ERROR % { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:error>' : error , <EOL> } <EOL> else : <EOL> content = '<STR_LIT>' <EOL> if self . as_varname is None : <EOL> return content <EOL> context [ self . as_varname ] = content <EOL> return '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def get_filename ( page , placeholder , data ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . join ( <EOL> settings . PAGE_UPLOAD_ROOT , <EOL> '<STR_LIT>' + str ( page . id ) , <EOL> placeholder . ctype + '<STR_LIT:->' + str ( time . time ( ) ) + '<STR_LIT:->' + data . name <EOL> ) <EOL> return filename <EOL> class FilePlaceholderNode ( PlaceholderNode ) : <EOL> """<STR_LIT>""" <EOL> def get_field ( self , page , language , initial = None ) : <EOL> help_text = "<STR_LIT>" <EOL> widget = FileInput ( page , language ) <EOL> return FileField ( <EOL> widget = widget , <EOL> initial = initial , <EOL> help_text = help_text , <EOL> required = False <EOL> ) <EOL> def save ( self , page , language , data , change , extra_data = None ) : <EOL> if '<STR_LIT>' in extra_data : <EOL> return super ( FilePlaceholderNode , self ) . save ( <EOL> page , <EOL> language , <EOL> "<STR_LIT>" , <EOL> change <EOL> ) <EOL> filename = '<STR_LIT>' <EOL> if change and data : <EOL> if not isinstance ( data , UploadedFile ) : <EOL> return <EOL> filename = get_filename ( page , self , data ) <EOL> filename = default_storage . save ( filename , data ) <EOL> return super ( FilePlaceholderNode , self ) . save ( <EOL> page , <EOL> language , <EOL> filename , <EOL> change <EOL> ) <EOL> class ImagePlaceholderNode ( FilePlaceholderNode ) : <EOL> """<STR_LIT>""" <EOL> def get_field ( self , page , language , initial = None ) : <EOL> help_text = "<STR_LIT>" <EOL> widget = ImageInput ( page , language ) <EOL> return ImageField ( <EOL> widget = widget , <EOL> initial = initial , <EOL> help_text = help_text , <EOL> required = False <EOL> ) <EOL> class ContactForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> email = forms . EmailField ( label = _ ( '<STR_LIT>' ) ) <EOL> subject = forms . CharField ( label = _ ( '<STR_LIT>' ) , <EOL> max_length = <NUM_LIT> ) <EOL> message = forms . CharField ( widget = forms . Textarea ( ) , <EOL> label = _ ( '<STR_LIT>' ) ) <EOL> class ContactPlaceholderNode ( PlaceholderNode ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , context ) : <EOL> content = self . get_content_from_context ( context ) <EOL> request = context . get ( '<STR_LIT>' , None ) <EOL> if not request : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ContactForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> recipients = [ adm [ <NUM_LIT:1> ] for adm in global_settings . ADMINS ] <EOL> try : <EOL> send_mail ( data [ '<STR_LIT>' ] , data [ '<STR_LIT:message>' ] , <EOL> data [ '<STR_LIT:email>' ] , recipients , fail_silently = False ) <EOL> return _ ( "<STR_LIT>" ) <EOL> except : <EOL> return _ ( "<STR_LIT>" ) <EOL> else : <EOL> form = ContactForm ( ) <EOL> renderer = render_to_string ( '<STR_LIT>' , { '<STR_LIT>' : form } , <EOL> RequestContext ( request ) ) <EOL> return mark_safe ( renderer ) <EOL> class JsonPlaceholderNode ( PlaceholderNode ) : <EOL> """<STR_LIT>""" <EOL> def get_render_content ( self , context ) : <EOL> import json <EOL> content = self . get_content_from_context ( context ) <EOL> try : <EOL> return json . loads ( str ( content ) ) <EOL> except : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return content <EOL> class MarkdownPlaceholderNode ( PlaceholderNode ) : <EOL> """<STR_LIT>""" <EOL> widget = Textarea <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> import markdown <EOL> content = self . get_content_from_context ( context ) <EOL> return markdown . markdown ( content ) </s>
