<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> from urllib import urlencode <EOL> from urlparse import parse_qs , urlparse <EOL> import re <EOL> import string <EOL> from bs4 import BeautifulSoup <EOL> from django . conf import settings <EOL> from django . utils . timezone import make_aware , get_current_timezone <EOL> from django . views . generic . list import MultipleObjectMixin <EOL> from open_connect . connect_core . utils . forms import ( <EOL> DateTimeRangeForm , PaginateByForm <EOL> ) <EOL> special_chars_regex = re . compile ( r'<STR_LIT>' ) <EOL> spaces_near_breaks_regex = re . compile ( r'<STR_LIT>' ) <EOL> single_break = re . compile ( r'<STR_LIT>' ) <EOL> three_or_more_breaks_regex = re . compile ( r'<STR_LIT>' ) <EOL> opening_closing_break = re . compile ( r'<STR_LIT>' ) <EOL> from open_connect . connect_core . utils . third_party . django_clean_html_backport import clean_html <EOL> class DateTimeRangeListMixin ( MultipleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> datetime_format = '<STR_LIT>' <EOL> date_range_field = '<STR_LIT>' <EOL> start_date_key = '<STR_LIT>' <EOL> end_date_key = '<STR_LIT>' <EOL> date_range_form_class = DateTimeRangeForm <EOL> date_range_form_context_name = '<STR_LIT>' <EOL> def get_date_range_form ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . date_range_form_class ( ** self . get_date_range_form_kwargs ( ) ) <EOL> def get_date_range_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : self . get_date_range_form_initial ( ) } <EOL> def get_date_range_form_initial ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . request . GET . get ( self . start_date_key , None ) , <EOL> '<STR_LIT>' : self . request . GET . get ( self . end_date_key , None ) <EOL> } <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . object_list = kwargs . pop ( '<STR_LIT>' ) <EOL> elif not hasattr ( self , '<STR_LIT>' ) : <EOL> self . object_list = None <EOL> context = super ( <EOL> DateTimeRangeListMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ self . date_range_form_context_name ] = self . get_date_range_form ( ) <EOL> return context <EOL> def get_queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> start_date = self . request . GET . get ( self . start_date_key , None ) <EOL> end_date = self . request . GET . get ( self . end_date_key , None ) <EOL> query_kwargs = { } <EOL> if start_date : <EOL> start_date = make_aware ( <EOL> datetime . strptime ( start_date , self . datetime_format ) , <EOL> get_current_timezone ( ) <EOL> ) <EOL> query_kwargs [ '<STR_LIT>' % self . date_range_field ] = start_date <EOL> if end_date : <EOL> end_date = make_aware ( <EOL> datetime . strptime ( end_date , self . datetime_format ) , <EOL> get_current_timezone ( ) <EOL> ) <EOL> query_kwargs [ '<STR_LIT>' % self . date_range_field ] = end_date <EOL> return super ( <EOL> DateTimeRangeListMixin , self ) . get_queryset ( ) . filter ( ** query_kwargs ) <EOL> class SortableListMixin ( MultipleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> valid_order_by = [ '<STR_LIT>' ] <EOL> default_order_by = '<STR_LIT>' <EOL> default_sort = '<STR_LIT>' <EOL> order_by_key = '<STR_LIT>' <EOL> sort_key = '<STR_LIT>' <EOL> sort_string_format = ( <EOL> '<STR_LIT>' ) <EOL> def get_context_sort_string_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def order_queryset ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> order_by = self . request . GET . get ( self . order_by_key ) <EOL> sort = self . request . GET . get ( self . sort_key , self . default_sort ) <EOL> if sort not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> sort = self . default_sort <EOL> if sort == '<STR_LIT>' : <EOL> sort = '<STR_LIT>' <EOL> else : <EOL> sort = '<STR_LIT:->' <EOL> if order_by not in self . valid_order_by : <EOL> order_by = self . default_order_by <EOL> queryset = queryset . order_by ( '<STR_LIT>' % ( sort , order_by ) ) <EOL> return queryset <EOL> def get_queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> queryset = super ( SortableListMixin , self ) . get_queryset ( ) <EOL> return self . order_queryset ( queryset ) <EOL> def get_query_string ( self ) : <EOL> """<STR_LIT>""" <EOL> query_string = parse_qs ( self . request . META [ '<STR_LIT>' ] ) <EOL> if self . order_by_key in query_string : <EOL> del query_string [ self . order_by_key ] <EOL> if self . sort_key in query_string : <EOL> del query_string [ self . sort_key ] <EOL> return urlencode ( query_string , doseq = True ) <EOL> def get_sort_strings ( self , query_string = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> existing_sort = self . request . GET . get ( self . sort_key , self . default_sort ) <EOL> existing_order_by = self . request . GET . get ( <EOL> self . order_by_key , self . default_order_by ) <EOL> sort_strings = { } <EOL> for order_by in self . valid_order_by : <EOL> if order_by == existing_order_by : <EOL> if existing_sort == '<STR_LIT>' : <EOL> sort = '<STR_LIT>' <EOL> else : <EOL> sort = '<STR_LIT>' <EOL> else : <EOL> sort = self . default_sort <EOL> sort_strings [ order_by ] = self . sort_string_format . format ( <EOL> order_by_key = self . order_by_key , <EOL> order_by = order_by , <EOL> sort_key = self . sort_key , <EOL> sort = sort , <EOL> querystring = query_string <EOL> ) <EOL> return sort_strings <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . object_list = kwargs . pop ( '<STR_LIT>' ) <EOL> context = super ( SortableListMixin , self ) . get_context_data ( ** kwargs ) <EOL> query_string = self . get_query_string ( ) <EOL> context [ '<STR_LIT>' ] = query_string <EOL> context [ '<STR_LIT>' ] = self . request . META [ '<STR_LIT>' ] <EOL> context [ self . get_context_sort_string_name ( ) ] = self . get_sort_strings ( <EOL> query_string ) <EOL> return context <EOL> class PaginateByMixin ( MultipleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> paginate_key = '<STR_LIT>' <EOL> paginate_form_class = PaginateByForm <EOL> paginate_form_context_name = '<STR_LIT>' <EOL> def get_paginate_form ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . paginate_form_class ( ** self . get_paginate_form_kwargs ( ) ) <EOL> def get_paginate_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : self . get_paginate_form_initial ( ) } <EOL> def get_paginate_form_initial ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . request . GET . get ( self . paginate_key , None ) , <EOL> } <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . object_list = kwargs . pop ( '<STR_LIT>' ) <EOL> elif not hasattr ( self , '<STR_LIT>' ) : <EOL> self . object_list = None <EOL> context = super ( PaginateByMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ self . paginate_form_context_name ] = self . get_paginate_form ( ) <EOL> return context <EOL> def get_paginate_by ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> paginate_by = self . request . GET . get ( self . paginate_key , None ) <EOL> if paginate_by and paginate_by . isdigit ( ) : <EOL> return int ( paginate_by ) <EOL> return super ( PaginateByMixin , self ) . get_paginate_by ( queryset ) <EOL> def handle_breaks ( html ) : <EOL> """<STR_LIT>""" <EOL> html = special_chars_regex . sub ( '<STR_LIT:U+0020>' , html ) <EOL> html = spaces_near_breaks_regex . sub ( '<STR_LIT>' , html ) <EOL> html = three_or_more_breaks_regex . sub ( '<STR_LIT>' , html ) <EOL> html = html . strip ( ) <EOL> html = opening_closing_break . sub ( '<STR_LIT>' , html ) <EOL> html = single_break . sub ( '<STR_LIT>' , html ) <EOL> return html <EOL> class SanitizeHTMLMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> VALID_TAGS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> VALID_ATTRS = [ '<STR_LIT>' , '<STR_LIT:src>' , '<STR_LIT>' ] <EOL> VALID_SCHEMES = [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ADD_MAX_WIDTH = True <EOL> def _cleanse_tags ( self , message ) : <EOL> """<STR_LIT>""" <EOL> soup = BeautifulSoup ( message , "<STR_LIT>" ) <EOL> for tag in soup . findAll ( ) : <EOL> if tag . name not in self . VALID_TAGS : <EOL> tag . hidden = True <EOL> for attr , value in dict ( tag . attrs ) . iteritems ( ) : <EOL> if attr not in self . VALID_ATTRS : <EOL> del tag . attrs [ attr ] <EOL> if attr == '<STR_LIT:src>' : <EOL> parsed_src = urlparse ( value ) <EOL> valid_netlocs = settings . ALLOWED_HOSTS <EOL> valid_netlocs . append ( '<STR_LIT>' ) <EOL> if parsed_src . netloc not in valid_netlocs : <EOL> tag . hidden = True <EOL> if attr == '<STR_LIT>' : <EOL> parsed_src = urlparse ( value ) <EOL> if parsed_src . scheme not in self . VALID_SCHEMES : <EOL> tag . hidden = True <EOL> if self . ADD_MAX_WIDTH and tag . name == '<STR_LIT>' : <EOL> tag . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return unicode ( soup ) . encode ( "<STR_LIT:utf-8>" , errors = "<STR_LIT:ignore>" ) <EOL> def sanitize_html ( self , message ) : <EOL> """<STR_LIT>""" <EOL> message = clean_html ( message ) <EOL> if '<STR_LIT>' not in message : <EOL> message = string . replace ( message , '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> clean_code = self . _cleanse_tags ( message ) <EOL> final = handle_breaks ( clean_code ) <EOL> return final </s>
