Nodes and Views: Building Website structure

Nodes

class philo.models.nodes.Node(*args, **kwargs)

Bases: philo.models.base.SlugTreeEntity

Nodes are the basic building blocks of a website using Philo. They define the URL hierarchy and connect each URL to a View subclass instance which is used to generate an HttpResponse.

view

GenericForeignKey to a non-abstract subclass of View

accepts_subpath

A property shortcut for self.view.accepts_subpath

render_to_response(request, extra_context=None)

This is a shortcut method for View.render_to_response()

get_absolute_url(*moreargs, **morekwargs)

This is essentially a shortcut for calling construct_url() without a subpath.

Returns:The absolute url of the node on the current site.
construct_url(subpath='/', request=None, with_domain=False, secure=False)

This method will do its best to construct a URL based on the Node’s location. If with_domain is True, that URL will include a domain and a protocol; if secure is True as well, the protocol will be https. The request will be used to construct a domain in cases where a call to Site.objects.get_current() fails.

Node urls will not contain a trailing slash unless a subpath is provided which ends with a trailing slash. Subpaths are expected to begin with a slash, as if returned by django.core.urlresolvers.reverse().

Because this method will be called frequently and will always try to reverse philo-root, the results of that reversal will be cached by default. This can be disabled by setting PHILO_CACHE_PHILO_ROOT to False.

construct_url() may raise the following exceptions:

  • NoReverseMatch if “philo-root” is not reversable – for example, if philo.urls is not included anywhere in your urlpatterns.
  • Site.DoesNotExist if with_domain is True but no Site or RequestSite can be built.
  • AncestorDoesNotExist if the root node of the site isn’t an ancestor of the node constructing the URL.
Parameters:
  • subpath (string) – The subpath to be constructed beyond beyond the node’s URL.
  • requestHttpRequest instance. Will be used to construct a RequestSite if Site.objects.get_current() fails.
  • with_domain – Whether the constructed URL should include a domain name and protocol.
  • secure – Whether the protocol, if included, should be http:// or https://.
Returns:

A constructed url for accessing the given subpath of the current node instance.

Views

Abstract View Models

class philo.models.nodes.View(*args, **kwargs)

Bases: philo.models.base.Entity

View is an abstract model that represents an item which can be “rendered”, generally in response to an HttpRequest.

accepts_subpath

An attribute on the class which defines whether this View can handle subpaths. Default: False

classmethod handles_subpath(subpath)

Returns True if the View handles the given subpath, and False otherwise.

reverse(view_name=None, args=None, kwargs=None, node=None, obj=None)

If accepts_subpath is True, try to reverse a URL using the given parameters using self as the urlconf.

If obj is provided, get_reverse_params() will be called and the results will be combined with any view_name, args, and kwargs that may have been passed in.

Parameters:
  • view_name – The name of the view to be reversed.
  • args – Extra args for reversing the view.
  • kwargs – A dictionary of arguments for reversing the view.
  • node – The node whose subpath this is.
  • obj – An object to be passed to get_reverse_params() to generate a view_name, args, and kwargs for reversal.
Returns:

A subpath beyond the node that reverses the view, or an absolute url that reverses the view if a node was passed in.

Raises:
get_reverse_params(obj)

This method is not implemented on the base class. It should return a (view_name, args, kwargs) tuple suitable for reversing a url for the given obj using self as the urlconf. If a reversal will not be possible, this method should raise ViewCanNotProvideSubpath.

attributes_with_node(node, mapper=<class 'philo.utils.entities.LazyPassthroughAttributeMapper'>)

Returns a LazyPassthroughAttributeMapper which can be used to directly retrieve the values of Attributes related to the View, falling back on the Attributes of the passed-in Node and its ancestors.

render_to_response(request, extra_context=None)

Renders the View as an HttpResponse. This will raise MIDDLEWARE_NOT_CONFIGURED if the request doesn’t have an attached Node. This can happen if the RequestNodeMiddleware is not in settings.MIDDLEWARE_CLASSES or if it is not functioning correctly.

render_to_response() will send the view_about_to_render signal, then call actually_render_to_response(), and finally send the view_finished_rendering signal before returning the response.

actually_render_to_response(request, extra_context=None)

Concrete subclasses must override this method to provide the business logic for turning a request and extra_context into an HttpResponse.

class philo.models.nodes.MultiView(*args, **kwargs)

Bases: philo.models.nodes.View

MultiView is an abstract model which represents a section of related pages - for example, a BlogView might have a foreign key to Pages for an index, an entry detail, an entry archive by day, and so on. MultiView subclasses View, and defines the following additional methods and attributes:

accepts_subpath

Same as View.accepts_subpath. Default: True

urlpatterns

Returns urlpatterns that point to views (generally methods on the class). MultiViews can be thought of as “managing” these subpaths.

actually_render_to_response(request, extra_context=None)

Resolves the remaining subpath left after finding this View‘s node using self.urlpatterns and renders the view function (or method) found with the appropriate args and kwargs.

get_context()

Hook for providing instance-specific context - such as the value of a Field - to any view methods on the instance.

basic_view(field_name)

Given the name of a field on the class, accesses the value of that field and treats it as a View instance. Creates a basic context based on self.get_context() and any extra_context that was passed in, then calls the View instance’s render_to_response() method. This method is meant to be called to return a view function appropriate for urlpatterns.

Parameters:field_name – The name of a field on the instance which contains a View subclass instance.
Returns:A simple view function.

Example:

class Foo(Multiview):
        page = models.ForeignKey(Page)
        
        @property
        def urlpatterns(self):
                urlpatterns = patterns('',
                        url(r'^$', self.basic_view('page'))
                )
                return urlpatterns

Concrete View Subclasses

class philo.models.nodes.Redirect(*args, **kwargs)

Bases: philo.models.nodes.TargetURLModel, philo.models.nodes.View

Represents a 301 or 302 redirect to a different url on an absolute or relative path.

STATUS_CODES

A choices tuple of redirect status codes (temporary or permanent).

status_code

An IntegerField which uses STATUS_CODES as its choices. Determines whether the redirect is considered temporary or permanent.

actually_render_to_response(request, extra_context=None)

Returns an HttpResponseRedirect to self.target_url.

class philo.models.nodes.File(*args, **kwargs)

Bases: philo.models.nodes.View

Stores an arbitrary file.

name

The name of the uploaded file. This is meant for finding the file again later, not for display.

mimetype

Defines the mimetype of the uploaded file. This will not be validated. If no mimetype is provided, it will be automatically generated based on the filename.

file

Contains the uploaded file. Files are uploaded to philo/files/%Y/%m/%d.

Pages

Pages are the most frequently used View subclass. They define a basic HTML page and its associated content. Each Page renders itself according to a Template. The Template may contain container tags, which define related Contentlets and ContentReferences for any page using that Template.

class philo.models.pages.Page(*args, **kwargs)

Bases: philo.models.nodes.View

Represents a page - something which is rendered according to a Template. The page will have a number of related Contentlets and ContentReferences depending on the template selected - but these will appear only after the page has been saved with that template.

template

A ForeignKey to the Template used to render this Page.

title

The name of this page. Chances are this will be used for organization - i.e. finding the page in a list of pages - rather than for display.

get_containers()

Returns the results containers for the related template. This is a tuple containing the specs of all containers in the Template‘s code. The value will be cached on the instance so that multiple accesses will be less expensive.

containers

Returns the results containers for the related template. This is a tuple containing the specs of all containers in the Template‘s code. The value will be cached on the instance so that multiple accesses will be less expensive.

render_to_string(request=None, extra_context=None)

In addition to rendering as an HttpResponse, a Page can also render as a string. This means, for example, that Pages can be used to render emails or other non-HTML content with the same container-based functionality as is used for HTML.

The Page will add itself to the context as page and its attributes as attributes. If a request is provided, then request.node will also be added to the context as node and attributes will be set to the result of calling attributes_with_node() with that Node.

actually_render_to_response(request, extra_context=None)

Returns an HttpResponse with the content of the render_to_string() method and the mimetype set to the mimetype of the related Template.

clean_fields(exclude=None)

This is an override of the default model clean_fields method. Essentially, in addition to validating the fields, this method validates the Template instance that is used to render this Page. This is useful for catching template errors before they show up as 500 errors on a live site.

class philo.models.pages.Template(*args, **kwargs)

Bases: philo.models.base.SlugTreeEntity

Represents a database-driven django template.

name

The name of the template. Used for organization and debugging.

documentation

Can be used to let users know what the template is meant to be used for.

mimetype

Defines the mimetype of the template. This is not validated. Default: text/html.

code

An insecure TemplateField containing the django template code for this template.

get_containers()

Returns a tuple where the first item is a list of names of contentlets referenced by containers, and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers. This will break if there is a recursive extends or includes in the template code. Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won’t work.

containers

Returns a tuple where the first item is a list of names of contentlets referenced by containers, and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers. This will break if there is a recursive extends or includes in the template code. Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won’t work.

class philo.models.pages.Contentlet(*args, **kwargs)

Represents a piece of content on a page. This content is treated as a secure TemplateField.

page

The page which this Contentlet is related to.

name

This represents the name of the container as defined by a container tag.

content

A secure TemplateField holding the content for this Contentlet. Note that actually using this field as a template requires use of the include_string template tag.

class philo.models.pages.ContentReference(*args, **kwargs)

Represents a model instance related to a page.

page

The page which this ContentReference is related to.

name

This represents the name of the container as defined by a container tag.

content

A GenericForeignKey to a model instance. The content type of this instance is defined by the container tag which defines this ContentReference.

Project Versions

Table Of Contents

Previous topic

Entities and Attributes

Next topic

Collections

This Page