2012-09-24 14:25:41 +03:00
# coding: utf-8
2009-01-17 14:42:12 +02:00
import re
2012-09-24 14:25:41 +03:00
from HTMLParser import HTMLParser , HTMLParseError
2012-10-22 11:18:19 -04:00
import postmarkup
2010-11-28 17:57:54 +02:00
try :
import markdown
except ImportError :
pass
2009-01-05 14:30:08 +02:00
2012-09-24 14:25:41 +03:00
from django . conf import settings
2009-01-05 14:30:08 +02:00
from django . shortcuts import render_to_response
from django . template import RequestContext
2009-08-24 23:37:53 +03:00
from django . http import HttpResponse , Http404
2009-01-05 14:30:08 +02:00
from django . utils . functional import Promise
2009-03-03 18:30:41 +02:00
from django . utils . translation import force_unicode , check_for_language
2009-01-05 14:30:08 +02:00
from django . utils . simplejson import JSONEncoder
from django . template . defaultfilters import urlize as django_urlize
2009-08-24 23:37:53 +03:00
from django . core . paginator import Paginator , EmptyPage , InvalidPage
2010-02-17 14:03:30 +02:00
from django . contrib . sites . models import Site
2009-01-05 14:30:08 +02:00
2009-12-23 17:06:48 +02:00
from djangobb_forum import settings as forum_settings
2009-03-03 18:30:41 +02:00
2011-08-11 17:15:08 +03:00
2009-01-21 18:28:36 +02:00
#compile smiles regexp
_SMILES = [ ( re . compile ( smile_re ) , path ) for smile_re , path in forum_settings . SMILES ]
2010-01-04 14:17:36 +02:00
2009-01-19 19:50:01 +02:00
def absolute_url ( path ) :
2010-02-17 14:03:30 +02:00
return ' http:// %s %s ' % ( Site . objects . get_current ( ) . domain , path )
2009-01-05 14:30:08 +02:00
2010-01-04 14:17:36 +02:00
2009-08-20 12:46:08 +03:00
def paged ( paged_list_name , per_page ) :
2009-01-05 14:30:08 +02:00
"""
Parse page from GET data and pass it to view . Split the
query set returned from view .
"""
2009-08-20 12:46:08 +03:00
2009-01-05 14:30:08 +02:00
def decorator ( func ) :
def wrapper ( request , * args , * * kwargs ) :
result = func ( request , * args , * * kwargs )
2009-08-20 12:46:08 +03:00
if not isinstance ( result , dict ) or ' paged_qs ' not in result :
2009-01-05 14:30:08 +02:00
return result
try :
page = int ( request . GET . get ( ' page ' , 1 ) )
except ValueError :
page = 1
real_per_page = per_page
#if per_page_var:
#try:
#value = int(request.GET[per_page_var])
#except (ValueError, KeyError):
#pass
#else:
#if value > 0:
#real_per_page = value
from django . core . paginator import Paginator
paginator = Paginator ( result [ ' paged_qs ' ] , real_per_page )
2009-08-27 17:41:42 +03:00
try :
2011-05-03 12:45:46 +03:00
page_obj = paginator . page ( page )
2009-08-27 17:41:42 +03:00
except ( InvalidPage , EmptyPage ) :
raise Http404
2011-05-03 12:45:46 +03:00
result [ paged_list_name ] = page_obj . object_list
result [ ' is_paginated ' ] = page_obj . has_other_pages ( ) ,
result [ ' page_obj ' ] = page_obj ,
2009-01-05 14:30:08 +02:00
result [ ' page ' ] = page
2011-05-03 12:45:46 +03:00
result [ ' page_range ' ] = paginator . page_range ,
2009-01-05 14:30:08 +02:00
result [ ' pages ' ] = paginator . num_pages
2011-05-03 12:45:46 +03:00
result [ ' results_per_page ' ] = paginator . per_page ,
2009-01-05 14:30:08 +02:00
result [ ' request ' ] = request
return result
return wrapper
return decorator
class LazyJSONEncoder ( JSONEncoder ) :
"""
This fing need to save django from crashing .
"""
def default ( self , o ) :
if isinstance ( o , Promise ) :
return force_unicode ( o )
else :
return super ( LazyJSONEncoder , self ) . default ( o )
class JsonResponse ( HttpResponse ) :
"""
HttpResponse subclass that serialize data into JSON format .
"""
def __init__ ( self , data , mimetype = ' application/json ' ) :
json_data = LazyJSONEncoder ( ) . encode ( data )
super ( JsonResponse , self ) . __init__ (
content = json_data , mimetype = mimetype )
2010-01-05 15:56:19 +02:00
2009-01-05 14:30:08 +02:00
def build_form ( Form , _request , GET = False , * args , * * kwargs ) :
"""
2009-07-02 16:01:22 +03:00
Shorcut for building the form instance of given form class
2009-01-05 14:30:08 +02:00
"""
if not GET and ' POST ' == _request . method :
form = Form ( _request . POST , _request . FILES , * args , * * kwargs )
elif GET and ' GET ' == _request . method :
form = Form ( _request . GET , _request . FILES , * args , * * kwargs )
else :
form = Form ( * args , * * kwargs )
return form
2010-01-05 15:56:19 +02:00
2009-01-21 18:28:36 +02:00
class ExcludeTagsHTMLParser ( HTMLParser ) :
"""
Class for html parsing with excluding specified tags .
"""
2011-04-04 12:56:16 +03:00
def __init__ ( self , func , tags = ( ' a ' , ' pre ' , ' span ' ) ) :
2009-01-09 16:11:30 +02:00
HTMLParser . __init__ ( self )
2009-01-21 18:28:36 +02:00
self . func = func
self . is_ignored = False
self . tags = tags
self . html = [ ]
2009-01-09 16:11:30 +02:00
def handle_starttag ( self , tag , attrs ) :
2009-01-21 18:28:36 +02:00
self . html . append ( ' < %s %s > ' % ( tag , self . __html_attrs ( attrs ) ) )
if tag in self . tags :
self . is_ignored = True
2009-01-09 16:11:30 +02:00
def handle_data ( self , data ) :
2009-01-21 18:28:36 +02:00
if not self . is_ignored :
data = self . func ( data )
self . html . append ( data )
2009-01-09 16:11:30 +02:00
def handle_startendtag ( self , tag , attrs ) :
2012-08-08 11:00:28 +03:00
self . html . append ( ' < %s %s /> ' % ( tag , self . __html_attrs ( attrs ) ) )
2009-01-21 18:28:36 +02:00
2009-01-09 16:11:30 +02:00
def handle_endtag ( self , tag ) :
2009-01-21 18:28:36 +02:00
self . is_ignored = False
self . html . append ( ' </ %s > ' % ( tag ) )
2009-01-20 17:42:02 +02:00
def handle_entityref ( self , name ) :
2009-01-21 18:28:36 +02:00
self . html . append ( ' & %s ; ' % name )
2009-01-20 17:42:02 +02:00
def handle_charref ( self , name ) :
2010-01-04 14:17:36 +02:00
self . html . append ( ' &# %s ; ' % name )
2009-01-20 17:42:02 +02:00
2010-01-05 15:56:19 +02:00
def unescape ( self , s ) :
#we don't need unescape data (without this possible XSS-attack)
return s
2009-01-09 16:11:30 +02:00
def __html_attrs ( self , attrs ) :
_attrs = ' '
if attrs :
2012-08-08 11:00:28 +03:00
_attrs = ' %s ' % ( ' ' . join ( [ ( ' %s = " %s " ' % ( k , v ) ) for k , v in attrs ] ) )
2009-01-09 16:11:30 +02:00
return _attrs
2009-01-21 18:28:36 +02:00
2009-01-09 16:11:30 +02:00
def feed ( self , data ) :
HTMLParser . feed ( self , data )
2009-01-21 18:28:36 +02:00
self . html = ' ' . join ( self . html )
2010-01-05 15:56:19 +02:00
2012-09-24 14:25:41 +03:00
def urlize ( html ) :
2009-01-21 18:28:36 +02:00
"""
Urlize plain text links in the HTML contents .
Do not urlize content of A and CODE tags .
"""
2012-09-24 14:25:41 +03:00
try :
parser = ExcludeTagsHTMLParser ( django_urlize )
parser . feed ( html )
urlized_html = parser . html
parser . close ( )
except HTMLParseError :
# HTMLParser from Python <2.7.3 is not robust
# see: http://support.djangobb.org/topic/349/
if settings . DEBUG :
raise
return html
2009-01-09 16:11:30 +02:00
return urlized_html
2009-01-05 14:30:08 +02:00
2009-01-21 18:28:36 +02:00
def _smile_replacer ( data ) :
for smile , path in _SMILES :
data = smile . sub ( path , data )
2009-01-17 14:42:12 +02:00
return data
2012-09-24 14:25:41 +03:00
def smiles ( html ) :
2009-01-21 18:28:36 +02:00
"""
Replace text smiles .
"""
2012-09-24 14:25:41 +03:00
try :
parser = ExcludeTagsHTMLParser ( _smile_replacer )
parser . feed ( html )
smiled_html = parser . html
parser . close ( )
except HTMLParseError :
# HTMLParser from Python <2.7.3 is not robust
# see: http://support.djangobb.org/topic/349/
if settings . DEBUG :
raise
return html
2009-01-21 18:28:36 +02:00
return smiled_html
2009-02-09 20:31:19 +02:00
def paginate ( items , request , per_page , total_count = None ) :
try :
page_number = int ( request . GET . get ( ' page ' , 1 ) )
except ValueError :
page_number = 1
2009-10-19 14:02:27 +03:00
2009-02-09 20:31:19 +02:00
paginator = Paginator ( items , per_page )
pages = paginator . num_pages
2009-08-24 23:37:53 +03:00
try :
paged_list_name = paginator . page ( page_number ) . object_list
except ( InvalidPage , EmptyPage ) :
2010-01-04 14:17:36 +02:00
raise Http404
2012-08-08 11:00:28 +03:00
return pages , paginator , paged_list_name
2009-02-09 20:31:19 +02:00
def set_language ( request , language ) :
"""
Change the language of session of authenticated user .
"""
2011-05-17 14:12:45 +03:00
if check_for_language ( language ) :
request . session [ ' django_language ' ] = language
2010-11-28 17:57:54 +02:00
def convert_text_to_html ( text , markup ) :
if markup == ' bbcode ' :
2012-10-22 11:18:19 -04:00
renderbb = customize_postmarkup ( )
text = renderbb ( text )
2011-02-28 12:17:16 +02:00
elif markup == ' markdown ' :
2010-11-28 17:57:54 +02:00
text = markdown . markdown ( text , safe_mode = ' escape ' )
else :
raise Exception ( ' Invalid markup property: %s ' % markup )
2011-02-28 12:17:16 +02:00
return urlize ( text )
2012-06-05 11:54:31 +03:00
2012-10-22 11:32:21 -04:00
# This allows us to control the bb tags
2012-10-22 11:18:19 -04:00
def customize_postmarkup ( ) :
custom_postmarkup = postmarkup . PostMarkup ( )
add_tag = custom_postmarkup . tag_factory . add_tag
custom_postmarkup . tag_factory . set_default_tag ( postmarkup . DefaultTag )
add_tag ( postmarkup . SimpleTag , ' b ' , ' strong ' )
add_tag ( postmarkup . SimpleTag , ' i ' , ' em ' )
add_tag ( postmarkup . SimpleTag , ' u ' , ' u ' )
add_tag ( postmarkup . SimpleTag , ' s ' , ' strike ' )
add_tag ( postmarkup . LinkTag , ' link ' , None )
add_tag ( postmarkup . LinkTag , ' url ' , None )
add_tag ( postmarkup . QuoteTag , ' quote ' )
add_tag ( postmarkup . SearchTag , u ' wiki ' ,
u " http://en.wikipedia.org/wiki/Special:Search?search= %s " , u ' wikipedia.com ' , None )
add_tag ( postmarkup . SearchTag , u ' google ' ,
u " http://www.google.com/search?hl=en&q= %s &btnG=Google+Search " , u ' google.com ' , None )
add_tag ( postmarkup . SearchTag , u ' dictionary ' ,
u " http://dictionary.reference.com/browse/ %s " , u ' dictionary.com ' , None )
add_tag ( postmarkup . SearchTag , u ' dict ' ,
u " http://dictionary.reference.com/browse/ %s " , u ' dictionary.com ' , None )
add_tag ( postmarkup . ImgTag , u ' img ' )
add_tag ( postmarkup . ListTag , u ' list ' )
add_tag ( postmarkup . ListItemTag , u ' * ' )
2012-10-22 11:32:21 -04:00
# removed 'size' and replaced it with 'big' and 'small'
2012-10-22 11:18:19 -04:00
add_tag ( postmarkup . SimpleTag , ' big ' , ' span style= " font-size:32px " ' )
add_tag ( postmarkup . SimpleTag , ' small ' , ' span style= " font-size:10px " ' )
add_tag ( postmarkup . ColorTag , u " color " )
add_tag ( postmarkup . CenterTag , u " center " )
add_tag ( postmarkup . PygmentsCodeTag , u ' code ' , None )
add_tag ( postmarkup . SimpleTag , u " p " , ' p ' )
return custom_postmarkup