def navigation(request): if hasattr(request, 'attributes') and 'group' in request.attributes: group = request.attributes['group'] else: group = get_current_group() sphdata = get_current_sphdata() sph_settings = getattr( settings, 'SPH_SETTINGS', None ) sphdata['installed_apps'] = settings.INSTALLED_APPS sphdata['current_url'] = request.path querystring = request.META.get('QUERY_STRING', None) if querystring: sphdata['current_url'] += '?'+querystring # urlPrefix is deprecated, don't use it. urlPrefix = '' if hasattr(request, 'attributes'): urlPrefix = request.attributes.get('urlPrefix', '') if group: return { 'navigation_left': Navigation.objects.filter( group = group, navigationType = 0 ), 'navigation_top': Navigation.objects.filter( group = group, navigationType = 1 ), 'urlPrefix': urlPrefix, 'group': group, 'sph': sphdata, 'sph_settings': SphSettings(), 'sph_perms': PermissionFlagLookup(), } return { 'sph': sphdata, 'sph_settings': SphSettings(), 'sph_perms': PermissionFlagLookup(), }
def sph_markdown(value, arg='', oldmd=None, extra_macros={}): try: from sphene.contrib.libs.markdown import markdown except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed." return value else: safe_mode = arg == 'safe' macros = { 'helloWorld': SimpleHelloWorldMacro(), 'news': NewsMacro(), 'newsrss': NewsRSSLinkMacro(), 'include': IncludeMacro(), 'includetemplate': IncludeTemplateMacro(), } macros.update(extra_macros), md = markdown.Markdown(value, extensions = [ 'footnotes', 'wikilink', 'macros', 'toc' ], extension_configs = { 'wikilink': [ ], 'macros': [ ( 'macros', macros ) ], 'toc': { 'include_header_one_in_toc': True, }, }, safe_mode = safe_mode, ) md.number_headings = get_sph_setting('markdown_number_headings', True) md.top_heading_level = get_sph_setting('markdown_top_heading_level', 1) if oldmd and hasattr(oldmd,'header_numbers'): md.header_numbers = oldmd.header_numbers ret = md.toString() if hasattr(md, 'tocDiv'): sphdata = get_current_sphdata() sphdata['toc'] = mark_safe( md.tocDiv.toxml() ) return ret
def add_rss_feed(url, label): sphdata = get_current_sphdata() if not 'rss' in sphdata: sphdata['rss'] = [] sphdata['rss'].append( { 'url': url, 'label': label, } )
def showThread(request, thread_id, group=None, slug=None): thread = get_object_or_404(Post.objects, pk=thread_id) if not thread.category.has_view_permission(request.user): raise PermissionDenied() thread.viewed(request.session, request.user) sphdata = get_current_sphdata() if sphdata != None: sphdata['subtitle'] = thread.subject category_type = thread.category.get_category_type() template_name = category_type.get_show_thread_template() res = object_list( request=request, #queryset = Post.objects.filter( Q( pk = thread_id ) | Q( thread = thread ) ).order_by('postdate'), queryset=thread.get_all_posts().order_by('postdate'), allow_empty=True, template_name=template_name, extra_context={ 'thread': thread, 'allowPosting': thread.allowPosting(request.user), 'postSubject': 'Re: ' + thread.subject, 'category_type': category_type, }, template_object_name='post', paginate_by=get_sph_setting('board_post_paging'), ) res.sph_lastmodified = thread.get_latest_post().postdate return res
def showThread(request, thread_id, group = None): thread = get_object_or_404(Post.objects, pk = thread_id ) if not thread.category.has_view_permission(request.user): raise PermissionDenied() thread.viewed( request.session, request.user ) sphdata = get_current_sphdata() if sphdata != None: sphdata['subtitle'] = thread.subject category_type = thread.category.get_category_type() template_name = category_type.get_show_thread_template() res = object_list( request = request, #queryset = Post.objects.filter( Q( pk = thread_id ) | Q( thread = thread ) ).order_by('postdate'), queryset = thread.get_all_posts().order_by('postdate'), allow_empty = True, template_name = template_name, extra_context = { 'thread': thread, 'allowPosting': thread.allowPosting( request.user ), 'postSubject': 'Re: ' + thread.subject, 'category_type': category_type, }, template_object_name = 'post', paginate_by = get_sph_setting( 'board_post_paging' ), ) res.sph_lastmodified = thread.get_latest_post().postdate return res
def accounts_logout(request, group=None): sphdata = get_current_sphdata() sphdata['is_logout'] = True return view_logout( request, template_name='sphene/community/accounts/logged_out.html', )
def showSnip(request, group, snipName): snip_rendered_body = None try: snip = WikiSnip.objects.get(group=group, name__exact=snipName) except WikiSnip.DoesNotExist: snip = WikiSnip(name=snipName, group=group) if not snip.has_view_permission(): raise PermissionDenied() res = None if 'type' in request.GET: if request.GET['type'] == 'src': res = HttpResponse( snip.body, mimetype='text/plain', ) if request.GET['type'] == 'full': res = HttpResponse( snip.render(), mimetype='text/html', ) if not res: sphdata = get_current_sphdata() snip_rendered_body = False redirects = () while not snip_rendered_body or 'sphwiki_redirect_to_snip' in sphdata: if snip_rendered_body: if snip in redirects: if request.user.is_authenticated(): request.user.message_set.create( message=ugettext("Detected redirect loop.")) break redirects += (snip, ) snip = sphdata['sphwiki_redirect_to_snip'] del sphdata['sphwiki_redirect_to_snip'] snip_rendered_body = snip.render() if sphdata != None: sphdata['subtitle'] = snip.title or snip.name res = render_to_response('sphene/sphwiki/showSnip.html', { 'snip': snip, 'snipName': snipName, 'snip_rendered_body': snip_rendered_body, 'redirects': redirects, 'commentstemplate': 'sphene.sphcomments' in settings.INSTALLED_APPS and 'sphene/sphwiki/wikicomments.html' or 'sphene/sphwiki/wikicomments_unavailable.html', }, context_instance=RequestContext(request)) res.sph_lastmodified = snip.changed return res
def sph_reverse(viewname, args=(), kwargs={}): req = get_current_request() urlconf = getattr(req, "urlconf", None) sphdata = get_current_sphdata() if "group_fromhost" in sphdata and not sphdata.get("group_fromhost", False): kwargs["groupName"] = get_current_group().name elif "groupName" in kwargs: del kwargs["groupName"] return reverse(viewname, urlconf, args, kwargs)
def sph_reverse( viewname, args=(), kwargs={} ): req = get_current_request() urlconf = getattr(req, 'urlconf', None) sphdata = get_current_sphdata() if 'group_fromhost' in sphdata and \ not sphdata.get('group_fromhost', False): kwargs['groupName'] = get_current_group().name elif 'groupName' in kwargs: del kwargs['groupName'] return reverse( viewname, urlconf, args, kwargs )
def showSnip(request, group, snipName): snip_rendered_body = None status = None try: snip = WikiSnip.objects.get( group = group, name__exact = snipName ) except WikiSnip.DoesNotExist: snip = WikiSnip( name = snipName, group = group ) status = 404 if not snip.has_view_permission(): raise PermissionDenied() res = None if 'type' in request.GET: if request.GET['type'] == 'src': res = HttpResponse( snip.body, mimetype = 'text/plain', ) if request.GET['type'] == 'full': res = HttpResponse( snip.render(), mimetype = 'text/html', ) if not res: sphdata = get_current_sphdata() snip_rendered_body = False redirects = () while not snip_rendered_body or 'sphwiki_redirect_to_snip' in sphdata: if snip_rendered_body: if snip in redirects: if request.user.is_authenticated(): request.user.message_set.create( message = ugettext("Detected redirect loop.") ) break redirects += (snip,) snip = sphdata['sphwiki_redirect_to_snip'] del sphdata['sphwiki_redirect_to_snip'] snip_rendered_body = snip.render() if sphdata != None: sphdata['subtitle'] = snip.title or snip.name res = render_to_response( 'sphene/sphwiki/showSnip.html', { 'snip': snip, 'snipName' : snipName, 'snip_rendered_body': snip_rendered_body, 'redirects': redirects, 'wiki_sidebar_html': rendersidebar('W'), 'commentstemplate': 'sphene.sphcomments' in settings.INSTALLED_APPS and 'sphene/sphwiki/wikicomments.html' or 'sphene/sphwiki/wikicomments_unavailable.html', }, context_instance = RequestContext(request) ) if status is not None: res.status_code = status res.sph_lastmodified = snip.changed return res
def get(self, request, group=None, thread_id=None, **kwargs): assert group self.thread = thread = get_object_or_404(Post.objects, pk=thread_id) if not thread.category.has_view_permission(request.user): raise PermissionDenied() thread.viewed(request.session, request.user) sphdata = get_current_sphdata() if sphdata is not None: sphdata['subtitle'] = thread.subject category_type = thread.category.get_category_type() self.template_name = category_type.get_show_thread_template() return super().get(request, **kwargs)
def handlePreprocessorMacroCall(self, params): if not params.has_key( 'snip' ): return 'No "snip" parameter given to redirect macro.' sphdata = get_current_sphdata() snipName = params['snip']; try: snip = WikiSnip.objects.get( group = get_current_group(), name = snipName ) except WikiSnip.DoesNotExist: return '**Wiki snip "%s" does not exist.**' % snipName request = get_current_request() if 'redirect' in request.GET and request.GET['redirect'] == 'no': return '**Redirect to "%s" disabled.**' % snipName sphdata['sphwiki_redirect_to_snip'] = snip return "**Redirecting to %s ...**" % snipName
def handlePreprocessorMacroCall(self, params): if 'snip' not in params: return 'No "snip" parameter given to redirect macro.' sphdata = get_current_sphdata() snipName = params['snip'] try: snip = WikiSnip.objects.get(group=get_current_group(), name=snipName) except WikiSnip.DoesNotExist: return '**Wiki snip "%s" does not exist.**' % snipName request = get_current_request() if 'redirect' in request.GET and request.GET['redirect'] == 'no': return '**Redirect to "%s" disabled.**' % snipName sphdata['sphwiki_redirect_to_snip'] = snip return "**Redirecting to %s ...**" % snipName
def sph_markdown(value, arg='', oldmd=None, extra_macros={}): try: from sphene.contrib.libs.markdown import markdown except ImportError: if settings.DEBUG: raise ( template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed." ) return value else: safe_mode = arg == 'safe' macros = { 'helloWorld': SimpleHelloWorldMacro(), 'news': NewsMacro(), 'newsrss': NewsRSSLinkMacro(), 'include': IncludeMacro(), 'includetemplate': IncludeTemplateMacro(), } macros.update(extra_macros), md = markdown.Markdown( value, extensions=['footnotes', 'wikilink', 'macros', 'toc'], extension_configs={ 'wikilink': [], 'macros': [('macros', macros)], 'toc': { 'include_header_one_in_toc': True, }, }, safe_mode=safe_mode, ) md.number_headings = get_sph_setting('markdown_number_headings', True) md.top_heading_level = get_sph_setting('markdown_top_heading_level', 1) if oldmd and hasattr(oldmd, 'header_numbers'): md.header_numbers = oldmd.header_numbers ret = md.toString() if hasattr(md, 'tocDiv'): sphdata = get_current_sphdata() sphdata['toc'] = mark_safe(md.tocDiv.toxml()) return ret
def accounts_logout(request, group = None, logged_out_template_name = 'sphene/community/accounts/logged_out.html'): sphdata = get_current_sphdata() sphdata['is_logout'] = True return view_logout( request, template_name = logged_out_template_name, )
def showCategory(request, group = None, category_id = None, showType = None): """ displays either all root categories, or the contents of a category. the contents of a category could be other categories or forum threads. TODO: clean this mess up - sorry for everyone who trys to understand this function - this is is probably the oldest and ugliest in the whole SCT source. """ args = { 'group__isnull': True, 'parent__isnull': True, } categoryObject = None sphdata = get_current_sphdata() if category_id != None and category_id != '0': args['parent__isnull'] = False args['parent'] = category_id categoryObject = get_object_or_404(Category, pk = category_id) if not categoryObject.has_view_permission( request.user ): raise PermissionDenied() categoryObject.touch(request.session, request.user) blog_feed_url = reverse('sphboard-feeds', urlconf=get_current_urlconf(), args = (), kwargs = { 'url': 'latest/2' }) add_rss_feed( blog_feed_url, 'Latest Threads in %s RSS Feed' % categoryObject.name ) if sphdata != None: sphdata['subtitle'] = categoryObject.name if group != None: args['group__isnull'] = False args['group'] = group if showType == 'threads': categories = [] else: if 'group' in args: categories = Category.sph_objects.filter( group = args['group'] )#filter_for_group( args['group'] ) if 'parent' in args: categories = categories.filter( parent = category_id ) else: categories = categories.filter( parent__isnull = True ) categories = [ category for category in categories if category.has_view_permission( request.user ) ] else: categories = Category.objects.filter( **args ) context = { 'rootCategories': categories, 'category': categoryObject, 'allowPostThread': categoryObject and categoryObject.allowPostThread( request.user ), 'category_id': category_id, } try: context['search_posts_url'] = sph_reverse('sphsearchboard_posts') except: pass templateName = 'sphene/sphboard/listCategories.html' if categoryObject == None: if showType != 'threads': return render_to_response( templateName, context, context_instance = RequestContext(request) ) ## Show the latest threads from all categories. allowed_categories = get_all_viewable_categories( group, request.user ) if group != None: thread_args = { 'category__group': group } else: thread_args = { 'category__group__isnull': True } #thread_args[ 'thread__isnull'] = True thread_args[ 'category__id__in'] = allowed_categories context['isShowLatest'] = True thread_list = ThreadInformation.objects.filter( **thread_args ) else: category_type = categoryObject.get_category_type() templateName = category_type.get_threadlist_template() thread_list = categoryObject.get_thread_list() #thread_list = thread_list.extra( select = { 'latest_postdate': 'SELECT MAX(postdate) FROM sphboard_post AS postinthread WHERE postinthread.thread_id = sphboard_post.id OR postinthread.id = sphboard_post.id', 'is_sticky': 'status & %d' % POST_STATUSES['sticky'] } ) if showType != 'threads': thread_list = thread_list.order_by( '-sticky_value', '-thread_latest_postdate' ) else: thread_list = thread_list.order_by( '-thread_latest_postdate' ) res = object_list( request = request, queryset = thread_list, template_name = templateName, extra_context = context, template_object_name = 'thread', allow_empty = True, paginate_by = 10, ) res.sph_lastmodified = True return res
def get(self, request, group=None, category_id=0, show_type=None, *args, **kwargs): category_id = category_id and int(category_id) or 0 self.group = group self.request = request if group is None: raise Http404 query_args = {'group': group, 'parent__isnull': True} sphdata = get_current_sphdata() categories = [] category = None if category_id: query_args['parent__isnull'] = False query_args['parent'] = category_id category = get_object_or_404(Category, pk=category_id) if not category.has_view_permission(request.user): raise PermissionDenied() category.touch(request.session, request.user) blog_feed_url = sph_reverse('sphboard-feeds', kwargs={'category_id': category.id}) add_rss_feed(blog_feed_url, 'Latest Threads in %s RSS Feed' % category.name) if sphdata is not None: sphdata['subtitle'] = category.name elif sphdata is not None: sphdata['subtitle'] = ugettext('Forum Overview') if show_type != 'threads': categories = Category.sph_objects.filter( group=group) # filter_for_group( args['group'] ) if category: categories = categories.filter(parent=category) else: categories = categories.filter(parent__isnull=True) categories = [ category for category in categories if category.has_view_permission(request.user) ] self.prepared_context = { 'rootCategories': categories, 'category': category, 'allowPostThread': category and category.allowPostThread(request.user), 'category_id': category_id, 'show_type': show_type, } try: self.prepared_context['search_posts_url'] = sph_reverse( 'sphsearchboard_posts') except: pass return super(CategoryList, self).get(request, *args, **kwargs)
def add_rss_feed(url, label): sphdata = get_current_sphdata() if not "rss" in sphdata: sphdata["rss"] = [] sphdata["rss"].append({"url": url, "label": label})
def showCategory(request, group, category_id=None, showType=None, slug=None): """ displays either all root categories, or the contents of a category. the contents of a category could be other categories or forum threads. TODO: clean this mess up - sorry for everyone who trys to understand this function - this is is probably the oldest and ugliest in the whole SCT source. We no longer support having no group !! """ args = {"group__isnull": True, "parent__isnull": True} categoryObject = None sphdata = get_current_sphdata() if category_id != None and category_id != "0": args["parent__isnull"] = False args["parent"] = category_id categoryObject = get_object_or_404(Category, pk=category_id) if not categoryObject.has_view_permission(request.user): raise PermissionDenied() categoryObject.touch(request.session, request.user) blog_feed_url = sph_reverse("sphboard-feeds", kwargs={"url": "latest/%s" % categoryObject.id}) add_rss_feed(blog_feed_url, "Latest Threads in %s RSS Feed" % categoryObject.name) if sphdata != None: sphdata["subtitle"] = categoryObject.name elif sphdata is not None: sphdata["subtitle"] = ugettext("Forum Overview") if group != None: args["group__isnull"] = False args["group"] = group if showType == "threads": categories = [] else: if "group" in args: categories = Category.sph_objects.filter(group=args["group"]) # filter_for_group( args['group'] ) if "parent" in args: categories = categories.filter(parent=category_id) else: categories = categories.filter(parent__isnull=True) categories = [category for category in categories if category.has_view_permission(request.user)] else: categories = Category.objects.filter(**args) context = { "rootCategories": categories, "category": categoryObject, "allowPostThread": categoryObject and categoryObject.allowPostThread(request.user), "category_id": category_id, } try: context["search_posts_url"] = sph_reverse("sphsearchboard_posts") except: pass templateName = "sphene/sphboard/listCategories.html" if categoryObject == None: if showType != "threads": return render_to_response(templateName, context, context_instance=RequestContext(request)) ## Show the latest threads from all categories. allowed_categories = get_all_viewable_categories(group, request.user) if group != None: thread_args = {"category__group": group} else: thread_args = {"category__group__isnull": True} # thread_args[ 'thread__isnull'] = True thread_args["category__id__in"] = allowed_categories context["isShowLatest"] = True thread_list = ThreadInformation.objects.filter(**thread_args) else: category_type = categoryObject.get_category_type() templateName = category_type.get_threadlist_template() thread_list = categoryObject.get_thread_list() # thread_list = thread_list.extra( select = { 'latest_postdate': 'SELECT MAX(postdate) FROM sphboard_post AS postinthread WHERE postinthread.thread_id = sphboard_post.id OR postinthread.id = sphboard_post.id', 'is_sticky': 'status & %d' % POST_STATUSES['sticky'] } ) if showType != "threads": thread_list = thread_list.order_by("-sticky_value", "-thread_latest_postdate") else: thread_list = thread_list.order_by("-thread_latest_postdate") res = object_list( request=request, queryset=thread_list, template_name=templateName, extra_context=context, template_object_name="thread", allow_empty=True, paginate_by=10, ) res.sph_lastmodified = True return res
def showCategory(request, group, category_id=None, showType=None, slug=None): """ displays either all root categories, or the contents of a category. the contents of a category could be other categories or forum threads. TODO: clean this mess up - sorry for everyone who trys to understand this function - this is is probably the oldest and ugliest in the whole SCT source. We no longer support having no group !! """ args = { 'group__isnull': True, 'parent__isnull': True, } categoryObject = None sphdata = get_current_sphdata() if category_id != None and category_id != '0': args['parent__isnull'] = False args['parent'] = category_id categoryObject = get_object_or_404(Category, pk=category_id) if not categoryObject.has_view_permission(request.user): raise PermissionDenied() categoryObject.touch(request.session, request.user) blog_feed_url = sph_reverse('sphboard-feeds', kwargs={'category_id': categoryObject.id}) add_rss_feed(blog_feed_url, 'Latest Threads in %s RSS Feed' % categoryObject.name) if sphdata != None: sphdata['subtitle'] = categoryObject.name elif sphdata is not None: sphdata['subtitle'] = ugettext('Forum Overview') if group != None: args['group__isnull'] = False args['group'] = group if showType == 'threads': categories = [] else: if 'group' in args: categories = Category.sph_objects.filter( group=args['group']) #filter_for_group( args['group'] ) if 'parent' in args: categories = categories.filter(parent=category_id) else: categories = categories.filter(parent__isnull=True) categories = [ category for category in categories if category.has_view_permission(request.user) ] else: categories = Category.objects.filter(**args) context = { 'rootCategories': categories, 'category': categoryObject, 'allowPostThread': categoryObject and categoryObject.allowPostThread(request.user), 'category_id': category_id, } try: context['search_posts_url'] = sph_reverse('sphsearchboard_posts') except: pass templateName = 'sphene/sphboard/listCategories.html' if categoryObject == None: if showType != 'threads': return render_to_response(templateName, context, context_instance=RequestContext(request)) ## Show the latest threads from all categories. allowed_categories = get_all_viewable_categories(group, request.user) if group != None: thread_args = {'category__group': group} else: thread_args = {'category__group__isnull': True} #thread_args[ 'thread__isnull'] = True thread_args['category__id__in'] = allowed_categories thread_args['root_post__is_hidden'] = 0 context['isShowLatest'] = True thread_list = ThreadInformation.objects.filter(**thread_args) else: category_type = categoryObject.get_category_type() templateName = category_type.get_threadlist_template() thread_list = categoryObject.get_thread_list() #thread_list = thread_list.extra( select = { 'latest_postdate': 'SELECT MAX(postdate) FROM sphboard_post AS postinthread WHERE postinthread.thread_id = sphboard_post.id OR postinthread.id = sphboard_post.id', 'is_sticky': 'status & %d' % POST_STATUSES['sticky'] } ) if showType != 'threads': thread_list = thread_list.order_by('-sticky_value', '-thread_latest_postdate') else: thread_list = thread_list.order_by('-thread_latest_postdate') thread_list = thread_list.select_related('root_post', 'latest_post', 'root_post__category', 'root_post__author', 'latest_post__author') res = object_list( request=request, queryset=thread_list, template_name=templateName, extra_context=context, template_object_name='thread', allow_empty=True, paginate_by=10, ) res.sph_lastmodified = True return res