diff --git a/.travis.yml b/.travis.yml index 3e0a4bb98..48c8494b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,11 @@ -# Python releases to test language: python +# Test matrix python: - - 2.7 -# Django releases + - 2.7 + - 3.4 env: - - DJANGO_VERSION=Django==1.6.2 + - DJANGO_VERSION=Django==1.6.5 + #- DJANGO_VERSION=Django==1.7.0 # Services services: - redis-server @@ -12,7 +13,7 @@ services: # Package installation install: - python setup.py install - - pip install psycopg2 pyelasticsearch elasticutils==0.8.2 wand embedly + - pip install psycopg2 elasticsearch wand embedly - pip install coveralls # Pre-test configuration before_script: diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 19eec21e7..8ebe93959 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -3,6 +3,8 @@ Changelog 0.4 (xx.xx.20xx) ~~~~~~~~~~~~~~~~ + * ElasticUtils/pyelasticsearch swapped for elasticsearch-py + * Added notification preferences * Added 'original' as a resizing rule supported by the 'image' tag * Hallo.js updated to version 1.0.4 * Snippets are now ordered alphabetically @@ -14,9 +16,15 @@ Changelog * Aesthetic improvements to preview experience * 'image' tag now accepts extra keyword arguments to be output as attributes on the img tag * Added an 'attrs' property to image rendition objects to output src, width, height and alt attributes all in one go + * Added 'construct_whitelister_element_rules' hook for customising the HTML whitelist used when saving rich text fields + * Added 'in_menu' and 'not_in_menu' methods to PageQuerySet + * Added 'get_next_siblings' and 'get_prev_siblings' to Page + * Added init_new_page signal * Fix: Animated GIFs are now coalesced before resizing * Fix: Wand backend clones images before modifying them * Fix: Admin breadcrumb now positioned correctly on mobile + * Fix: Page chooser breadcrumb now updates the chooser modal instead of linking to Explorer + * Fix: Embeds - Fixed crash when no HTML field is sent back from the embed provider 0.3.1 (03.06.2014) ~~~~~~~~~~~~~~~~~~ diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index fcba8d2d7..df81741c9 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -29,6 +29,7 @@ Contributors * Tom Talbot * Jeffrey Hearn * Robert Clark +* Tim Heap Translators =========== diff --git a/README.rst b/README.rst index 0e68b8c6b..341e8d2bf 100644 --- a/README.rst +++ b/README.rst @@ -46,5 +46,9 @@ Wagtail supports Django 1.6.2+ on Python 2.6 and 2.7. Django 1.7 and Python 3 su Contributing ~~~~~~~~~~~~ -If you're a Python or Django developer, fork the repo and get stuck in! Send us a useful pull request and we'll post you a `t-shirt `_. Our immediate priorities are better docs, more tests, internationalisation and localisation. +If you're a Python or Django developer, fork the repo and get stuck in! + +We suggest you start by checking the `Help develop me! `_ label. + +Send us a useful pull request and we'll post you a `t-shirt `_. diff --git a/docs/building_your_site/djangodevelopers.rst b/docs/building_your_site/djangodevelopers.rst index b4899d70f..aeabb264f 100644 --- a/docs/building_your_site/djangodevelopers.rst +++ b/docs/building_your_site/djangodevelopers.rst @@ -1,9 +1,15 @@ For Django developers ===================== +.. contents:: Contents + :local: + .. note:: This documentation is currently being written. +Overview +~~~~~~~~ + Wagtail requires a little careful setup to define the types of content that you want to present through your website. The basic unit of content in Wagtail is the ``Page``, and all of your page-level content will inherit basic webpage-related properties from it. But for the most part, you will be defining content yourself, through the construction of Django models using Wagtail's ``Page`` as a base. Wagtail organizes content created from your models in a tree, which can have any structure and combination of model objects in it. Wagtail doesn't prescribe ways to organize and interrelate your content, but here we've sketched out some strategies for organizing your models. @@ -203,7 +209,6 @@ Methods: * get_context * get_template * is_navigable -* get_other_siblings * get_ancestors * get_descendants * get_siblings @@ -269,6 +274,7 @@ not_type(self, model): return self.get_query_set().not_type(model) +.. _wagtail_site_admin: Site ~~~~ @@ -278,3 +284,13 @@ Django's built-in admin interface provides the way to map a "site" (hostname or Access this by going to ``/django-admin/`` and then "Home › Wagtailcore › Sites." To try out a development site, add a single site with the hostname ``localhost`` at port ``8000`` and map it to one of the pieces of content you have created. Wagtail's developers plan to move the site settings into the Wagtail admin interface. + + +.. _redirects: + +Redirects +~~~~~~~~~ + +Wagtail provides a simple interface for creating arbitrary redirects to and from any URL. + +.. image:: ../images/screen_wagtail_redirects.png diff --git a/docs/building_your_site/frontenddevelopers.rst b/docs/building_your_site/frontenddevelopers.rst index a8cf6a25e..35aa1c845 100644 --- a/docs/building_your_site/frontenddevelopers.rst +++ b/docs/building_your_site/frontenddevelopers.rst @@ -1,7 +1,8 @@ For Front End developers ======================== -.. contents:: +.. contents:: Contents + :local: ======================== Overview @@ -90,6 +91,9 @@ In addition to Django's standard tags and filters, Wagtail provides some of its Images (tag) ~~~~~~~~~~~~ +.. versionchanged:: 0.4 + The 'image_tags' tags library was renamed to 'wagtailimages_tags' + The ``image`` tag inserts an XHTML-compatible ``img`` element into the page, setting its ``src``, ``width``, ``height`` and ``alt``. See also :ref:`image_tag_alt`. The syntax for the tag is thus:: @@ -100,7 +104,7 @@ For example: .. code-block:: django - {% load image %} + {% load wagtailimages_tags %} ... {% image self.photo width-400 %} @@ -186,35 +190,57 @@ The available resizing methods are: More control over the ``img`` tag --------------------------------- -In some cases greater control over the ``img`` tag is required, for example to add a custom ``class``. Rather than generating the ``img`` element for you, Wagtail can assign the relevant data to another object using Django's ``as`` syntax: +Wagtail provides two shorcuts to give greater control over the ``img`` element: + +.. versionadded:: 0.4 +**Adding attributes to the {% image %} tag** + +Extra attributes can be specified with the syntax ``attribute="value"``: .. code-block:: django - - {% load image %} - ... + + {% image self.photo width-400 class="foo" id="bar" %} + +No validation is performed on attributes add in this way by the developer. It's possible to add `src`, `width`, `height` and `alt` of your own that might conflict with those generated by the tag itself. + + +**Generating the image "as"** + +Wagtail can assign the image data to another object using Django's ``as`` syntax: + +.. code-block:: django + {% image self.photo width-400 as tmp_photo %} {{ tmp_photo.alt }} +.. versionadded:: 0.4 +The ``attrs`` shortcut +----------------------- + You can also use the ``attrs`` property as a shorthand to output the ``src``, ``width``, ``height`` and ``alt`` attributes in one go: .. code-block:: django + .. _rich-text-filter: Rich text (filter) ~~~~~~~~~~~~~~~~~~ +.. versionchanged:: 0.4 + The 'rich_text' tags library was renamed to 'wagtailcore_tags' + This filter takes a chunk of HTML content and renders it as safe HTML in the page. Importantly it also expands internal shorthand references to embedded images and links made in the Wagtail editor into fully-baked HTML ready for display. Only fields using ``RichTextField`` need this applied in the template. .. code-block:: django - {% load rich_text %} + {% load wagtailcore_tags %} ... {{ self.body|richtext }} @@ -250,6 +276,9 @@ Wagtail embeds and images are included at their full width, which may overflow t Internal links (tag) ~~~~~~~~~~~~~~~~~~~~ +.. versionchanged:: 0.4 + The 'pageurl' tags library was renamed to 'wagtailcore_tags' + pageurl -------- @@ -257,7 +286,7 @@ Takes a Page object and returns a relative URL (``/foo/bar/``) if within the sam .. code-block:: django - {% load pageurl %} + {% load wagtailcore_tags %} ... @@ -268,7 +297,7 @@ Takes any ``slug`` as defined in a page's "Promote" tab and returns the URL for .. code-block:: django - {% load slugurl %} + {% load wagtailcore_tags %} ... diff --git a/docs/editing_api.rst b/docs/editing_api.rst index 88fc9458f..609237e76 100644 --- a/docs/editing_api.rst +++ b/docs/editing_api.rst @@ -1,28 +1,26 @@ -Editing API +Defining models with the Editing API =========== .. note:: This documentation is currently being written. - Wagtail provides a highly-customizable editing interface consisting of several components: - * **Fields** — built-in content types to augment the basic types provided by Django. + * **Fields** — built-in content types to augment the basic types provided by Django * **Panels** — the basic editing blocks for fields, groups of fields, and related object clusters * **Choosers** — interfaces for finding related objects in a ForeignKey relationship -Configuring your models to use these components will shape the Wagtail editor to your needs. Wagtail also provides an API for injecting custom CSS and Javascript for further customization, including extending the hallo.js rich text editor. +Configuring your models to use these components will shape the Wagtail editor to your needs. Wagtail also provides an API for injecting custom CSS and JavaScript for further customization, including extending the hallo.js rich text editor. There is also an Edit Handler API for creating your own Wagtail editor components. - Defining Panels ~~~~~~~~~~~~~~~ -A "panel" is the basic editing block in Wagtail. Wagtail will automatically pick the appropriate editing widget for most Django field types, you just need to add a panel for each field you want to show in the Wagtail page editor, in the order you want them to appear. +A "panel" is the basic editing block in Wagtail. Wagtail will automatically pick the appropriate editing widget for most Django field types; implementors just need to add a panel for each field they want to show in the Wagtail page editor, in the order they want them to appear. -There are three types of panels: +There are four basic types of panels: ``FieldPanel( field_name, classname=None )`` This is the panel used for basic Django field types. ``field_name`` is the name of the class property used in your model definition. ``classname`` is a string of optional CSS classes given to the panel which are used in formatting and scripted interactivity. By default, panels are formatted as inset fields. The CSS class ``full`` can be used to format the panel so it covers the full width of the Wagtail page editor. The CSS class ``title`` can be used to mark a field as the source for auto-generated slug strings. @@ -30,10 +28,21 @@ There are three types of panels: ``MultiFieldPanel( children, heading="", classname=None )`` This panel condenses several ``FieldPanel`` s or choosers, from a list or tuple, under a single ``heading`` string. - ``InlinePanel( base_model, relation_name, panels=None, label='', help_text='' )`` + ``InlinePanel( base_model, relation_name, panels=None, classname=None, label='', help_text='' )`` This panel allows for the creation of a "cluster" of related objects over a join to a separate model, such as a list of related links or slides to an image carousel. This is a very powerful, but tricky feature which will take some space to cover, so we'll skip over it for now. For a full explanation on the usage of ``InlinePanel``, see :ref:`inline_panels`. -Wagtail provides a tabbed interface to help organize panels. ``content_panels`` is the main tab, used for the meat of your model content. The other, ``promote_panels``, is suggested for organizing metadata about the content, such as SEO information and other machine-readable information. Since you're writing the panel definitions, you can organize them however you want. + ``FieldRowPanel( children, classname=None)`` + This panel is purely aesthetic. It creates a columnar layout in the editing interface, where each of the child Panels appears alongside each other rather than below. Use of FieldRowPanel particularly helps reduce the "snow-blindness" effect of seeing so many fields on the page, for complex models. It also improves the perceived association between fields of a similar nature. For example if you created a model representing an "Event" which had a starting date and ending date, it may be intuitive to find the start and end date on the same "row". + + FieldRowPanel should be used in combination with ``col*`` classnames added to each of the child Panels of the FieldRowPanel. The Wagtail editing interface is layed out using a grid system, in which the maximum width of the editor is 12 columns wide. Classes ``col1``-``col12`` can be applied to each child of a FieldRowPanel. The class ``col3`` will ensure that field appears 3 columns wide or a quarter the width. ``col4`` would cause the field to be 4 columns wide, or a third the width. + + **(In addition to these four, there are also Chooser Panels, detailed below.)** + +Wagtail provides a tabbed interface to help organize panels. Three such tabs are provided: + +* ``content_panels`` is the main tab, used for the bulk of your model's fields. +* ``promote_panels`` is suggested for organizing fields regarding the promotion of the page around the site and the Internet. For example, a field to dictate whether the page should show in site-wide menus, descriptive text that should appear in site search results, SEO-friendly titles, OpenGraph meta tag content and other machine-readable information. +* ``settings_panels`` is essentially for non-copy fields. By default it contains the page's scheduled publishing fields. Other suggested fields could include a field to switch between one layout/style and another. Let's look at an example of a panel definition: @@ -55,7 +64,10 @@ Let's look at an example of a panel definition: ExamplePage.content_panels = [ FieldPanel('title', classname="full title"), FieldPanel('body', classname="full"), - FieldPanel('date'), + FieldRowPanel([ + FieldPanel('start_date', classname="col3"), + FieldPanel('end_date', classname="col3"), + ]), ImageChooserPanel('splash_image'), DocumentChooserPanel('free_download'), PageChooserPanel('related_page'), @@ -119,7 +131,7 @@ One of the features of Wagtail is a unified image library, which you can access on_delete=models.SET_NULL, related_name='+' ) - + BookPage.content_panels = [ ImageChooserPanel('cover'), # ... @@ -225,7 +237,7 @@ Snippets are vanilla Django models you create yourself without a Wagtail-provide on_delete=models.SET_NULL, related_name='+' ) - + BookPage.content_panels = [ SnippetChooserPanel('advert', Advert), # ... @@ -254,6 +266,12 @@ Titles Use ``classname="title"`` to make Page's built-in title field stand out with more vertical padding. +Col* +------ + +Fields within a ``FieldRowPanel`` can have their width dictated in terms of the number of columns it should span. The ``FieldRowPanel`` is always considered to be 12 columns wide regardless of browser size or the nesting of ``FieldRowPanel`` in any other type of panel. Specify a number of columns thus: ``col3``, ``col4``, ``col6`` etc (up to 12). The resulting width with be *relative* to the full width of the ``FieldRowPanel``. + + Required Fields --------------- @@ -346,9 +364,9 @@ The ``RelatedLink`` class is a vanilla Django abstract model. The ``BookPageRela For another example of using model clusters, see :ref:`tagging` -For more on ``django-modelcluster``, visit `the django-modelcluster github project page`_ ). +For more on ``django-modelcluster``, visit `the django-modelcluster github project page`_. -.. _the django-modelcluster github page: https://github.com/torchbox/django-modelcluster +.. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster .. _extending_wysiwyg: @@ -366,11 +384,9 @@ hallo.js plugin names are prefixed with the ``"IKS."`` namespace, but the ``name For information on developing custom hallo.js plugins, see the project's page: https://github.com/bergie/hallo - Edit Handler API ~~~~~~~~~~~~~~~~ - Admin Hooks ----------- @@ -484,7 +500,7 @@ Where ``'hook'`` is one of the following hook strings and ``function`` is a func .. _construct_main_menu: ``construct_main_menu`` - Add, remove, or alter ``MenuItem`` objects from the Wagtail admin menu. The callable passed to this hook must take a ``request`` object and a list of ``menu_items``; it must return a list of menu items. New items can be constructed from the ``MenuItem`` class by passing in: a ``label`` which will be the text in the menu item, the URL of the admin page you want the menu item to link to (usually by calling ``reverse()`` on the admin view you've set up), CSS class ``name`` applied to the wrapping ``
  • `` of the menu item as ``"menu-{name}"``, CSS ``classnames`` which are used to give the link an icon, and an ``order`` integer which determine's the item's place in the menu. + Add, remove, or alter ``MenuItem`` objects from the Wagtail admin menu. The callable passed to this hook must take a ``request`` object and a list of ``menu_items``; it must return a list of menu items. New items can be constructed from the ``MenuItem`` class by passing in: a ``label`` which will be the text in the menu item, the URL of the admin page you want the menu item to link to (usually by calling ``reverse()`` on the admin view you've set up), CSS class ``name`` applied to the wrapping ``
  • `` of the menu item as ``"menu-{name}"``, CSS ``classnames`` which are used to give the link an icon, and an ``order`` integer which determine's the item's place in the menu. .. code-block:: python @@ -546,6 +562,28 @@ Where ``'hook'`` is one of the following hook strings and ``function`` is a func + 'demo/css/vendor/font-awesome/css/font-awesome.min.css">') hooks.register('insert_editor_css', editor_css) +.. _construct_whitelister_element_rules: + +``construct_whitelister_element_rules`` + .. versionadded:: 0.4 + Customise the rules that define which HTML elements are allowed in rich text areas. By default only a limited set of HTML elements and attributes are whitelisted - all others are stripped out. The callables passed into this hook must return a dict, which maps element names to handler functions that will perform some kind of manipulation of the element. These handler functions receive the element as a `BeautifulSoup `_ Tag object. + + The ``wagtail.wagtailcore.whitelist`` module provides a few helper functions to assist in defining these handlers: ``allow_without_attributes``, a handler which preserves the element but strips out all of its attributes, and ``attribute_rule`` which accepts a dict specifying how to handle each attribute, and returns a handler function. This dict will map attribute names to either True (indicating that the attribute should be kept), False (indicating that it should be dropped), or a callable (which takes the initial attribute value and returns either a final value for the attribute, or None to drop the attribute). + + For example, the following hook function will add the ``
    `` element to the whitelist, and allow the ``target`` attribute on ```` elements: + + .. code-block:: python + + from wagtail.wagtailadmin import hooks + from wagtail.wagtailcore.whitelist import attribute_rule, check_url, allow_without_attributes + + def whitelister_element_rules(): + return { + 'blockquote': allow_without_attributes, + 'a': attribute_rule({'href': check_url, 'target': True}), + } + hooks.register('construct_whitelister_element_rules', whitelister_element_rules) + Image Formats in the Rich Text Editor ------------------------------------- @@ -589,4 +627,3 @@ Custom Choosers Tests ----- - diff --git a/docs/editor_manual/new_pages/inserting_videos.rst b/docs/editor_manual/new_pages/inserting_videos.rst index 038f3e427..1b32c0d1c 100644 --- a/docs/editor_manual/new_pages/inserting_videos.rst +++ b/docs/editor_manual/new_pages/inserting_videos.rst @@ -1,3 +1,6 @@ + +.. _inserting_videos: + Inserting videos into body content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -9,4 +12,4 @@ As well as inserting videos into a carousel, Wagtail's rich text fields allow yo .. image:: ../../images/screen21_video_in_editor.png -* A placeholder with the name of the video and a screenshot will be inserted into the text area. Clicking the X in the top corner will remove the video. \ No newline at end of file +* A placeholder with the name of the video and a screenshot will be inserted into the text area. Clicking the X in the top corner will remove the video. diff --git a/docs/form_builder.rst b/docs/form_builder.rst index 9aa220e19..a141cc376 100644 --- a/docs/form_builder.rst +++ b/docs/form_builder.rst @@ -1,3 +1,6 @@ + +.. _form_builder: + Form builder ============ diff --git a/docs/images/screen_wagtail_redirects.png b/docs/images/screen_wagtail_redirects.png new file mode 100644 index 000000000..516bdd046 Binary files /dev/null and b/docs/images/screen_wagtail_redirects.png differ diff --git a/docs/index.rst b/docs/index.rst index abbb7fdbe..5d2344b18 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -9,6 +9,7 @@ It supports Django 1.6.2+ on Python 2.6 and 2.7. Django 1.7 and Python 3 support :maxdepth: 3 gettingstarted + settings building_your_site/index editing_api snippets @@ -19,6 +20,7 @@ It supports Django 1.6.2+ on Python 2.6 and 2.7. Django 1.7 and Python 3 support deploying performance static_site_generation + management_commands contributing support roadmap diff --git a/docs/management_commands.rst b/docs/management_commands.rst new file mode 100644 index 000000000..3f77fc3ef --- /dev/null +++ b/docs/management_commands.rst @@ -0,0 +1,52 @@ +Management commands +=================== + +publish_scheduled_pages +----------------------- + +:code:`./manage.py publish_scheduled_pages` + +This command publishes or unpublishes pages that have had these actions scheduled by an editor. It is recommended to run this command once an hour. + +fixtree +------- + +:code:`./manage.py fixtree` + +This command scans for errors in your database and attempts to fix any issues it finds. + +move_pages +---------- + +:code:`manage.py move_pages from to` + +This command moves a selection of pages from one section of the tree to another. + +Options: + + - **from** + This is the **id** of the page to move pages from. All descendants of this page will be moved to the destination. After the operation is complete, this page will have no children. + + - **to** + This is the **id** of the page to move pages to. + +update_index +------------ + +:code:`./manage.py update_index` + +This command rebuilds the search index from scratch. It is only required when using Elasticsearch. + +It is recommended to run this command once a week and at the following times: + + - whenever any pages have been created through a script (after an import, for example) + - whenever any changes have been made to models or search configuration + +The search may not return any results while this command is running, so avoid running it at peak times. + +search_garbage_collect +---------------------- + +:code:`./manage.py search_garbage_collect` + +Wagtail keeps a log of search queries that are popular on your website. On high traffic websites, this log may get big and you may want to clean out old search queries. This command cleans out all search query logs that are more than one week old. diff --git a/docs/settings.rst b/docs/settings.rst new file mode 100644 index 000000000..018af5d01 --- /dev/null +++ b/docs/settings.rst @@ -0,0 +1,603 @@ + +============================== +Configuring Django for Wagtail +============================== + +To install Wagtail completely from scratch, create a new Django project and an app within that project. For instructions on these tasks, see `Writing your first Django app`_. Your project directory will look like the following:: + + myproject/ + myproject/ + __init__.py + settings.py + urls.py + wsgi.py + myapp/ + __init__.py + models.py + tests.py + admin.py + views.py + manage.py + +From your app directory, you can safely remove ``admin.py`` and ``views.py``, since Wagtail will provide this functionality for your models. Configuring Django to load Wagtail involves adding modules and variables to ``settings.py`` and urlconfs to ``urls.py``. For a more complete view of what's defined in these files, see `Django Settings`_ and `Django URL Dispatcher`_. + +.. _Writing your first Django app: https://docs.djangoproject.com/en/dev/intro/tutorial01/ + +.. _Django Settings: https://docs.djangoproject.com/en/dev/topics/settings/ + +.. _Django URL Dispatcher: https://docs.djangoproject.com/en/dev/topics/http/urls/ + +What follows is a settings reference which skips many boilerplate Django settings. If you just want to get your Wagtail install up quickly without fussing with settings at the moment, see :ref:`complete_example_config`. + + +Middleware (settings.py) +~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: python + + MIDDLEWARE_CLASSES = ( + 'django.middleware.common.CommonMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + + 'wagtail.wagtailcore.middleware.SiteMiddleware', + + 'wagtail.wagtailredirects.middleware.RedirectMiddleware', + ) + +Wagtail requires several common Django middleware modules to work and cover basic security. Wagtail provides its own middleware to cover these tasks: + +``SiteMiddleware`` + Wagtail routes pre-defined hosts to pages within the Wagtail tree using this middleware. For configuring sites, see :ref:`wagtail_site_admin`. + +``RedirectMiddleware`` + Wagtail provides a simple interface for adding arbitrary redirects to your site and this module makes it happen. + + +Apps (settings.py) +~~~~~~~~~~~~~~~~~~ + +.. code-block:: python + + INSTALLED_APPS = ( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'south', + 'compressor', + 'taggit', + 'modelcluster', + 'django.contrib.admin', + + 'wagtail.wagtailcore', + 'wagtail.wagtailadmin', + 'wagtail.wagtaildocs', + 'wagtail.wagtailsnippets', + 'wagtail.wagtailusers', + 'wagtail.wagtailimages', + 'wagtail.wagtailembeds', + 'wagtail.wagtailsearch', + 'wagtail.wagtailredirects', + 'wagtail.wagtailforms', + + 'myapp', # your own app + ) + +Wagtail requires several Django app modules, third-party apps, and defines several apps of its own. Wagtail was built to be modular, so many Wagtail apps can be omitted to suit your needs. Your own app (here ``myapp``) is where you define your models, templates, static assets, template tags, and other custom functionality for your site. + + +Third-Party Apps +---------------- + +``south`` + Used for database migrations. See `South Documentation`_. + +.. _South Documentation: http://south.readthedocs.org/en/latest/ + +``compressor`` + Static asset combiner and minifier for Django. Compressor also enables for the use of preprocessors. See `Compressor Documentation`_. + +.. _Compressor Documentation: http://django-compressor.readthedocs.org/en/latest/ + +``taggit`` + Tagging framework for Django. This is used internally within Wagtail for image and document tagging and is available for your own models as well. See :ref:`tagging` for a Wagtail model recipe or the `Taggit Documentation`_. + +.. _Taggit Documentation: http://django-taggit.readthedocs.org/en/latest/index.html + +``modelcluster`` + Extension of Django ForeignKey relation functionality, which is used in Wagtail pages for on-the-fly related object creation. For more information, see :ref:`inline_panels` or `the django-modelcluster github project page`_. + +.. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster + +``django.contrib.admin`` + The Django admin module. While Wagtail will eventually provide a sites-editing interface, the Django admin is included for now to provide that functionality. + + +Wagtail Apps +------------ + +``wagtailcore`` + The core functionality of Wagtail, such as the ``Page`` class, the Wagtail tree, and model fields. + +``wagtailadmin`` + The administration interface for Wagtail, including page edit handlers. + +``wagtaildocs`` + The Wagtail document content type. + +``wagtailsnippets`` + Editing interface for non-Page models and objects. See :ref:`Snippets`. + +``wagtailusers`` + User editing interface. + +``wagtailimages`` + The Wagtail image content type. + +``wagtailembeds`` + Module governing oEmbed and Embedly content in Wagtail rich text fields. See :ref:`inserting_videos`. + +``wagtailsearch`` + Search framework for Page content. See :ref:`search`. + +``wagtailredirects`` + Admin interface for creating arbitrary redirects on your site. See :ref:`redirects`. + +``wagtailforms`` + Models for creating forms on your pages and viewing submissions. See :ref:`form_builder`. + + +Settings Variables (settings.py) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Authentication +-------------- + +.. code-block:: python + + LOGIN_URL = 'wagtailadmin_login' + LOGIN_REDIRECT_URL = 'wagtailadmin_home' + +These settings variables set the Django authentication system to redirect to the Wagtail admin login. If you plan to use the Django authentication module to log in non-privileged users, you should set these variables to your own login views. See `Django User Authentication`_. + +.. _Django User Authentication: https://docs.djangoproject.com/en/dev/topics/auth/ + + +Site Name +--------- + +.. code-block:: python + + WAGTAIL_SITE_NAME = 'Stark Industries Skunkworks' + +This is the human-readable name of your Wagtail install which welcomes users upon login to the Wagtail admin. + + +Search +------ + +.. code-block:: python + + # Override the search results template for wagtailsearch + WAGTAILSEARCH_RESULTS_TEMPLATE = 'myapp/search_results.html' + WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX = 'myapp/includes/search_listing.html' + + # Replace the search backend + WAGTAILSEARCH_BACKENDS = { + 'default': { + 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch', + 'INDEX': 'myapp' + } + } + +The search settings customize the search results templates as well as choosing a custom backend for search. For a full explanation, see :ref:`search`. + + +Embeds +------ + +Wagtail uses the oEmbed standard with a large but not comprehensive number of "providers" (youtube, vimeo, etc.). You can also use a different embed backend by providing an Embedly key or replacing the embed backend by writing your own embed finder function. + +.. code-block:: python + + WAGTAILEMBEDS_EMBED_FINDER = 'myapp.embeds.my_embed_finder_function' + +Use a custom embed finder function, which takes a URL and returns a dict with metadata and embeddable HTML. Refer to the ``wagtail.wagtailemebds.embeds`` module source for more information and examples. + +.. code-block:: python + + # not a working key, get your own! + EMBEDLY_KEY = '253e433d59dc4d2xa266e9e0de0cb830' + +Providing an API key for the Embedly service will use that as a embed backend, with a more extensive list of providers, as well as analytics and other features. For more information, see `Embedly`_. + +.. _Embedly: http://embed.ly/ + +To use Embedly, you must also install their python module: + +.. code-block:: bash + + $ pip install embedly + + +Images +------ + +.. code-block:: python + + WAGTAILIMAGES_IMAGE_MODEL = 'myapp.MyImage' + +This setting lets you provide your own image model for use in Wagtail, which might extend the built-in ``AbstractImage`` class or replace it entirely. + + +Email Notifications +------------------- + +.. code-block:: python + + WAGTAILADMIN_NOTIFICATION_FROM_EMAIL = 'wagtail@myhost.io' + +Wagtail sends email notifications when content is submitted for moderation, and when the content is accepted or rejected. This setting lets you pick which email address these automatic notifications will come from. If omitted, Django will fall back to using the ``DEFAULT_FROM_EMAIL`` variable if set, and ``webmaster@localhost`` if not. + + +Other Django Settings Used by Wagtail +------------------------------------- + +.. code-block:: python + + ALLOWED_HOSTS + APPEND_SLASH + AUTH_USER_MODEL + BASE_URL + CACHES + DEFAULT_FROM_EMAIL + INSTALLED_APPS + MEDIA_ROOT + SESSION_COOKIE_DOMAIN + SESSION_COOKIE_NAME + SESSION_COOKIE_PATH + STATIC_URL + TEMPLATE_CONTEXT_PROCESSORS + USE_I18N + +For information on what these settings do, see `Django Settings`_. + +.. _Django Settings: https://docs.djangoproject.com/en/dev/ref/settings/ + + +Search Signal Handlers +---------------------- + +.. code-block:: python + + from wagtail.wagtailsearch import register_signal_handlers as wagtailsearch_register_signal_handlers + + wagtailsearch_register_signal_handlers() + +This loads Wagtail's search signal handlers, which need to be loaded very early in the Django life cycle. While not technically a urlconf, this is a convenient place to load them. Calling this function registers signal handlers to watch for when indexed models get saved or deleted. This allows wagtailsearch to update ElasticSearch automatically. + + +URL Patterns +------------ + +.. code-block:: python + + from django.contrib import admin + + from wagtail.wagtailcore import urls as wagtail_urls + from wagtail.wagtailadmin import urls as wagtailadmin_urls + from wagtail.wagtaildocs import urls as wagtaildocs_urls + from wagtail.wagtailsearch.urls import frontend as wagtailsearch_frontend_urls + + admin.autodiscover() + + urlpatterns = patterns('', + url(r'^django-admin/', include(admin.site.urls)), + + url(r'^admin/', include(wagtailadmin_urls)), + url(r'^search/', include(wagtailsearch_frontend_urls)), + url(r'^documents/', include(wagtaildocs_urls)), + + # Optional urlconf for including your own vanilla Django urls/views + url(r'', include('myapp.urls')), + + # For anything not caught by a more specific rule above, hand over to + # Wagtail's serving mechanism + url(r'', include(wagtail_urls)), + ) + +This block of code for your project's ``urls.py`` does a few things: + +* Load the vanilla Django admin interface to ``/django-admin/`` +* Load the Wagtail admin and its various apps +* Dispatch any vanilla Django apps you're using other than Wagtail which require their own urlconfs (this is optional, since Wagtail might be all you need) +* Lets Wagtail handle any further URL dispatching. + +That's not everything you might want to include in your project's urlconf, but it's what's necessary for Wagtail to flourish. + + +.. _complete_example_config: + +Ready to Use Example Config Files +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +These two files should reside in your project directory (``myproject/myproject/``). + + +settings.py +----------- + +.. code-block:: python + + import os + + PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..') + + DEBUG = True + TEMPLATE_DEBUG = DEBUG + + ADMINS = ( + # ('Your Name', 'your_email@example.com'), + ) + + MANAGERS = ADMINS + + # Default to dummy email backend. Configure dev/production/local backend + # as per https://docs.djangoproject.com/en/dev/topics/email/#email-backends + EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend' + + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'myprojectdb', + 'USER': 'postgres', + 'PASSWORD': '', + 'HOST': '', # Set to empty string for localhost. + 'PORT': '', # Set to empty string for default. + 'CONN_MAX_AGE': 600, # number of seconds database connections should persist for + } + } + + # Hosts/domain names that are valid for this site; required if DEBUG is False + # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts + ALLOWED_HOSTS = [] + + # Local time zone for this installation. Choices can be found here: + # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name + # although not all choices may be available on all operating systems. + # On Unix systems, a value of None will cause Django to use the same + # timezone as the operating system. + # If running in a Windows environment this must be set to the same as your + # system time zone. + TIME_ZONE = 'Europe/London' + + # Language code for this installation. All choices can be found here: + # http://www.i18nguy.com/unicode/language-identifiers.html + LANGUAGE_CODE = 'en-gb' + + SITE_ID = 1 + + # If you set this to False, Django will make some optimizations so as not + # to load the internationalization machinery. + USE_I18N = True + + # If you set this to False, Django will not format dates, numbers and + # calendars according to the current locale. + # Note that with this set to True, Wagtail will fall back on using numeric dates + # in date fields, as opposed to 'friendly' dates like "24 Sep 2013", because + # Python's strptime doesn't support localised month names: https://code.djangoproject.com/ticket/13339 + USE_L10N = False + + # If you set this to False, Django will not use timezone-aware datetimes. + USE_TZ = True + + # Absolute filesystem path to the directory that will hold user-uploaded files. + # Example: "/home/media/media.lawrence.com/media/" + MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') + + # URL that handles the media served from MEDIA_ROOT. Make sure to use a + # trailing slash. + # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" + MEDIA_URL = '/media/' + + # Absolute path to the directory static files should be collected to. + # Don't put anything in this directory yourself; store your static files + # in apps' "static/" subdirectories and in STATICFILES_DIRS. + # Example: "/home/media/media.lawrence.com/static/" + STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') + + # URL prefix for static files. + # Example: "http://media.lawrence.com/static/" + STATIC_URL = '/static/' + + # List of finder classes that know how to find static files in + # various locations. + STATICFILES_FINDERS = ( + 'django.contrib.staticfiles.finders.FileSystemFinder', + 'django.contrib.staticfiles.finders.AppDirectoriesFinder', + 'compressor.finders.CompressorFinder', + ) + + # Make this unique, and don't share it with anybody. + SECRET_KEY = 'change-me' + + # List of callables that know how to import templates from various sources. + TEMPLATE_LOADERS = ( + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ) + + MIDDLEWARE_CLASSES = ( + 'django.middleware.common.CommonMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + + 'wagtail.wagtailcore.middleware.SiteMiddleware', + + 'wagtail.wagtailredirects.middleware.RedirectMiddleware', + ) + + from django.conf import global_settings + TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ( + 'django.core.context_processors.request', + ) + + ROOT_URLCONF = 'myproject.urls' + + # Python dotted path to the WSGI application used by Django's runserver. + WSGI_APPLICATION = 'wagtaildemo.wsgi.application' + + INSTALLED_APPS = ( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'south', + 'compressor', + 'taggit', + 'modelcluster', + 'django.contrib.admin', + + 'wagtail.wagtailcore', + 'wagtail.wagtailadmin', + 'wagtail.wagtaildocs', + 'wagtail.wagtailsnippets', + 'wagtail.wagtailusers', + 'wagtail.wagtailimages', + 'wagtail.wagtailembeds', + 'wagtail.wagtailsearch', + 'wagtail.wagtailredirects', + 'wagtail.wagtailforms', + + 'myapp', + ) + + EMAIL_SUBJECT_PREFIX = '[Wagtail] ' + + INTERNAL_IPS = ('127.0.0.1', '10.0.2.2') + + # django-compressor settings + COMPRESS_PRECOMPILERS = ( + ('text/x-scss', 'django_libsass.SassCompiler'), + ) + + # Auth settings + LOGIN_URL = 'wagtailadmin_login' + LOGIN_REDIRECT_URL = 'wagtailadmin_home' + + # A sample logging configuration. The only tangible logging + # performed by this configuration is to send an email to + # the site admins on every HTTP 500 error when DEBUG=False. + # See http://docs.djangoproject.com/en/dev/topics/logging for + # more details on how to customize your logging configuration. + LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse' + } + }, + 'handlers': { + 'mail_admins': { + 'level': 'ERROR', + 'filters': ['require_debug_false'], + 'class': 'django.utils.log.AdminEmailHandler' + } + }, + 'loggers': { + 'django.request': { + 'handlers': ['mail_admins'], + 'level': 'ERROR', + 'propagate': True, + }, + } + } + + + # WAGTAIL SETTINGS + + # This is the human-readable name of your Wagtail install + # which welcomes users upon login to the Wagtail admin. + WAGTAIL_SITE_NAME = 'My Project' + + # Override the search results template for wagtailsearch + # WAGTAILSEARCH_RESULTS_TEMPLATE = 'myapp/search_results.html' + # WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX = 'myapp/includes/search_listing.html' + + # Replace the search backend + #WAGTAILSEARCH_BACKENDS = { + # 'default': { + # 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch', + # 'INDEX': 'myapp' + # } + #} + + # Wagtail email notifications from address + # WAGTAILADMIN_NOTIFICATION_FROM_EMAIL = 'wagtail@myhost.io' + + # If you want to use Embedly for embeds, supply a key + # (this key doesn't work, get your own!) + # EMBEDLY_KEY = '253e433d59dc4d2xa266e9e0de0cb830' + + +urls.py +------- + +.. code-block:: python + + from django.conf.urls import patterns, include, url + from django.conf.urls.static import static + from django.views.generic.base import RedirectView + from django.contrib import admin + from django.conf import settings + import os.path + + from wagtail.wagtailcore import urls as wagtail_urls + from wagtail.wagtailadmin import urls as wagtailadmin_urls + from wagtail.wagtaildocs import urls as wagtaildocs_urls + from wagtail.wagtailsearch.urls import frontend as wagtailsearch_frontend_urls + + admin.autodiscover() + + + # Signal handlers + from wagtail.wagtailsearch import register_signal_handlers as wagtailsearch_register_signal_handlers + wagtailsearch_register_signal_handlers() + + + urlpatterns = patterns('', + url(r'^django-admin/', include(admin.site.urls)), + + url(r'^admin/', include(wagtailadmin_urls)), + url(r'^search/', include(wagtailsearch_frontend_urls)), + url(r'^documents/', include(wagtaildocs_urls)), + + # For anything not caught by a more specific rule above, hand over to + # Wagtail's serving mechanism + url(r'', include(wagtail_urls)), + ) + + + if settings.DEBUG: + from django.contrib.staticfiles.urls import staticfiles_urlpatterns + + urlpatterns += staticfiles_urlpatterns() # tell gunicorn where static files are in dev mode + urlpatterns += static(settings.MEDIA_URL + 'images/', document_root=os.path.join(settings.MEDIA_ROOT, 'images')) + urlpatterns += patterns('', + (r'^favicon\.ico$', RedirectView.as_view(url=settings.STATIC_URL + 'myapp/images/favicon.ico')) + ) + + + diff --git a/docs/wagtail_search.rst b/docs/wagtail_search.rst index 3d8ea26b2..6a77b06f3 100644 --- a/docs/wagtail_search.rst +++ b/docs/wagtail_search.rst @@ -1,3 +1,6 @@ + +.. _search: + Search ====== @@ -220,17 +223,14 @@ The default DB search backend uses Django's ``__icontains`` filter. Elasticsearch Backend ````````````````````` -Prerequisites are the Elasticsearch service itself and, via pip, the `elasticutils`_ and `pyelasticsearch`_ packages: +Prerequisites are the Elasticsearch service itself and, via pip, the `elasticsearch-py`_ package: .. code-block:: guess - pip install elasticutils==0.8.2 pyelasticsearch + pip install elasticsearch .. note:: - ElasticUtils 0.9+ is not supported. - -.. note:: - The dependency on elasticutils and pyelasticsearch is scheduled to be replaced by a dependency on `elasticsearch-py`_. + If you are using Elasticsearch < 1.0, install elasticsearch-py version 0.4.5: ```pip install elasticsearch==0.4.5``` The backend is configured in settings: @@ -246,7 +246,7 @@ The backend is configured in settings: } } -Other than ``BACKEND`` the keys are optional and default to the values shown. ``FORCE_NEW`` is used by elasticutils. In addition, any other keys are passed directly to the Elasticsearch constructor as case-sensitive keyword arguments (e.g. ``'max_retries': 1``). +Other than ``BACKEND`` the keys are optional and default to the values shown. ``FORCE_NEW`` is used by elasticsearch-py. In addition, any other keys are passed directly to the Elasticsearch constructor as case-sensitive keyword arguments (e.g. ``'max_retries': 1``). If you prefer not to run an Elasticsearch server in development or production, there are many hosted services available, including `Searchly`_, who offer a free account suitable for testing and development. To use Searchly: @@ -256,8 +256,6 @@ If you prefer not to run an Elasticsearch server in development or production, t - Configure ``URLS`` and ``INDEX`` in the Elasticsearch entry in ``WAGTAILSEARCH_BACKENDS`` - Run ``./manage.py update_index`` -.. _elasticutils: http://elasticutils.readthedocs.org -.. _pyelasticsearch: http://pyelasticsearch.readthedocs.org .. _elasticsearch-py: http://elasticsearch-py.readthedocs.org .. _Searchly: http://www.searchly.com/ .. _dashboard.searchly.com/users/sign\_up: https://dashboard.searchly.com/users/sign_up diff --git a/runtests.py b/runtests.py index 00fa821e6..c911377f1 100755 --- a/runtests.py +++ b/runtests.py @@ -13,7 +13,7 @@ MEDIA_ROOT = os.path.join(WAGTAIL_ROOT, 'test-media') if not settings.configured: try: - import elasticutils + import elasticsearch has_elasticsearch = True except ImportError: has_elasticsearch = False diff --git a/setup.py b/setup.py index 7b26817cd..2d2939a9e 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +import sys + + try: from setuptools import setup, find_packages except ImportError: @@ -16,6 +19,31 @@ except ImportError: pass +PY3 = sys.version_info[0] == 3 + + +install_requires = [ + "Django>=1.6.2,<1.7", + "South>=0.8.4", + "django-compressor>=1.3", + "django-libsass>=0.1", + "django-modelcluster>=0.1", + "django-taggit==0.11.2", + "django-treebeard==2.0", + "Pillow>=2.3.0", + "beautifulsoup4>=4.3.2", + "lxml>=3.3.0", + "Unidecode>=0.04.14", + "six==1.7.3", +] + + +if not PY3: + install_requires += [ + "unicodecsv>=0.9.4" + ] + + setup( name='wagtail', version='0.3.1', @@ -37,23 +65,11 @@ setup( 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', 'Framework :: Django', 'Topic :: Internet :: WWW/HTTP :: Site Management', ], - install_requires=[ - "Django>=1.6.2,<1.7", - "South>=0.8.4", - "django-compressor>=1.3", - "django-libsass>=0.1", - "django-modelcluster>=0.1", - "django-taggit==0.11.2", - "django-treebeard==2.0", - "Pillow>=2.3.0", - "beautifulsoup4>=4.3.2", - "lxml>=3.3.0", - 'unicodecsv>=0.9.4', - 'Unidecode>=0.04.14', - "BeautifulSoup==3.2.1", # django-compressor gets confused if we have lxml but not BS3 installed - ], + install_requires=install_requires, zip_safe=False, ) diff --git a/tox.ini b/tox.ini index 11b8bb3a2..e1f888d6c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,15 +1,17 @@ [deps] dj16= Django>=1.6,<1.7 - pyelasticsearch==0.6.1 - elasticutils==0.8.2 + elasticsearch==1.0.0 + mock==1.0.1 [tox] envlist = py26-dj16-postgres, py26-dj16-sqlite, py27-dj16-postgres, - py27-dj16-sqlite + py27-dj16-sqlite, + py33-dj16-postgres, + py34-dj16-postgres # mysql not currently supported # (wagtail.wagtailimages.tests.TestImageEditView currently fails with a @@ -17,6 +19,11 @@ envlist = # py26-dj16-mysql # py27-dj16-mysql +# South fails with sqlite on python3, because it tries to use DryRunMigrator which uses iteritems +# py33-dj16-sqlite, +# py34-dj16-sqlite + + [testenv] commands=./runtests.py @@ -67,3 +74,33 @@ deps = setenv = DATABASE_ENGINE=django.db.backends.mysql DATABASE_USER=wagtail + +[testenv:py33-dj16-postgres] +basepython=python3.3 +deps = + {[deps]dj16} + psycopg2==2.5.2 +setenv = + DATABASE_ENGINE=django.db.backends.postgresql_psycopg2 + +[testenv:py33-dj16-sqlite] +basepython=python3.3 +deps = + {[deps]dj16} +setenv = + DATABASE_ENGINE=django.db.backends.sqlite3 + +[testenv:py34-dj16-postgres] +basepython=python3.4 +deps = + {[deps]dj16} + psycopg2==2.5.2 +setenv = + DATABASE_ENGINE=django.db.backends.postgresql_psycopg2 + +[testenv:py34-dj16-sqlite] +basepython=python3.4 +deps = + {[deps]dj16} +setenv = + DATABASE_ENGINE=django.db.backends.sqlite3 diff --git a/wagtail/contrib/wagtailstyleguide/templates/wagtailstyleguide/base.html b/wagtail/contrib/wagtailstyleguide/templates/wagtailstyleguide/base.html index e7bd88606..8506e9075 100644 --- a/wagtail/contrib/wagtailstyleguide/templates/wagtailstyleguide/base.html +++ b/wagtail/contrib/wagtailstyleguide/templates/wagtailstyleguide/base.html @@ -42,9 +42,10 @@
  • color-teal
  • color-teal-darker
  • color-teal-dark
  • -
  • color-red
  • -
  • color-orange
  • -
  • color-green
  • + +
      +
    • color-salmon
    • +
    • color-salmon-light
    • color-grey-1
    • @@ -54,6 +55,12 @@
    • color-grey-4
    • color-grey-5
    +
      +
    • color-red
    • +
    • color-orange
    • +
    • color-green
    • +
    +
    @@ -149,29 +156,37 @@

    Buttons

    -
    button
    +
    button -
    button-secondary
    + button-secondary -
    yes
    + yes -
    no / serious
    + no / serious -
    bicolor with icon
    + bicolor with icon -
    button-small
    + button-small -
    bicolo button-small
    + bicolo button-small -
    mixed 1
    + mixed 1 -
    mixed 2
    + mixed 2 + +
    button on a div

    Buttons must have interaction possible (i.e be an input or button element) to get a suitable hover cursor

    + + + + + +
    diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/login.html b/wagtail/wagtailadmin/templates/wagtailadmin/login.html index 22682500f..52448ffe5 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/login.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/login.html @@ -28,17 +28,17 @@
    • -
      +
      {{ form.username.label_tag }} -
      +
      {{ form.username }}
    • -
      +
      {{ form.password.label_tag }} -
      +
      {{ form.password }}
      diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/approved.html b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/approved.html index 169f4ba7c..0af5ec841 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/approved.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/approved.html @@ -1,4 +1,4 @@ -{% load i18n %}{% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been approved{% endblocktrans %} +{% extends 'wagtailadmin/notifications/base_notification.html' %}{% block notification %}{% load i18n %}{% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been approved{% endblocktrans %} {% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been approved.{% endblocktrans %} -{% trans "You can view the page here:" %} {{ revision.page.full_url }} \ No newline at end of file +{% trans "You can view the page here:" %} {{ revision.page.full_url }}{% endblock %} diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/base_notification.html b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/base_notification.html new file mode 100644 index 000000000..948fe186e --- /dev/null +++ b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/base_notification.html @@ -0,0 +1,3 @@ +{% load i18n %}{% block notification %}{% endblock %} + +{% trans "Edit your notification preferences here:" %} {{ settings.BASE_URL }}{% url 'wagtailadmin_account_notification_preferences' %} diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/rejected.html b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/rejected.html index 34a49fae7..f4be24976 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/rejected.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/rejected.html @@ -1,4 +1,4 @@ -{% load i18n %}{% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been rejected{% endblocktrans %} +{% extends 'wagtailadmin/notifications/base_notification.html' %}{% block notification %}{% load i18n %}{% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been rejected{% endblocktrans %} {% blocktrans with title=revision.page.title|safe %}The page "{{ title }}" has been rejected.{% endblocktrans %} -{% trans "You can edit the page here:"%} {{ settings.BASE_URL }}{% url 'wagtailadmin_pages_edit' revision.page.id %} \ No newline at end of file +{% trans "You can edit the page here:"%} {{ settings.BASE_URL }}{% url 'wagtailadmin_pages_edit' revision.page.id %}{% endblock %} diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/submitted.html b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/submitted.html index f3a9115e0..e048d73a5 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/notifications/submitted.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/notifications/submitted.html @@ -1,5 +1,5 @@ -{% load i18n %}{% blocktrans with page=revision.page|safe %}The page "{{ page }}" has been submitted for moderation{% endblocktrans %} +{% extends 'wagtailadmin/notifications/base_notification.html' %}{% block notification %}{% load i18n %}{% blocktrans with page=revision.page|safe %}The page "{{ page }}" has been submitted for moderation{% endblocktrans %} {% blocktrans with page=revision.page|safe %}The page "{{ page }}" has been submitted for moderation.{% endblocktrans %} {% trans "You can preview the page here:" %} {{ settings.BASE_URL }}{% url 'wagtailadmin_pages_preview_for_moderation' revision.id %} -{% trans "You can edit the page here:" %} {{ settings.BASE_URL }}{% url 'wagtailadmin_pages_edit' revision.page.id %} \ No newline at end of file +{% trans "You can edit the page here:" %} {{ settings.BASE_URL }}{% url 'wagtailadmin_pages_edit' revision.page.id %}{% endblock %} diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html index 3ddb8c1f3..17bcbd8bf 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html @@ -16,11 +16,9 @@ - - diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_create.html b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_create.html index 4f60050be..1809a6510 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_create.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_create.html @@ -1,4 +1,4 @@ - diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_edit.html b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_edit.html index a5cd5d24a..d24b9b0e9 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_edit.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_preview_button_on_edit.html @@ -1,4 +1,4 @@ - diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/pages/edit.html b/wagtail/wagtailadmin/templates/wagtailadmin/pages/edit.html index 1e1f7e9b3..901ee0ac1 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/pages/edit.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/pages/edit.html @@ -70,7 +70,7 @@ {% blocktrans with last_mod=page.get_latest_revision.created_at %}Last modified: {{ last_mod }}{% endblocktrans %} {% if page.get_latest_revision.user %} {% blocktrans with modified_by=page.get_latest_revision.user.get_full_name|default:page.get_latest_revision.user.username %}by {{ modified_by }}{% endblocktrans %} - {% if request.user.email %} + {% if page.get_latest_revision.user.email %} {% endif %} {% endif %} diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/shared/breadcrumb.html b/wagtail/wagtailadmin/templates/wagtailadmin/shared/breadcrumb.html index cb8b65f28..50bfb186e 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/shared/breadcrumb.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/shared/breadcrumb.html @@ -3,12 +3,12 @@ diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/shared/field.html b/wagtail/wagtailadmin/templates/wagtailadmin/shared/field.html new file mode 100644 index 000000000..9113e1b6b --- /dev/null +++ b/wagtail/wagtailadmin/templates/wagtailadmin/shared/field.html @@ -0,0 +1,25 @@ +{% load wagtailadmin_tags %} +
      + {{ field.label_tag }} +
      +
      + {% block form_field %} + {{ field }} + {% endblock %} + + {# This span only used on rare occasions by certain types of input #} + +
      + {% if field.help_text %} +

      {{ field.help_text }}

      + {% endif %} + + {% if field.errors %} +

      + {% for error in field.errors %} + {{ error|escape }} + {% endfor %} +

      + {% endif %} +
      +
      \ No newline at end of file diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/shared/field_as_li.html b/wagtail/wagtailadmin/templates/wagtailadmin/shared/field_as_li.html index a1f51174a..d10e9ba27 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/shared/field_as_li.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/shared/field_as_li.html @@ -1,25 +1,4 @@ {% load wagtailadmin_tags %} -
    • -
      - {{ field.label_tag }} -
      -
      - {% block form_field %} - {{ field }} - {% endblock %} - -
      - {% if field.help_text %} -

      {{ field.help_text }}

      - {% endif %} - - {% if field.errors %} -

      - {% for error in field.errors %} - {{ error|escape }} - {% endfor %} -

      - {% endif %} -
      -
      +
    • + {% include "wagtailadmin/shared/field.html" %}
    • \ No newline at end of file diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/shared/header.html b/wagtail/wagtailadmin/templates/wagtailadmin/shared/header.html index 59340d2d8..be391989d 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/shared/header.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/shared/header.html @@ -8,7 +8,7 @@
        {% for field in search_form %} - {% include "wagtailadmin/shared/field_as_li.html" with field=field input_classes="field-small iconfield icon-search" %} + {% include "wagtailadmin/shared/field_as_li.html" with field=field field_classes="field-small iconfield" input_classes="icon-search" %} {% endfor %}
      diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/skeleton.html b/wagtail/wagtailadmin/templates/wagtailadmin/skeleton.html index ac5038056..e24c1a588 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/skeleton.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/skeleton.html @@ -1,10 +1,7 @@ {% load compress %} - - - - - + + diff --git a/wagtail/wagtailadmin/templatetags/gravatar.py b/wagtail/wagtailadmin/templatetags/gravatar.py index 5c69f4fd2..c831e6cb5 100644 --- a/wagtail/wagtailadmin/templatetags/gravatar.py +++ b/wagtail/wagtailadmin/templatetags/gravatar.py @@ -8,9 +8,11 @@ ### ### just make sure to update the "default" image path below -import urllib import hashlib +from six import b +from six.moves.urllib.parse import urlencode + from django import template register = template.Library() @@ -30,8 +32,8 @@ class GravatarUrlNode(template.Node): default = "blank" size = int(self.size) * 2 # requested at retina size by default and scaled down at point of use with css - gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?" - gravatar_url += urllib.urlencode({'s': str(size), 'd': default}) + gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(b(email.lower())).hexdigest() + "?" + gravatar_url += urlencode({'s': str(size), 'd': default}) return gravatar_url diff --git a/wagtail/wagtailadmin/tests/test_account_management.py b/wagtail/wagtailadmin/tests/test_account_management.py index 566d68723..7128f8885 100644 --- a/wagtail/wagtailadmin/tests/test_account_management.py +++ b/wagtail/wagtailadmin/tests/test_account_management.py @@ -1,10 +1,12 @@ from django.test import TestCase -from wagtail.tests.utils import unittest, WagtailTestUtils from django.core.urlresolvers import reverse -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group, Permission from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.core import mail +from wagtail.tests.utils import unittest, WagtailTestUtils +from wagtail.wagtailusers.models import UserProfile + class TestAuthentication(TestCase, WagtailTestUtils): """ @@ -177,6 +179,97 @@ class TestAccountSection(TestCase, WagtailTestUtils): # Check that the password was not changed self.assertTrue(User.objects.get(username='test').check_password('password')) + def test_notification_preferences_view(self): + """ + This tests that the notification preferences view responds with the + notification preferences page + """ + # Get notification preferences page + response = self.client.get(reverse('wagtailadmin_account_notification_preferences')) + + # Check that the user recieved a notification preferences page + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'wagtailadmin/account/notification_preferences.html') + + def test_notification_preferences_view_post(self): + """ + This posts to the notification preferences view and checks that the + user's profile is updated + """ + # Post new values to the notification preferences page + post_data = { + 'submitted_notifications': u'false', + 'approved_notifications': u'false', + 'rejected_notifications': u'true', + } + response = self.client.post(reverse('wagtailadmin_account_notification_preferences'), post_data) + + # Check that the user was redirected to the account page + self.assertRedirects(response, reverse('wagtailadmin_account')) + + profile = UserProfile.get_for_user(User.objects.get(username='test')) + + # Check that the notification preferences are as submitted + self.assertFalse(profile.submitted_notifications) + self.assertFalse(profile.approved_notifications) + self.assertTrue(profile.rejected_notifications) + + +class TestAccountManagementForNonModerator(TestCase, WagtailTestUtils): + """ + Tests of reduced-functionality for editors + """ + def setUp(self): + # Create a non-moderator user + self.submitter = User.objects.create_user('submitter', 'submitter@example.com', 'password') + self.submitter.groups.add(Group.objects.get(name='Editors')) + + self.client.login(username=self.submitter.username, password='password') + + def test_notification_preferences_form_is_reduced_for_non_moderators(self): + """ + This tests that a user without publish permissions is not shown the + notification preference for 'submitted' items + """ + response = self.client.get(reverse('wagtailadmin_account_notification_preferences')) + self.assertIn('approved_notifications', response.context['form'].fields.keys()) + self.assertIn('rejected_notifications', response.context['form'].fields.keys()) + self.assertNotIn('submitted_notifications', response.context['form'].fields.keys()) + + +class TestAccountManagementForAdminOnlyUser(TestCase, WagtailTestUtils): + """ + Tests for users with no edit/publish permissions at all + """ + def setUp(self): + # Create a non-moderator user + admin_only_group = Group.objects.create(name='Admin Only') + admin_only_group.permissions.add(Permission.objects.get(codename='access_admin')) + + self.admin_only_user = User.objects.create_user('admin_only_user', 'admin_only_user@example.com', 'password') + self.admin_only_user.groups.add(admin_only_group) + + self.client.login(username=self.admin_only_user.username, password='password') + + def test_notification_preferences_view_redirects_for_admin_only_users(self): + """ + Test that the user is not shown the notification preferences view but instead + redirected to the account page + """ + response = self.client.get(reverse('wagtailadmin_account_notification_preferences')) + self.assertRedirects(response, reverse('wagtailadmin_account')) + + def test_notification_preferences_link_not_shown_for_admin_only_users(self): + """ + Test that the user is not even shown the link to the notification + preferences view + """ + response = self.client.get(reverse('wagtailadmin_account')) + self.assertEqual(response.context['show_notification_preferences'], False) + self.assertNotContains(response, reverse('wagtailadmin_account_notification_preferences')) + # safety check that checking for absence/presence of urls works + self.assertContains(response, reverse('wagtailadmin_home')) + class TestPasswordReset(TestCase, WagtailTestUtils): """ diff --git a/wagtail/wagtailadmin/tests/test_pages_views.py b/wagtail/wagtailadmin/tests/test_pages_views.py index 6c1f490dd..a550b288e 100644 --- a/wagtail/wagtailadmin/tests/test_pages_views.py +++ b/wagtail/wagtailadmin/tests/test_pages_views.py @@ -1,11 +1,16 @@ +from datetime import timedelta + from django.test import TestCase -from wagtail.tests.models import SimplePage, EventPage, StandardIndex, StandardChild, BusinessIndex, BusinessChild -from wagtail.tests.utils import unittest, WagtailTestUtils -from wagtail.wagtailcore.models import Page, PageRevision from django.core.urlresolvers import reverse from django.contrib.auth.models import User, Permission from django.core import mail from django.core.paginator import Paginator +from django.utils import timezone + +from wagtail.tests.models import SimplePage, EventPage, StandardIndex, StandardChild, BusinessIndex, BusinessChild, BusinessSubIndex +from wagtail.tests.utils import unittest, WagtailTestUtils +from wagtail.wagtailcore.models import Page, PageRevision +from wagtail.wagtailusers.models import UserProfile class TestPageExplorer(TestCase, WagtailTestUtils): @@ -167,6 +172,61 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertIsInstance(page, SimplePage) self.assertFalse(page.live) + def test_create_simplepage_scheduled(self): + go_live_at = timezone.now() + timedelta(days=1) + expire_at = timezone.now() + timedelta(days=2) + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_at': str(go_live_at).split('.')[0], + 'expire_at': str(expire_at).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + # Find the page and check the scheduled times + page = Page.objects.get(path__startswith=self.root_page.path, slug='hello-world').specific + self.assertEquals(page.go_live_at.date(), go_live_at.date()) + self.assertEquals(page.expire_at.date(), expire_at.date()) + self.assertEquals(page.expired, False) + self.assertTrue(page.status_string, "draft") + + # No revisions with approved_go_live_at + self.assertFalse(PageRevision.objects.filter(page=page).exclude(approved_go_live_at__isnull=True).exists()) + + def test_create_simplepage_scheduled_go_live_before_expiry(self): + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_at': str(timezone.now() + timedelta(days=2)).split('.')[0], + 'expire_at': str(timezone.now() + timedelta(days=1)).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + self.assertEqual(response.status_code, 200) + + # Check that a form error was raised + self.assertFormError(response, 'form', 'go_live_at', "Go live date/time must be before expiry date/time") + self.assertFormError(response, 'form', 'expire_at', "Go live date/time must be before expiry date/time") + + def test_create_simplepage_scheduled_expire_in_the_past(self): + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'expire_at': str(timezone.now() + timedelta(days=-1)).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + self.assertEqual(response.status_code, 200) + + # Check that a form error was raised + self.assertFormError(response, 'form', 'expire_at', "Expiry date/time must be in the future") + def test_create_simplepage_post_publish(self): post_data = { 'title': "New page!", @@ -185,6 +245,34 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertIsInstance(page, SimplePage) self.assertTrue(page.live) + def test_create_simplepage_post_publish_scheduled(self): + go_live_at = timezone.now() + timedelta(days=1) + expire_at = timezone.now() + timedelta(days=2) + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_at': str(go_live_at).split('.')[0], + 'expire_at': str(expire_at).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + # Find the page and check it + page = Page.objects.get(path__startswith=self.root_page.path, slug='hello-world').specific + self.assertEquals(page.go_live_at.date(), go_live_at.date()) + self.assertEquals(page.expire_at.date(), expire_at.date()) + self.assertEquals(page.expired, False) + + # A revision with approved_go_live_at should exist now + self.assertTrue(PageRevision.objects.filter(page=page).exclude(approved_go_live_at__isnull=True).exists()) + # But Page won't be live + self.assertFalse(page.live) + self.assertTrue(page.status_string, "scheduled") + def test_create_simplepage_post_submit(self): # Create a moderator user for testing email moderator = User.objects.create_superuser('moderator', 'moderator@email.com', 'password') @@ -243,7 +331,6 @@ class TestPageCreation(TestCase, WagtailTestUtils): response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', 100000))) self.assertEqual(response.status_code, 404) - @unittest.expectedFailure # FIXME: Crashes! def test_create_nonpagetype(self): response = self.client.get(reverse('wagtailadmin_pages_create', args=('wagtailimages', 'image', self.root_page.id))) self.assertEqual(response.status_code, 404) @@ -325,6 +412,63 @@ class TestPageEdit(TestCase, WagtailTestUtils): child_page_new = SimplePage.objects.get(id=self.child_page.id) self.assertTrue(child_page_new.has_unpublished_changes) + def test_edit_post_scheduled(self): + go_live_at = timezone.now() + timedelta(days=1) + expire_at = timezone.now() + timedelta(days=2) + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_at': str(go_live_at).split('.')[0], + 'expire_at': str(expire_at).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + + # The page will still be live + self.assertTrue(child_page_new.live) + + # A revision with approved_go_live_at should not exist + self.assertFalse(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_at__isnull=True).exists()) + + # But a revision with go_live_at and expire_at in their content json *should* exist + self.assertTrue(PageRevision.objects.filter(page=child_page_new, content_json__contains=str(go_live_at.date())).exists()) + self.assertTrue(PageRevision.objects.filter(page=child_page_new, content_json__contains=str(expire_at.date())).exists()) + + def test_edit_scheduled_go_live_before_expiry(self): + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_at': str(timezone.now() + timedelta(days=2)).split('.')[0], + 'expire_at': str(timezone.now() + timedelta(days=1)).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + self.assertEqual(response.status_code, 200) + + # Check that a form error was raised + self.assertFormError(response, 'form', 'go_live_at', "Go live date/time must be before expiry date/time") + self.assertFormError(response, 'form', 'expire_at', "Go live date/time must be before expiry date/time") + + def test_edit_scheduled_expire_in_the_past(self): + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'expire_at': str(timezone.now() + timedelta(days=-1)).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + self.assertEqual(response.status_code, 200) + + # Check that a form error was raised + self.assertFormError(response, 'form', 'expire_at', "Expiry date/time must be in the future") + def test_page_edit_post_publish(self): # Tests publish from edit page post_data = { @@ -345,6 +489,77 @@ class TestPageEdit(TestCase, WagtailTestUtils): # The page shouldn't have "has_unpublished_changes" flag set self.assertFalse(child_page_new.has_unpublished_changes) + def test_edit_post_publish_scheduled(self): + go_live_at = timezone.now() + timedelta(days=1) + expire_at = timezone.now() + timedelta(days=2) + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_at': str(go_live_at).split('.')[0], + 'expire_at': str(expire_at).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + + # The page should not be live anymore + self.assertFalse(child_page_new.live) + + # Instead a revision with approved_go_live_at should now exist + self.assertTrue(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_at__isnull=True).exists()) + + def test_edit_post_publish_now_an_already_scheduled(self): + # First let's publish a page with a go_live_at in the future + go_live_at = timezone.now() + timedelta(days=1) + expire_at = timezone.now() + timedelta(days=2) + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_at': str(go_live_at).split('.')[0], + 'expire_at': str(expire_at).split('.')[0], + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + + # The page should not be live anymore + self.assertFalse(child_page_new.live) + + # Instead a revision with approved_go_live_at should now exist + self.assertTrue(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_at__isnull=True).exists()) + + # Now, let's edit it and publish it right now + go_live_at = timezone.now() + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_at': "", + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + + # The page should be live now + self.assertTrue(child_page_new.live) + + # And a revision with approved_go_live_at should not exist + self.assertFalse(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_at__isnull=True).exists()) + def test_page_edit_post_submit(self): # Create a moderator user for testing email moderator = User.objects.create_superuser('moderator', 'moderator@email.com', 'password') @@ -651,11 +866,6 @@ class TestApproveRejectModeration(TestCase, WagtailTestUtils): # Page must be live self.assertTrue(Page.objects.get(id=self.page.id).live) - # Submitter must recieve an approved email - self.assertEqual(len(mail.outbox), 1) - self.assertEqual(mail.outbox[0].to, ['submitter@email.com']) - self.assertEqual(mail.outbox[0].subject, 'The page "Hello world!" has been approved') - def test_approve_moderation_view_bad_revision_id(self): """ This tests that the approve moderation view handles invalid revision ids correctly @@ -705,11 +915,6 @@ class TestApproveRejectModeration(TestCase, WagtailTestUtils): # Revision must no longer be submitted for moderation self.assertFalse(PageRevision.objects.get(id=self.revision.id).submitted_for_moderation) - # Submitter must recieve a rejected email - self.assertEqual(len(mail.outbox), 1) - self.assertEqual(mail.outbox[0].to, ['submitter@email.com']) - self.assertEqual(mail.outbox[0].subject, 'The page "Hello world!" has been rejected') - def test_reject_moderation_view_bad_revision_id(self): """ This tests that the reject moderation view handles invalid revision ids correctly @@ -771,41 +976,220 @@ class TestSubpageBusinessRules(TestCase, WagtailTestUtils): # Find root page self.root_page = Page.objects.get(id=2) - # Add standard page + # Add standard page (allows subpages of any type) self.standard_index = StandardIndex() self.standard_index.title = "Standard Index" self.standard_index.slug = "standard-index" self.root_page.add_child(instance=self.standard_index) - # Add business page + # Add business page (allows BusinessChild and BusinessSubIndex as subpages) self.business_index = BusinessIndex() self.business_index.title = "Business Index" self.business_index.slug = "business-index" self.root_page.add_child(instance=self.business_index) - # Add business child + # Add business child (allows no subpages) self.business_child = BusinessChild() self.business_child.title = "Business Child" self.business_child.slug = "business-child" self.business_index.add_child(instance=self.business_child) + # Add business subindex (allows only BusinessChild as subpages) + self.business_subindex = BusinessSubIndex() + self.business_subindex.title = "Business Subindex" + self.business_subindex.slug = "business-subindex" + self.business_index.add_child(instance=self.business_subindex) + # Login self.login() def test_standard_subpage(self): - response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.standard_index.id, ))) + add_subpage_url = reverse('wagtailadmin_pages_add_subpage', args=(self.standard_index.id, )) + + # explorer should contain a link to 'add child page' + response = self.client.get(reverse('wagtailadmin_explore', args=(self.standard_index.id, ))) + self.assertEqual(response.status_code, 200) + self.assertContains(response, add_subpage_url) + + # add_subpage should give us the full set of page types to choose + response = self.client.get(add_subpage_url) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Standard Child') self.assertContains(response, 'Business Child') def test_business_subpage(self): - response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.business_index.id, ))) + add_subpage_url = reverse('wagtailadmin_pages_add_subpage', args=(self.business_index.id, )) + + # explorer should contain a link to 'add child page' + response = self.client.get(reverse('wagtailadmin_explore', args=(self.business_index.id, ))) + self.assertEqual(response.status_code, 200) + self.assertContains(response, add_subpage_url) + + # add_subpage should give us a cut-down set of page types to choose + response = self.client.get(add_subpage_url) self.assertEqual(response.status_code, 200) self.assertNotContains(response, 'Standard Child') self.assertContains(response, 'Business Child') def test_business_child_subpage(self): - response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.business_child.id, ))) + add_subpage_url = reverse('wagtailadmin_pages_add_subpage', args=(self.business_child.id, )) + + # explorer should not contain a link to 'add child page', as this page doesn't accept subpages + response = self.client.get(reverse('wagtailadmin_explore', args=(self.business_child.id, ))) self.assertEqual(response.status_code, 200) - self.assertNotContains(response, 'Standard Child') - self.assertEqual(0, len(response.context['page_types'])) + self.assertNotContains(response, add_subpage_url) + + # this also means that fetching add_subpage is blocked at the permission-check level + response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.business_child.id, ))) + self.assertEqual(response.status_code, 403) + + def test_cannot_add_invalid_subpage_type(self): + # cannot add SimplePage as a child of BusinessIndex, as SimplePage is not present in subpage_types + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.business_index.id))) + self.assertEqual(response.status_code, 403) + + # likewise for BusinessChild which has an empty subpage_types list + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.business_child.id))) + self.assertEqual(response.status_code, 403) + + # but we can add a BusinessChild to BusinessIndex + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'businesschild', self.business_index.id))) + self.assertEqual(response.status_code, 200) + + def test_not_prompted_for_page_type_when_only_one_choice(self): + response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.business_subindex.id, ))) + # BusinessChild is the only valid subpage type of BusinessSubIndex, so redirect straight there + self.assertRedirects(response, reverse('wagtailadmin_pages_create', args=('tests', 'businesschild', self.business_subindex.id))) + + +class TestNotificationPreferences(TestCase, WagtailTestUtils): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Login + self.user = self.login() + + # Create two moderator users for testing 'submitted' email + self.moderator = User.objects.create_superuser('moderator', 'moderator@email.com', 'password') + self.moderator2 = User.objects.create_superuser('moderator2', 'moderator2@email.com', 'password') + + # Create a submitter for testing 'rejected' and 'approved' emails + self.submitter = User.objects.create_user('submitter', 'submitter@email.com', 'password') + + # User profiles for moderator2 and the submitter + self.moderator2_profile = UserProfile.get_for_user(self.moderator2) + self.submitter_profile = UserProfile.get_for_user(self.submitter) + + # Create a page and submit it for moderation + self.child_page = SimplePage( + title="Hello world!", + slug='hello-world', + live=False, + ) + self.root_page.add_child(instance=self.child_page) + + # POST data to edit the page + self.post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-submit': "Submit", + } + + def submit(self): + return self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), self.post_data) + + def silent_submit(self): + """ + Sets up the child_page as needing moderation, without making a request + """ + self.child_page.save_revision(user=self.submitter, submitted_for_moderation=True) + self.revision = self.child_page.get_latest_revision() + + def approve(self): + return self.client.post(reverse('wagtailadmin_pages_approve_moderation', args=(self.revision.id, )), { + 'foo': "Must post something or the view won't see this as a POST request", + }) + + def reject(self): + return self.client.post(reverse('wagtailadmin_pages_reject_moderation', args=(self.revision.id, )), { + 'foo': "Must post something or the view won't see this as a POST request", + }) + + def test_vanilla_profile(self): + # Check that the vanilla profile has rejected notifications on + self.assertEqual(self.submitter_profile.rejected_notifications, True) + + # Check that the vanilla profile has approved notifications on + self.assertEqual(self.submitter_profile.approved_notifications, True) + + def test_submit_notifications_sent(self): + # Submit + self.submit() + + # Check that both the moderators got an email, and no others + self.assertEqual(len(mail.outbox), 1) + self.assertIn(self.moderator.email, mail.outbox[0].to) + self.assertIn(self.moderator2.email, mail.outbox[0].to) + self.assertEqual(len(mail.outbox[0].to), 2) + + def test_submit_notification_preferences_respected(self): + # moderator2 doesn't want emails + self.moderator2_profile.submitted_notifications = False + self.moderator2_profile.save() + + # Submit + self.submit() + + # Check that only one moderator got an email + self.assertEqual(len(mail.outbox), 1) + self.assertEqual([self.moderator.email], mail.outbox[0].to) + + def test_approved_notifications(self): + # Set up the page version + self.silent_submit() + # Approve + self.approve() + + # Submitter must recieve an approved email + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].to, ['submitter@email.com']) + self.assertEqual(mail.outbox[0].subject, 'The page "Hello world!" has been approved') + + def test_approved_notifications_preferences_respected(self): + # Submitter doesn't want 'approved' emails + self.submitter_profile.approved_notifications = False + self.submitter_profile.save() + + # Set up the page version + self.silent_submit() + # Approve + self.approve() + + # No email to send + self.assertEqual(len(mail.outbox), 0) + + def test_rejected_notifications(self): + # Set up the page version + self.silent_submit() + # Reject + self.reject() + + # Submitter must recieve a rejected email + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].to, ['submitter@email.com']) + self.assertEqual(mail.outbox[0].subject, 'The page "Hello world!" has been rejected') + + def test_rejected_notification_preferences_respected(self): + # Submitter doesn't want 'rejected' emails + self.submitter_profile.rejected_notifications = False + self.submitter_profile.save() + + # Set up the page version + self.silent_submit() + # Reject + self.reject() + + # No email to send + self.assertEqual(len(mail.outbox), 0) diff --git a/wagtail/wagtailadmin/urls.py b/wagtail/wagtailadmin/urls.py index 21f127c1d..35bb63d5e 100644 --- a/wagtail/wagtailadmin/urls.py +++ b/wagtail/wagtailadmin/urls.py @@ -78,6 +78,7 @@ urlpatterns += [ url(r'^login/$', account.login, name='wagtailadmin_login'), url(r'^account/$', account.account, name='wagtailadmin_account'), url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'), + url(r'^account/notification_preferences/$', account.notification_preferences, name='wagtailadmin_account_notification_preferences'), url(r'^logout/$', account.logout, name='wagtailadmin_logout'), url(r'^userbar/(\d+)/$', userbar.for_frontend, name='wagtailadmin_userbar_frontend'), diff --git a/wagtail/wagtailadmin/views/account.py b/wagtail/wagtailadmin/views/account.py index c5e461f55..65e8dbeec 100644 --- a/wagtail/wagtailadmin/views/account.py +++ b/wagtail/wagtailadmin/views/account.py @@ -9,12 +9,19 @@ from django.views.decorators.debug import sensitive_post_parameters from django.views.decorators.cache import never_cache from wagtail.wagtailadmin import forms +from wagtail.wagtailusers.forms import NotificationPreferencesForm +from wagtail.wagtailusers.models import UserProfile +from wagtail.wagtailcore.models import UserPagePermissionsProxy @permission_required('wagtailadmin.access_admin') def account(request): + user_perms = UserPagePermissionsProxy(request.user) + show_notification_preferences = user_perms.can_edit_pages() or user_perms.can_publish_pages() + return render(request, 'wagtailadmin/account/account.html', { 'show_change_password': getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True) and request.user.has_usable_password(), + 'show_notification_preferences': show_notification_preferences }) @@ -42,6 +49,29 @@ def change_password(request): }) +@permission_required('wagtailadmin.access_admin') +def notification_preferences(request): + + if request.POST: + form = NotificationPreferencesForm(request.POST, instance=UserProfile.get_for_user(request.user)) + + if form.is_valid(): + form.save() + messages.success(request, _("Your preferences have been updated successfully!")) + return redirect('wagtailadmin_account') + else: + form = NotificationPreferencesForm(instance=UserProfile.get_for_user(request.user)) + + # quick-and-dirty catch-all in case the form has been rendered with no + # fields, as the user has no customisable permissions + if not form.fields: + return redirect('wagtailadmin_account') + + return render(request, 'wagtailadmin/account/notification_preferences.html', { + 'form': form, + }) + + @sensitive_post_parameters() @never_cache def login(request): diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py index bfca64463..29585717f 100644 --- a/wagtail/wagtailadmin/views/pages.py +++ b/wagtail/wagtailadmin/views/pages.py @@ -5,14 +5,15 @@ from django.contrib import messages from django.contrib.contenttypes.models import ContentType from django.contrib.auth.decorators import permission_required from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger +from django.utils import timezone from django.utils.translation import ugettext as _ from django.views.decorators.vary import vary_on_headers from wagtail.wagtailadmin.edit_handlers import TabbedInterface, ObjectList from wagtail.wagtailadmin.forms import SearchForm -from wagtail.wagtailadmin import tasks, hooks +from wagtail.wagtailadmin import tasks, hooks, signals -from wagtail.wagtailcore.models import Page, PageRevision, get_page_types +from wagtail.wagtailcore.models import Page, PageRevision @permission_required('wagtailadmin.access_admin') @@ -57,6 +58,12 @@ def add_subpage(request, parent_page_id): page_types = sorted(parent_page.clean_subpage_types(), key=lambda pagetype: pagetype.name.lower()) + if len(page_types) == 1: + # Only one page type is available - redirect straight to the create form rather than + # making the user choose + content_type = page_types[0] + return redirect('wagtailadmin_pages_create', content_type.app_label, content_type.model, parent_page.id) + return render(request, 'wagtailadmin/pages/add_subpage.html', { 'parent_page': parent_page, 'page_types': page_types, @@ -109,15 +116,16 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_ except ContentType.DoesNotExist: raise Http404 + # Get class page_class = content_type.model_class() + # Make sure the class is a descendant of Page + if not issubclass(page_class, Page): + raise Http404 + # page must be in the list of allowed subpage types for this parent ID - # == Restriction temporarily relaxed so that as superusers we can add index pages and things - - # == TODO: reinstate this for regular editors when we have distinct user types - # - # if page_class not in parent_page.clean_subpage_types(): - # messages.error(request, "Sorry, you do not have access to create a page of type '%s' here." % content_type.name) - # return redirect('wagtailadmin_pages_select_type') + if content_type not in parent_page.clean_subpage_types(): + raise PermissionDenied page = page_class(owner=request.user) edit_handler_class = get_page_edit_handler(page_class) @@ -134,21 +142,63 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_ return slug form.fields['slug'].clean = clean_slug + # Stick another validator into the form to check that the scheduled publishing settings are set correctly + def clean(): + cleaned_data = form_class.clean(form) + + # Go live must be before expire + go_live_at = cleaned_data.get('go_live_at') + expire_at = cleaned_data.get('expire_at') + + if go_live_at and expire_at: + if go_live_at > expire_at: + msg = _('Go live date/time must be before expiry date/time') + form._errors['go_live_at'] = form.error_class([msg]) + form._errors['expire_at'] = form.error_class([msg]) + del cleaned_data['go_live_at'] + del cleaned_data['expire_at'] + + # Expire must be in the future + expire_at = cleaned_data.get('expire_at') + + if expire_at and expire_at < timezone.now(): + form._errors['expire_at'] = form.error_class([_('Expiry date/time must be in the future')]) + del cleaned_data['expire_at'] + + return cleaned_data + form.clean = clean + if form.is_valid(): page = form.save(commit=False) # don't save yet, as we need treebeard to assign tree params is_publishing = bool(request.POST.get('action-publish')) and parent_page_perms.can_publish_subpage() is_submitting = bool(request.POST.get('action-submit')) + go_live_at = form.cleaned_data.get('go_live_at') + future_go_live = go_live_at and go_live_at > timezone.now() + approved_go_live_at = None if is_publishing: - page.live = True page.has_unpublished_changes = False + page.expired = False + if future_go_live: + page.live = False + # Set approved_go_live_at only if is publishing + # and the future_go_live is actually in future + approved_go_live_at = go_live_at + else: + page.live = True else: page.live = False page.has_unpublished_changes = True parent_page.add_child(instance=page) # assign tree parameters - will cause page to be saved - page.save_revision(user=request.user, submitted_for_moderation=is_submitting) + + # Pass approved_go_live_at to save_revision + page.save_revision( + user=request.user, + submitted_for_moderation=is_submitting, + approved_go_live_at=approved_go_live_at + ) if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title)) @@ -165,9 +215,10 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_ return redirect('wagtailadmin_explore', page.get_parent().id) else: - messages.error(request, _("The page could not be created due to errors.")) + messages.error(request, _("The page could not be created due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) else: + signals.init_new_page.send(sender=create, page=page, parent=parent_page) form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) @@ -207,15 +258,54 @@ def edit(request, page_id): return slug form.fields['slug'].clean = clean_slug + # Stick another validator into the form to check that the scheduled publishing settings are set correctly + def clean(): + cleaned_data = form_class.clean(form) + + # Go live must be before expire + go_live_at = cleaned_data.get('go_live_at') + expire_at = cleaned_data.get('expire_at') + + if go_live_at and expire_at: + if go_live_at > expire_at: + msg = _('Go live date/time must be before expiry date/time') + form._errors['go_live_at'] = form.error_class([msg]) + form._errors['expire_at'] = form.error_class([msg]) + del cleaned_data['go_live_at'] + del cleaned_data['expire_at'] + + # Expire must be in the future + expire_at = cleaned_data.get('expire_at') + + if expire_at and expire_at < timezone.now(): + form._errors['expire_at'] = form.error_class([_('Expiry date/time must be in the future')]) + del cleaned_data['expire_at'] + + return cleaned_data + form.clean = clean + if form.is_valid(): is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) + go_live_at = form.cleaned_data.get('go_live_at') + future_go_live = go_live_at and go_live_at > timezone.now() + approved_go_live_at = None if is_publishing: - page.live = True page.has_unpublished_changes = False + page.expired = False + if future_go_live: + page.live = False + # Set approved_go_live_at only if publishing + approved_go_live_at = go_live_at + else: + page.live = True form.save() - page.revisions.update(submitted_for_moderation=False) + # Clear approved_go_live_at for older revisions + page.revisions.update( + submitted_for_moderation=False, + approved_go_live_at=None, + ) else: # not publishing the page if page.live: @@ -227,7 +317,11 @@ def edit(request, page_id): page.has_unpublished_changes = True form.save() - page.save_revision(user=request.user, submitted_for_moderation=is_submitting) + page.save_revision( + user=request.user, + submitted_for_moderation=is_submitting, + approved_go_live_at=approved_go_live_at + ) if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title)) @@ -245,10 +339,11 @@ def edit(request, page_id): return redirect('wagtailadmin_explore', page.get_parent().id) else: messages.error(request, _("The page could not be saved due to validation errors")) + edit_handler = edit_handler_class(instance=page, form=form) errors_debug = ( repr(edit_handler.form.errors) - + repr([(name, formset.errors) for (name, formset) in edit_handler.form.formsets.iteritems() if formset.errors]) + + repr([(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors]) ) else: form = form_class(instance=page) @@ -436,6 +531,8 @@ def unpublish(request, page_id): parent_id = page.get_parent().id page.live = False page.save() + # Since page is unpublished clear the approved_go_live_at of all revisions + page.revisions.update(approved_go_live_at=None) messages.success(request, _("Page '{0}' unpublished.").format(page.title)) return redirect('wagtailadmin_explore', parent_id) @@ -538,7 +635,8 @@ def get_page_edit_handler(page_class): if page_class not in PAGE_EDIT_HANDLERS: PAGE_EDIT_HANDLERS[page_class] = TabbedInterface([ ObjectList(page_class.content_panels, heading='Content'), - ObjectList(page_class.promote_panels, heading='Promote') + ObjectList(page_class.promote_panels, heading='Promote'), + ObjectList(page_class.settings_panels, heading='Settings', classname="settings") ]) return PAGE_EDIT_HANDLERS[page_class] diff --git a/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py new file mode 100644 index 000000000..cb0d4cab8 --- /dev/null +++ b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py @@ -0,0 +1,111 @@ +from __future__ import print_function + +import datetime +import json +from optparse import make_option + +from django.core.management.base import BaseCommand +from django.utils import dateparse, timezone +from wagtail.wagtailcore.models import Page, PageRevision + + +def revision_date_expired(r): + expiry_str = json.loads(r.content_json).get('expire_at') + if not expiry_str: + return False + expire_at = dateparse.parse_datetime(expiry_str) + if expire_at < timezone.now(): + return True + else: + return False + + +class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option( + '--dryrun', + action='store_true', + dest='dryrun', + default=False, + help='Dry run -- don\'t change anything.'), + ) + + def handle(self, *args, **options): + dryrun = False + if options['dryrun']: + print("Will do a dry run.") + dryrun = True + + # 1. get all expired pages with live = True + expired_pages = Page.objects.filter( + live=True, + expire_at__lt=timezone.now() + ) + if dryrun: + if expired_pages: + print("Expired pages to be deactivated:") + print("Expiry datetime\t\tSlug\t\tName") + print("---------------\t\t----\t\t----") + for ep in expired_pages: + print("{0}\t{1}\t{2}".format( + ep.expire_at.strftime("%Y-%m-%d %H:%M"), + ep.slug, + ep.title + )) + else: + print("No expired pages to be deactivated found.") + else: + expired_pages.update(expired=True, live=False) + + # 2. get all page revisions for moderation that have been expired + expired_revs = [ + r for r in PageRevision.objects.filter( + submitted_for_moderation=True + ) if revision_date_expired(r) + ] + if dryrun: + print("---------------------------------") + if expired_revs: + print("Expired revisions to be dropped from moderation queue:") + print("Expiry datetime\t\tSlug\t\tName") + print("---------------\t\t----\t\t----") + for er in expired_revs: + rev_data = json.loads(er.content_json) + print("{0}\t{1}\t{2}".format( + dateparse.parse_datetime( + rev_data.get('expire_at') + ).strftime("%Y-%m-%d %H:%M"), + rev_data.get('slug'), + rev_data.get('title') + )) + else: + print("No expired revision to be dropped from moderation.") + else: + for er in expired_revs: + er.submitted_for_moderation = False + er.save() + + # 3. get all revisions that need to be published + revs_for_publishing = PageRevision.objects.filter( + approved_go_live_at__lt=timezone.now() + ) + if dryrun: + print("---------------------------------") + if revs_for_publishing: + print("Revisions to be published:") + print("Go live datetime\t\tSlug\t\tName") + print("---------------\t\t\t----\t\t----") + for rp in revs_for_publishing: + rev_data = json.loads(rp.content_json) + print("{0}\t\t{1}\t{2}".format( + rp.approved_go_live_at.strftime("%Y-%m-%d %H:%M"), + rev_data.get('slug'), + rev_data.get('title') + )) + else: + print("No pages to go live.") + else: + for rp in revs_for_publishing: + # just run publish for the revision -- since the approved go + # live datetime is before now it will make the page live + rp.publish() diff --git a/wagtail/wagtailcore/migrations/0003_auto__del_unique_site_hostname__add_unique_site_hostname_port.py b/wagtail/wagtailcore/migrations/0003_auto__del_unique_site_hostname__add_unique_site_hostname_port.py new file mode 100644 index 000000000..16686e0a9 --- /dev/null +++ b/wagtail/wagtailcore/migrations/0003_auto__del_unique_site_hostname__add_unique_site_hostname_port.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Removing unique constraint on 'Site', fields ['hostname'] + db.delete_unique(u'wagtailcore_site', ['hostname']) + + # Adding unique constraint on 'Site', fields ['hostname', 'port'] + db.create_unique(u'wagtailcore_site', ['hostname', 'port']) + + + def backwards(self, orm): + # Removing unique constraint on 'Site', fields ['hostname', 'port'] + db.delete_unique(u'wagtailcore_site', ['hostname', 'port']) + + # Adding unique constraint on 'Site', fields ['hostname'] + db.create_unique(u'wagtailcore_site', ['hostname']) + + + models = { + u'auth.group': { + 'Meta': {'object_name': 'Group'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Permission']"}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + u'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'wagtailcore.grouppagepermission': { + 'Meta': {'object_name': 'GroupPagePermission'}, + 'group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'page_permissions'", 'to': u"orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'group_permissions'", 'to': u"orm['wagtailcore.Page']"}), + 'permission_type': ('django.db.models.fields.CharField', [], {'max_length': '20'}) + }, + u'wagtailcore.page': { + 'Meta': {'object_name': 'Page'}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'pages'", 'to': u"orm['contenttypes.ContentType']"}), + 'depth': ('django.db.models.fields.PositiveIntegerField', [], {}), + 'has_unpublished_changes': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'live': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'numchild': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), + 'owner': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'owned_pages'", 'null': 'True', 'to': u"orm['auth.User']"}), + 'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}), + 'search_description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'seo_title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}), + 'show_in_menus': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'url_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}) + }, + u'wagtailcore.pagerevision': { + 'Meta': {'object_name': 'PageRevision'}, + 'content_json': ('django.db.models.fields.TextField', [], {}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'revisions'", 'to': u"orm['wagtailcore.Page']"}), + 'submitted_for_moderation': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']", 'null': 'True', 'blank': 'True'}) + }, + u'wagtailcore.site': { + 'Meta': {'unique_together': "(('hostname', 'port'),)", 'object_name': 'Site'}, + 'hostname': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_default_site': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'port': ('django.db.models.fields.IntegerField', [], {'default': '80'}), + 'root_page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'sites_rooted_here'", 'to': u"orm['wagtailcore.Page']"}) + } + } + + complete_apps = ['wagtailcore'] \ No newline at end of file diff --git a/wagtail/wagtailcore/migrations/0004_fields_for_scheduled_publishing.py b/wagtail/wagtailcore/migrations/0004_fields_for_scheduled_publishing.py new file mode 100644 index 000000000..40602fa97 --- /dev/null +++ b/wagtail/wagtailcore/migrations/0004_fields_for_scheduled_publishing.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'PageRevision.approved_go_live_at' + db.add_column(u'wagtailcore_pagerevision', 'approved_go_live_at', + self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True), + keep_default=False) + + # Adding field 'Page.go_live_at' + db.add_column(u'wagtailcore_page', 'go_live_at', + self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True), + keep_default=False) + + # Adding field 'Page.expire_at' + db.add_column(u'wagtailcore_page', 'expire_at', + self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True), + keep_default=False) + + # Adding field 'Page.expired' + db.add_column(u'wagtailcore_page', 'expired', + self.gf('django.db.models.fields.BooleanField')(default=False), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'PageRevision.approved_go_live_at' + db.delete_column(u'wagtailcore_pagerevision', 'approved_go_live_at') + + # Deleting field 'Page.go_live_at' + db.delete_column(u'wagtailcore_page', 'go_live_at') + + # Deleting field 'Page.expire_at' + db.delete_column(u'wagtailcore_page', 'expire_at') + + # Deleting field 'Page.expired' + db.delete_column(u'wagtailcore_page', 'expired') + + + models = { + u'auth.group': { + 'Meta': {'object_name': 'Group'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Permission']"}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + u'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'wagtailcore.grouppagepermission': { + 'Meta': {'object_name': 'GroupPagePermission'}, + 'group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'page_permissions'", 'to': u"orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'group_permissions'", 'to': u"orm['wagtailcore.Page']"}), + 'permission_type': ('django.db.models.fields.CharField', [], {'max_length': '20'}) + }, + u'wagtailcore.page': { + 'Meta': {'object_name': 'Page'}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'pages'", 'to': u"orm['contenttypes.ContentType']"}), + 'depth': ('django.db.models.fields.PositiveIntegerField', [], {}), + 'expire_at': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'expired': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'go_live_at': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'has_unpublished_changes': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'live': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'numchild': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), + 'owner': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'owned_pages'", 'null': 'True', 'to': u"orm['auth.User']"}), + 'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}), + 'search_description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'seo_title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}), + 'show_in_menus': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'url_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}) + }, + u'wagtailcore.pagerevision': { + 'Meta': {'object_name': 'PageRevision'}, + 'approved_go_live_at': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'content_json': ('django.db.models.fields.TextField', [], {}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'revisions'", 'to': u"orm['wagtailcore.Page']"}), + 'submitted_for_moderation': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']", 'null': 'True', 'blank': 'True'}) + }, + u'wagtailcore.site': { + 'Meta': {'unique_together': "(('hostname', 'port'),)", 'object_name': 'Site'}, + 'hostname': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_default_site': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'port': ('django.db.models.fields.IntegerField', [], {'default': '80'}), + 'root_page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'sites_rooted_here'", 'to': u"orm['wagtailcore.Page']"}) + } + } + + complete_apps = ['wagtailcore'] \ No newline at end of file diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 294f5d40b..8c6ced813 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -1,7 +1,10 @@ -from StringIO import StringIO -from urlparse import urlparse import warnings +import six +from six import string_types +from six import StringIO +from six.moves.urllib.parse import urlparse + from modelcluster.models import ClusterableModel from django.db import models, connection, transaction @@ -14,7 +17,12 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import Group from django.conf import settings from django.template.response import TemplateResponse +from django.utils import timezone +from django.utils.translation import ugettext from django.utils.translation import ugettext_lazy as _ +from django.core.exceptions import ValidationError +from django.utils.functional import cached_property +from django.utils.encoding import python_2_unicode_compatible from treebeard.mp_tree import MP_Node @@ -25,29 +33,48 @@ from wagtail.wagtailsearch import Indexed, get_search_backend class SiteManager(models.Manager): - def get_by_natural_key(self, hostname): - return self.get(hostname=hostname) + def get_by_natural_key(self, hostname, port): + return self.get(hostname=hostname, port=port) +@python_2_unicode_compatible class Site(models.Model): - hostname = models.CharField(max_length=255, unique=True, db_index=True) + hostname = models.CharField(max_length=255, db_index=True) port = models.IntegerField(default=80, help_text=_("Set this to something other than 80 if you need a specific port number to appear in URLs (e.g. development on port 8000). Does not affect request handling (so port forwarding still works).")) root_page = models.ForeignKey('Page', related_name='sites_rooted_here') is_default_site = models.BooleanField(default=False, help_text=_("If true, this site will handle requests for all other hostnames that do not have a site entry of their own")) - def natural_key(self): - return (self.hostname,) + class Meta: + unique_together = ('hostname', 'port') - def __unicode__(self): + def natural_key(self): + return (self.hostname, self.port) + + def __str__(self): return self.hostname + ("" if self.port == 80 else (":%d" % self.port)) + (" [default]" if self.is_default_site else "") @staticmethod def find_for_request(request): - """Find the site object responsible for responding to this HTTP request object""" + """ + Find the site object responsible for responding to this HTTP + request object. Try: + - unique hostname first + - then hostname and port + - if there is no matching hostname at all, or no matching + hostname:port combination, fall back to the unique default site, + or raise an exception + NB this means that high-numbered ports on an extant hostname may + still be routed to a different hostname which is set as the default + """ try: - hostname = request.META['HTTP_HOST'].split(':')[0] - # find a Site matching this specific hostname - return Site.objects.get(hostname=hostname) + hostname = request.META['HTTP_HOST'].split(':')[0] # KeyError here goes to the final except clause + try: + # find a Site matching this specific hostname + return Site.objects.get(hostname=hostname) # Site.DoesNotExist here goes to the final except clause + except Site.MultipleObjectsReturned: + # as there were more than one, try matching by port too + port = request.META['SERVER_PORT'] # KeyError here goes to the final except clause + return Site.objects.get(hostname=hostname, port=int(port)) # Site.DoesNotExist here goes to the final except clause except (Site.DoesNotExist, KeyError): # If no matching site exists, or request does not specify an HTTP_HOST (which # will often be the case for the Django test client), look for a catch-all Site. @@ -63,6 +90,24 @@ class Site(models.Model): else: return 'http://%s:%d' % (self.hostname, self.port) + def clean_fields(self, exclude=None): + super(Site, self).clean_fields(exclude) + # Only one site can have the is_default_site flag set + try: + default = Site.objects.get(is_default_site=True) + except Site.DoesNotExist: + pass + except Site.MultipleObjectsReturned: + raise + else: + if self.is_default_site and self.pk != default.pk: + raise ValidationError( + {'is_default_site': [ + _("%(hostname)s is already configured as the default site. You must unset that before you can save this site as default.") + % { 'hostname': default.hostname } + ]} + ) + # clear the wagtail_site_root_paths cache whenever Site records are updated def save(self, *args, **kwargs): result = super(Site, self).save(*args, **kwargs) @@ -135,6 +180,12 @@ class PageManager(models.Manager): def not_live(self): return self.get_queryset().not_live() + def in_menu(self): + return self.get_queryset().in_menu() + + def not_in_menu(self): + return self.get_queryset().not_in_menu() + def page(self, other): return self.get_queryset().page(other) @@ -209,9 +260,8 @@ class PageBase(models.base.ModelBase): PAGE_MODEL_CLASSES.append(cls) -class Page(MP_Node, ClusterableModel, Indexed): - __metaclass__ = PageBase - +@python_2_unicode_compatible +class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, Indexed)): title = models.CharField(max_length=255, help_text=_("The page title as you'd like it to be seen by the public")) slug = models.SlugField(help_text=_("The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/")) # TODO: enforce uniqueness on slug field per parent (will have to be done at the Django @@ -226,6 +276,10 @@ class Page(MP_Node, ClusterableModel, Indexed): show_in_menus = models.BooleanField(default=False, help_text=_("Whether a link to this page will appear in automatically generated menus")) search_description = models.TextField(blank=True) + go_live_at = models.DateTimeField(verbose_name=_("Go live date/time"), help_text=_("Please add a date-time in the form YYYY-MM-DD hh:mm."), blank=True, null=True) + expire_at = models.DateTimeField(verbose_name=_("Expiry date/time"), help_text=_("Please add a date-time in the form YYYY-MM-DD hh:mm."), blank=True, null=True) + expired = models.BooleanField(default=False, editable=False) + indexed_fields = { 'title': { 'type': 'string', @@ -250,7 +304,7 @@ class Page(MP_Node, ClusterableModel, Indexed): # created as self.content_type = ContentType.objects.get_for_model(self) - def __unicode__(self): + def __str__(self): return self.title is_abstract = True # don't offer Page in the list of page types a superuser can create @@ -320,10 +374,10 @@ class Page(MP_Node, ClusterableModel, Indexed): SET url_path = %s || substring(url_path from %s) WHERE path LIKE %s AND id <> %s """ - cursor.execute(update_statement, + cursor.execute(update_statement, [new_url_path, len(old_url_path) + 1, self.path + '%', self.id]) - @property + @cached_property def specific(self): """ Return this page in its most specific subclassed form. @@ -337,7 +391,7 @@ class Page(MP_Node, ClusterableModel, Indexed): else: return content_type.get_object_for_this_type(id=self.id) - @property + @cached_property def specific_class(self): """ return the class that this page would be if instantiated in its @@ -366,24 +420,24 @@ class Page(MP_Node, ClusterableModel, Indexed): else: raise Http404 - def save_revision(self, user=None, submitted_for_moderation=False): - self.revisions.create(content_json=self.to_json(), user=user, submitted_for_moderation=submitted_for_moderation) + def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None): + return self.revisions.create( + content_json=self.to_json(), + user=user, + submitted_for_moderation=submitted_for_moderation, + approved_go_live_at=approved_go_live_at, + ) def get_latest_revision(self): - try: - revision = self.revisions.order_by('-created_at')[0] - except IndexError: - return False - - return revision + return self.revisions.order_by('-created_at').first() def get_latest_revision_as_page(self): - try: - revision = self.revisions.order_by('-created_at')[0] - except IndexError: - return self.specific + latest_revision = self.get_latest_revision() - return revision.as_page_object() + if latest_revision: + return latest_revision.as_page_object() + else: + return self.specific def get_context(self, request, *args, **kwargs): return { @@ -399,8 +453,8 @@ class Page(MP_Node, ClusterableModel, Indexed): def serve(self, request, *args, **kwargs): return TemplateResponse( - request, - self.get_template(request, *args, **kwargs), + request, + self.get_template(request, *args, **kwargs), self.get_context(request, *args, **kwargs) ) @@ -413,6 +467,10 @@ class Page(MP_Node, ClusterableModel, Indexed): return (not self.is_leaf()) or self.depth == 2 def get_other_siblings(self): + warnings.warn( + "The 'Page.get_other_siblings()' method has been replaced. " + "Use 'Page.get_siblings(inclusive=False)' instead.", DeprecationWarning) + # get sibling pages excluding self return self.get_siblings().exclude(id=self.id) @@ -482,7 +540,7 @@ class Page(MP_Node, ClusterableModel, Indexed): else: res = [] for page_type in cls.subpage_types: - if isinstance(page_type, basestring): + if isinstance(page_type, string_types): try: app_label, model_name = page_type.split(".") except ValueError: @@ -527,13 +585,22 @@ class Page(MP_Node, ClusterableModel, Indexed): @property def status_string(self): if not self.live: - return "draft" + if self.expired: + return "expired" + elif self.approved_schedule: + return "scheduled" + else: + return "draft" else: if self.has_unpublished_changes: return "live + draft" else: return "live" + @property + def approved_schedule(self): + return self.revisions.exclude(approved_go_live_at__isnull=True).exists() + def has_unpublished_subtree(self): """ An awkwardly-defined flag used in determining whether unprivileged editors have @@ -655,6 +722,12 @@ class Page(MP_Node, ClusterableModel, Indexed): def get_siblings(self, inclusive=True): return Page.objects.sibling_of(self, inclusive) + def get_next_siblings(self, inclusive=False): + return self.get_siblings(inclusive).filter(path__gte=self.path).order_by('path') + + def get_prev_siblings(self, inclusive=False): + return self.get_siblings(inclusive).filter(path__lte=self.path).order_by('-path') + def get_navigation_menu_items(): # Get all pages that appear in the navigation menu: ones which have children, @@ -711,12 +784,14 @@ class SubmittedRevisionsManager(models.Manager): return super(SubmittedRevisionsManager, self).get_queryset().filter(submitted_for_moderation=True) +@python_2_unicode_compatible class PageRevision(models.Model): page = models.ForeignKey('Page', related_name='revisions') submitted_for_moderation = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) content_json = models.TextField() + approved_go_live_at = models.DateTimeField(null=True, blank=True) objects = models.Manager() submitted_revisions = SubmittedRevisionsManager() @@ -750,11 +825,27 @@ class PageRevision(models.Model): def publish(self): page = self.as_page_object() - page.live = True + if page.go_live_at and page.go_live_at > timezone.now(): + # if we have a go_live in the future don't make the page live + page.live = False + # Instead set the approved_go_live_at of this revision + self.approved_go_live_at = page.go_live_at + self.save() + # And clear the the approved_go_live_at of any other revisions + page.revisions.exclude(id=self.id).update(approved_go_live_at=None) + else: + page.live = True + # If page goes live clear the approved_go_live_at of all revisions + page.revisions.update(approved_go_live_at=None) + page.expired = False # When a page is published it can't be expired page.save() self.submitted_for_moderation = False page.revisions.update(submitted_for_moderation=False) + def __str__(self): + return '"' + unicode(self.page) + '" at ' + unicode(self.created_at) + + PAGE_PERMISSION_TYPE_CHOICES = [ ('add', 'Add'), ('edit', 'Edit'), @@ -813,18 +904,39 @@ class UserPagePermissionsProxy(object): if self.user.is_superuser: return Page.objects.all() + editable_pages = Page.objects.none() + + for perm in self.permissions.filter(permission_type='add'): + # user has edit permission on any subpage of perm.page + # (including perm.page itself) that is owned by them + editable_pages |= Page.objects.descendant_of(perm.page, inclusive=True).filter(owner=self.user) + + for perm in self.permissions.filter(permission_type='edit'): + # user has edit permission on any subpage of perm.page + # (including perm.page itself) regardless of owner + editable_pages |= Page.objects.descendant_of(perm.page, inclusive=True) + + return editable_pages + + + def can_edit_pages(self): + """Return True if the user has permission to edit any pages""" + return True if self.editable_pages().count() else False + + def publishable_pages(self): + """Return a queryset of the pages that this user has permission to publish""" + # Deal with the trivial cases first... + if not self.user.is_active: + return Page.objects.none() + if self.user.is_superuser: + return Page.objects.all() + # Translate each of the user's permission rules into a Q-expression q_expressions = [] for perm in self.permissions: - if perm.permission_type == 'add': - # user has edit permission on any subpage of perm.page - # (including perm.page itself) that is owned by them - q_expressions.append( - Q(path__startswith=perm.page.path, owner=self.user) - ) - elif perm.permission_type == 'edit': - # user has edit permission on any subpage of perm.page - # (including perm.page itself) regardless of owner + if perm.permission_type == 'publish': + # user has publish permission on any subpage of perm.page + # (including perm.page itself) q_expressions.append( Q(path__startswith=perm.page.path) ) @@ -837,6 +949,11 @@ class UserPagePermissionsProxy(object): else: return Page.objects.none() + def can_publish_pages(self): + """Return True if the user has permission to publish any pages""" + return True if self.publishable_pages().count() else False + + class PagePermissionTester(object): def __init__(self, user_perms, page): self.user = user_perms.user @@ -853,6 +970,8 @@ class PagePermissionTester(object): def can_add_subpage(self): if not self.user.is_active: return False + if not self.page.specific_class.clean_subpage_types(): # this page model has an empty subpage_types list, so no subpages are allowed + return False return self.user.is_superuser or ('add' in self.permissions) def can_edit(self): @@ -907,10 +1026,13 @@ class PagePermissionTester(object): """ Niggly special case for creating and publishing a page in one go. Differs from can_publish in that we want to be able to publish subpages of root, but not - to be able to publish root itself + to be able to publish root itself. (Also, can_publish_subpage returns false if the page + does not allow subpages at all.) """ if not self.user.is_active: return False + if not self.page.specific_class.clean_subpage_types(): # this page model has an empty subpage_types list, so no subpages are allowed + return False return self.user.is_superuser or ('publish' in self.permissions) diff --git a/wagtail/wagtailcore/query.py b/wagtail/wagtailcore/query.py index 9404e25b4..57e8ffff3 100644 --- a/wagtail/wagtailcore/query.py +++ b/wagtail/wagtailcore/query.py @@ -16,6 +16,15 @@ class PageQuerySet(MP_NodeQuerySet): def not_live(self): return self.exclude(self.live_q()) + def in_menu_q(self): + return Q(show_in_menus=True) + + def in_menu(self): + return self.filter(self.in_menu_q()) + + def not_in_menu(self): + return self.exclude(self.in_menu_q()) + def page_q(self, other): return Q(id=other.id) diff --git a/wagtail/wagtailcore/rich_text.py b/wagtail/wagtailcore/rich_text.py index aa8f25336..c5229f747 100644 --- a/wagtail/wagtailcore/rich_text.py +++ b/wagtail/wagtailcore/rich_text.py @@ -13,6 +13,8 @@ from wagtail.wagtaildocs.models import Document from wagtail.wagtailimages.models import get_image_model from wagtail.wagtailimages.formats import get_image_format +from wagtail.wagtailadmin import hooks + # Define a set of 'embed handlers' and 'link handlers'. These handle the translation # of 'special' HTML elements in rich text - ones which we do not want to include @@ -158,6 +160,18 @@ LINK_HANDLERS = { # Prepare a whitelisting engine with custom behaviour: # rewrite any elements with a data-embedtype or data-linktype attribute class DbWhitelister(Whitelister): + has_loaded_custom_whitelist_rules = False + + @classmethod + def clean(cls, html): + if not cls.has_loaded_custom_whitelist_rules: + for fn in hooks.get_hooks('construct_whitelister_element_rules'): + cls.element_rules = cls.element_rules.copy() + cls.element_rules.update(fn()) + cls.has_loaded_custom_whitelist_rules = True + + return super(DbWhitelister, cls).clean(html) + @classmethod def clean_tag_node(cls, doc, tag): if 'data-embedtype' in tag.attrs: diff --git a/wagtail/wagtailcore/templatetags/pageurl.py b/wagtail/wagtailcore/templatetags/pageurl.py index d3488d9b4..63d2eb45e 100644 --- a/wagtail/wagtailcore/templatetags/pageurl.py +++ b/wagtail/wagtailcore/templatetags/pageurl.py @@ -1,24 +1,8 @@ -from django import template +import warnings -from wagtail.wagtailcore.models import Page - -register = template.Library() +warnings.warn( + "The pageurl tag library has been moved to wagtailcore_tags. " + "Use {% load wagtailcore_tags %} instead.", DeprecationWarning) -@register.simple_tag(takes_context=True) -def pageurl(context, page): - """ - Outputs a page's URL as relative (/foo/bar/) if it's within the same site as the - current page, or absolute (http://example.com/foo/bar/) if not. - """ - return page.relative_url(context['request'].site) - -@register.simple_tag(takes_context=True) -def slugurl(context, slug): - """Returns the URL for the page that has the given slug.""" - page = Page.objects.filter(slug=slug).first() - - if page: - return page.relative_url(context['request'].site) - else: - return None +from wagtail.wagtailcore.templatetags.wagtailcore_tags import register, pageurl diff --git a/wagtail/wagtailcore/templatetags/rich_text.py b/wagtail/wagtailcore/templatetags/rich_text.py index d3bc64fdd..c1db48250 100644 --- a/wagtail/wagtailcore/templatetags/rich_text.py +++ b/wagtail/wagtailcore/templatetags/rich_text.py @@ -1,11 +1,8 @@ -from django import template -from django.utils.safestring import mark_safe +import warnings -from wagtail.wagtailcore.rich_text import expand_db_html - -register = template.Library() +warnings.warn( + "The rich_text tag library has been moved to wagtailcore_tags. " + "Use {% load wagtailcore_tags %} instead.", DeprecationWarning) -@register.filter -def richtext(value): - return mark_safe('
      ' + expand_db_html(value) + '
      ') +from wagtail.wagtailcore.templatetags.wagtailcore_tags import register, richtext diff --git a/wagtail/wagtailcore/templatetags/wagtailcore_tags.py b/wagtail/wagtailcore/templatetags/wagtailcore_tags.py new file mode 100644 index 000000000..5e137c7a2 --- /dev/null +++ b/wagtail/wagtailcore/templatetags/wagtailcore_tags.py @@ -0,0 +1,32 @@ +from django import template +from django.utils.safestring import mark_safe + +from wagtail.wagtailcore.models import Page +from wagtail.wagtailcore.rich_text import expand_db_html + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def pageurl(context, page): + """ + Outputs a page's URL as relative (/foo/bar/) if it's within the same site as the + current page, or absolute (http://example.com/foo/bar/) if not. + """ + return page.relative_url(context['request'].site) + + +@register.simple_tag(takes_context=True) +def slugurl(context, slug): + """Returns the URL for the page that has the given slug.""" + page = Page.objects.filter(slug=slug).first() + + if page: + return page.relative_url(context['request'].site) + else: + return None + + +@register.filter +def richtext(value): + return mark_safe('
      ' + expand_db_html(value) + '
      ') diff --git a/wagtail/wagtailcore/tests/test_dbwhitelister.py b/wagtail/wagtailcore/tests/test_dbwhitelister.py new file mode 100644 index 000000000..795b9637c --- /dev/null +++ b/wagtail/wagtailcore/tests/test_dbwhitelister.py @@ -0,0 +1,50 @@ +from django.test import TestCase +from wagtail.wagtailcore.rich_text import DbWhitelister +from wagtail.wagtailcore.whitelist import Whitelister + +from bs4 import BeautifulSoup + +class TestDbWhitelister(TestCase): + def assertHtmlEqual(self, str1, str2): + """ + Assert that two HTML strings are equal at the DOM level + (necessary because we can't guarantee the order that attributes are output in) + """ + self.assertEqual(BeautifulSoup(str1), BeautifulSoup(str2)) + + def test_page_link_is_rewritten(self): + input_html = '

      Look at the lovely homepage of my Wagtail site

      ' + output_html = DbWhitelister.clean(input_html) + expected = '

      Look at the lovely homepage of my Wagtail site

      ' + self.assertHtmlEqual(expected, output_html) + + def test_document_link_is_rewritten(self): + input_html = '

      Look at our horribly oversized brochure

      ' + output_html = DbWhitelister.clean(input_html) + expected = '

      Look at our horribly oversized brochure

      ' + self.assertHtmlEqual(expected, output_html) + + def test_image_embed_is_rewritten(self): + input_html = '

      OMG look at this picture of a kitten:

      A cute kitten
      A kitten, yesterday.

      ' + output_html = DbWhitelister.clean(input_html) + expected = '

      OMG look at this picture of a kitten:

      ' + self.assertHtmlEqual(expected, output_html) + + def test_media_embed_is_rewritten(self): + input_html = '

      OMG look at this video of a kitten:

      ' + output_html = DbWhitelister.clean(input_html) + expected = '

      OMG look at this video of a kitten:

      ' + self.assertHtmlEqual(expected, output_html) + + def test_whitelist_hooks(self): + # wagtail.tests.wagtail_hooks overrides the whitelist to permit
      and + input_html = '
      I would put a tax on all people who stand in water.

      - Gumby

      ' + output_html = DbWhitelister.clean(input_html) + expected = '
      I would put a tax on all people who stand in water.

      - Gumby

      ' + self.assertHtmlEqual(expected, output_html) + + # check that the base Whitelister class is unaffected by these custom whitelist rules + input_html = '
      I would put a tax on all people who stand in water.

      - Gumby

      ' + output_html = Whitelister.clean(input_html) + expected = 'I would put a tax on all people who stand in water.

      - Gumby

      ' + self.assertHtmlEqual(expected, output_html) diff --git a/wagtail/wagtailcore/tests/test_management_commands.py b/wagtail/wagtailcore/tests/test_management_commands.py index 787b808b5..1e3ff28e1 100644 --- a/wagtail/wagtailcore/tests/test_management_commands.py +++ b/wagtail/wagtailcore/tests/test_management_commands.py @@ -1,11 +1,14 @@ -from StringIO import StringIO +from datetime import timedelta + +from six import StringIO from django.test import TestCase, Client from django.http import HttpRequest, Http404 from django.core import management from django.contrib.auth.models import User +from django.utils import timezone -from wagtail.wagtailcore.models import Page, Site, UserPagePermissionsProxy +from wagtail.wagtailcore.models import Page, PageRevision, Site, UserPagePermissionsProxy from wagtail.tests.models import EventPage, EventIndex, SimplePage @@ -87,3 +90,107 @@ class TestReplaceTextCommand(TestCase): # Check that its now about easter self.assertEqual(Page.objects.get(url_path='/home/events/christmas/').title, "Easter") + + +class TestPublishScheduledPagesCommand(TestCase): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + def test_go_live_page_will_be_published(self): + page = SimplePage( + title="Hello world!", + slug="hello-world", + live=False, + go_live_at=timezone.now() - timedelta(days=1), + ) + self.root_page.add_child(instance=page) + + page.save_revision(approved_go_live_at=timezone.now() - timedelta(days=1)) + + p = Page.objects.get(slug='hello-world') + self.assertFalse(p.live) + self.assertTrue(PageRevision.objects.filter(page=p).exclude(approved_go_live_at__isnull=True).exists()) + + management.call_command('publish_scheduled_pages') + + p = Page.objects.get(slug='hello-world') + self.assertTrue(p.live) + self.assertFalse(PageRevision.objects.filter(page=p).exclude(approved_go_live_at__isnull=True).exists()) + + def test_future_go_live_page_will_not_be_published(self): + page = SimplePage( + title="Hello world!", + slug="hello-world", + live=False, + go_live_at=timezone.now() + timedelta(days=1), + ) + self.root_page.add_child(instance=page) + + page.save_revision(approved_go_live_at=timezone.now() - timedelta(days=1)) + + p = Page.objects.get(slug='hello-world') + self.assertFalse(p.live) + self.assertTrue(PageRevision.objects.filter(page=p).exclude(approved_go_live_at__isnull=True).exists()) + + management.call_command('publish_scheduled_pages') + + p = Page.objects.get(slug='hello-world') + self.assertFalse(p.live) + self.assertTrue(PageRevision.objects.filter(page=p).exclude(approved_go_live_at__isnull=True).exists()) + + def test_expired_page_will_be_unpublished(self): + page = SimplePage( + title="Hello world!", + slug="hello-world", + live=True, + expire_at=timezone.now() - timedelta(days=1), + ) + self.root_page.add_child(instance=page) + + p = Page.objects.get(slug='hello-world') + self.assertTrue(p.live) + + management.call_command('publish_scheduled_pages') + + p = Page.objects.get(slug='hello-world') + self.assertFalse(p.live) + self.assertTrue(p.expired) + + def test_future_expired_page_will_not_be_unpublished(self): + page = SimplePage( + title="Hello world!", + slug="hello-world", + live=True, + expire_at=timezone.now() + timedelta(days=1), + ) + self.root_page.add_child(instance=page) + + p = Page.objects.get(slug='hello-world') + self.assertTrue(p.live) + + management.call_command('publish_scheduled_pages') + + p = Page.objects.get(slug='hello-world') + self.assertTrue(p.live) + self.assertFalse(p.expired) + + def test_expired_pages_are_dropped_from_mod_queue(self): + page = SimplePage( + title="Hello world!", + slug="hello-world", + live=False, + expire_at=timezone.now() - timedelta(days=1), + ) + self.root_page.add_child(instance=page) + + page.save_revision(submitted_for_moderation=True) + + p = Page.objects.get(slug='hello-world') + self.assertFalse(p.live) + self.assertTrue(PageRevision.objects.filter(page=p, submitted_for_moderation=True).exists()) + + management.call_command('publish_scheduled_pages') + + p = Page.objects.get(slug='hello-world') + self.assertFalse(PageRevision.objects.filter(page=p, submitted_for_moderation=True).exists()) diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 1bae57952..58c359d72 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -1,4 +1,4 @@ -from StringIO import StringIO +from six import StringIO from django.test import TestCase, Client from django.http import HttpRequest, Http404 @@ -9,30 +9,96 @@ from wagtail.wagtailcore.models import Page, Site, UserPagePermissionsProxy from wagtail.tests.models import EventPage, EventIndex, SimplePage -class TestRouting(TestCase): +class TestSiteRouting(TestCase): fixtures = ['test.json'] - def test_find_site_for_request(self): - default_site = Site.objects.get(is_default_site=True) + def setUp(self): + self.default_site = Site.objects.get(is_default_site=True) events_page = Page.objects.get(url_path='/home/events/') - events_site = Site.objects.create(hostname='events.example.com', root_page=events_page) + about_page = Page.objects.get(url_path='/home/about-us/') + self.events_site = Site.objects.create(hostname='events.example.com', root_page=events_page) + self.alternate_port_events_site = Site.objects.create(hostname='events.example.com', root_page=events_page, port='8765') + self.about_site = Site.objects.create(hostname='about.example.com', root_page=about_page) + self.unrecognised_port = '8000' + self.unrecognised_hostname = 'unknown.site.com' + def test_no_host_header_routes_to_default_site(self): # requests without a Host: header should be directed to the default site request = HttpRequest() request.path = '/' - self.assertEqual(Site.find_for_request(request), default_site) + self.assertEqual(Site.find_for_request(request), self.default_site) + def test_valid_headers_route_to_specific_site(self): # requests with a known Host: header should be directed to the specific site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = 'events.example.com' - self.assertEqual(Site.find_for_request(request), events_site) + request.META['HTTP_HOST'] = self.events_site.hostname + request.META['SERVER_PORT'] = self.events_site.port + self.assertEqual(Site.find_for_request(request), self.events_site) + def test_ports_in_request_headers_are_respected(self): + # ports in the Host: header should be respected + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = self.alternate_port_events_site.hostname + request.META['SERVER_PORT'] = self.alternate_port_events_site.port + self.assertEqual(Site.find_for_request(request), self.alternate_port_events_site) + + def test_unrecognised_host_header_routes_to_default_site(self): # requests with an unrecognised Host: header should be directed to the default site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = 'unknown.example.com' - self.assertEqual(Site.find_for_request(request), default_site) + request.META['HTTP_HOST'] = self.unrecognised_hostname + request.META['SERVER_PORT'] = '80' + self.assertEqual(Site.find_for_request(request), self.default_site) + + def test_unrecognised_port_and_default_host_routes_to_default_site(self): + # requests to the default host on an unrecognised port should be directed to the default site + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = self.default_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + + def test_unrecognised_port_and_unrecognised_host_routes_to_default_site(self): + # requests with an unrecognised Host: header _and_ an unrecognised port + # hould be directed to the default site + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = self.unrecognised_hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + + def test_unrecognised_port_on_known_hostname_routes_there_if_no_ambiguity(self): + # requests on an unrecognised port should be directed to the site with + # matching hostname if there is no ambiguity + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = self.about_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.about_site) + + def test_unrecognised_port_on_known_hostname_routes_to_default_site_if_ambiguity(self): + # requests on an unrecognised port should be directed to the default + # site, even if their hostname (but not port) matches more than one + # other entry + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = self.events_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + + def test_port_in_http_host_header_is_ignored(self): + # port in the HTTP_HOST header is ignored + request = HttpRequest() + request.path = '/' + request.META['HTTP_HOST'] = "%s:%s" % (self.events_site.hostname, self.events_site.port) + request.META['SERVER_PORT'] = self.alternate_port_events_site.port + self.assertEqual(Site.find_for_request(request), self.alternate_port_events_site) + + +class TestRouting(TestCase): + fixtures = ['test.json'] def test_urls(self): default_site = Site.objects.get(is_default_site=True) @@ -224,3 +290,24 @@ class TestMovePage(TestCase): christmas = events_index.get_children().get(slug='christmas') self.assertEqual(christmas.depth, 5) self.assertEqual(christmas.url_path, '/home/about-us/events/christmas/') + + +class TestPrevNextSiblings(TestCase): + fixtures = ['test.json'] + + def test_get_next_siblings(self): + christmas_event = Page.objects.get(url_path='/home/events/christmas/') + self.assertTrue(christmas_event.get_next_siblings().filter(url_path='/home/events/final-event/').exists()) + + def test_get_next_siblings_inclusive(self): + christmas_event = Page.objects.get(url_path='/home/events/christmas/') + + # First element must always be the current page + self.assertEqual(christmas_event.get_next_siblings(inclusive=True).first(), christmas_event) + + def test_get_prev_siblings(self): + final_event = Page.objects.get(url_path='/home/events/final-event/') + self.assertTrue(final_event.get_prev_siblings().filter(url_path='/home/events/christmas/').exists()) + + # First element must always be the current page + self.assertEqual(final_event.get_prev_siblings(inclusive=True).first(), final_event) diff --git a/wagtail/wagtailcore/tests/test_page_permissions.py b/wagtail/wagtailcore/tests/test_page_permissions.py index dbe35e39b..eb2fe3257 100644 --- a/wagtail/wagtailcore/tests/test_page_permissions.py +++ b/wagtail/wagtailcore/tests/test_page_permissions.py @@ -1,4 +1,4 @@ -from StringIO import StringIO +from six import StringIO from django.test import TestCase, Client from django.http import HttpRequest, Http404 @@ -176,13 +176,26 @@ class TestPagePermission(TestCase): unpublished_event_page = EventPage.objects.get(url_path='/home/events/tentative-unpublished-event/') someone_elses_event_page = EventPage.objects.get(url_path='/home/events/someone-elses-event/') - editable_pages = UserPagePermissionsProxy(event_editor).editable_pages() + user_perms = UserPagePermissionsProxy(event_editor) + editable_pages = user_perms.editable_pages() + can_edit_pages = user_perms.can_edit_pages() + publishable_pages = user_perms.publishable_pages() + can_publish_pages = user_perms.can_publish_pages() self.assertFalse(editable_pages.filter(id=homepage.id).exists()) self.assertTrue(editable_pages.filter(id=christmas_page.id).exists()) self.assertTrue(editable_pages.filter(id=unpublished_event_page.id).exists()) self.assertFalse(editable_pages.filter(id=someone_elses_event_page.id).exists()) + self.assertTrue(can_edit_pages) + + self.assertFalse(publishable_pages.filter(id=homepage.id).exists()) + self.assertFalse(publishable_pages.filter(id=christmas_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertFalse(can_publish_pages) + def test_editable_pages_for_user_with_edit_permission(self): event_moderator = User.objects.get(username='eventmoderator') homepage = Page.objects.get(url_path='/home/') @@ -190,13 +203,26 @@ class TestPagePermission(TestCase): unpublished_event_page = EventPage.objects.get(url_path='/home/events/tentative-unpublished-event/') someone_elses_event_page = EventPage.objects.get(url_path='/home/events/someone-elses-event/') - editable_pages = UserPagePermissionsProxy(event_moderator).editable_pages() + user_perms = UserPagePermissionsProxy(event_moderator) + editable_pages = user_perms.editable_pages() + can_edit_pages = user_perms.can_edit_pages() + publishable_pages = user_perms.publishable_pages() + can_publish_pages = user_perms.can_publish_pages() self.assertFalse(editable_pages.filter(id=homepage.id).exists()) self.assertTrue(editable_pages.filter(id=christmas_page.id).exists()) self.assertTrue(editable_pages.filter(id=unpublished_event_page.id).exists()) self.assertTrue(editable_pages.filter(id=someone_elses_event_page.id).exists()) + self.assertTrue(can_edit_pages) + + self.assertFalse(publishable_pages.filter(id=homepage.id).exists()) + self.assertTrue(publishable_pages.filter(id=christmas_page.id).exists()) + self.assertTrue(publishable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertTrue(publishable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertTrue(can_publish_pages) + def test_editable_pages_for_inactive_user(self): user = User.objects.get(username='inactiveuser') homepage = Page.objects.get(url_path='/home/') @@ -204,13 +230,26 @@ class TestPagePermission(TestCase): unpublished_event_page = EventPage.objects.get(url_path='/home/events/tentative-unpublished-event/') someone_elses_event_page = EventPage.objects.get(url_path='/home/events/someone-elses-event/') - editable_pages = UserPagePermissionsProxy(user).editable_pages() + user_perms = UserPagePermissionsProxy(user) + editable_pages = user_perms.editable_pages() + can_edit_pages = user_perms.can_edit_pages() + publishable_pages = user_perms.publishable_pages() + can_publish_pages = user_perms.can_publish_pages() self.assertFalse(editable_pages.filter(id=homepage.id).exists()) self.assertFalse(editable_pages.filter(id=christmas_page.id).exists()) self.assertFalse(editable_pages.filter(id=unpublished_event_page.id).exists()) self.assertFalse(editable_pages.filter(id=someone_elses_event_page.id).exists()) + self.assertFalse(can_edit_pages) + + self.assertFalse(publishable_pages.filter(id=homepage.id).exists()) + self.assertFalse(publishable_pages.filter(id=christmas_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertFalse(can_publish_pages) + def test_editable_pages_for_superuser(self): user = User.objects.get(username='superuser') homepage = Page.objects.get(url_path='/home/') @@ -218,9 +257,49 @@ class TestPagePermission(TestCase): unpublished_event_page = EventPage.objects.get(url_path='/home/events/tentative-unpublished-event/') someone_elses_event_page = EventPage.objects.get(url_path='/home/events/someone-elses-event/') - editable_pages = UserPagePermissionsProxy(user).editable_pages() + user_perms = UserPagePermissionsProxy(user) + editable_pages = user_perms.editable_pages() + can_edit_pages = user_perms.can_edit_pages() + publishable_pages = user_perms.publishable_pages() + can_publish_pages = user_perms.can_publish_pages() self.assertTrue(editable_pages.filter(id=homepage.id).exists()) self.assertTrue(editable_pages.filter(id=christmas_page.id).exists()) self.assertTrue(editable_pages.filter(id=unpublished_event_page.id).exists()) self.assertTrue(editable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertTrue(can_edit_pages) + + self.assertTrue(publishable_pages.filter(id=homepage.id).exists()) + self.assertTrue(publishable_pages.filter(id=christmas_page.id).exists()) + self.assertTrue(publishable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertTrue(publishable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertTrue(can_publish_pages) + + def test_editable_pages_for_non_editing_user(self): + user = User.objects.get(username='admin_only_user') + homepage = Page.objects.get(url_path='/home/') + christmas_page = EventPage.objects.get(url_path='/home/events/christmas/') + unpublished_event_page = EventPage.objects.get(url_path='/home/events/tentative-unpublished-event/') + someone_elses_event_page = EventPage.objects.get(url_path='/home/events/someone-elses-event/') + + user_perms = UserPagePermissionsProxy(user) + editable_pages = user_perms.editable_pages() + can_edit_pages = user_perms.can_edit_pages() + publishable_pages = user_perms.publishable_pages() + can_publish_pages = user_perms.can_publish_pages() + + self.assertFalse(editable_pages.filter(id=homepage.id).exists()) + self.assertFalse(editable_pages.filter(id=christmas_page.id).exists()) + self.assertFalse(editable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertFalse(editable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertFalse(can_edit_pages) + + self.assertFalse(publishable_pages.filter(id=homepage.id).exists()) + self.assertFalse(publishable_pages.filter(id=christmas_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=unpublished_event_page.id).exists()) + self.assertFalse(publishable_pages.filter(id=someone_elses_event_page.id).exists()) + + self.assertFalse(can_publish_pages) diff --git a/wagtail/wagtailcore/tests/test_page_queryset.py b/wagtail/wagtailcore/tests/test_page_queryset.py index 06f2c3e21..2c4e46c9c 100644 --- a/wagtail/wagtailcore/tests/test_page_queryset.py +++ b/wagtail/wagtailcore/tests/test_page_queryset.py @@ -1,4 +1,4 @@ -from StringIO import StringIO +from six import StringIO from django.test import TestCase, Client from django.http import HttpRequest, Http404 @@ -34,6 +34,27 @@ class TestPageQuerySet(TestCase): event = Page.objects.get(url_path='/home/events/someone-elses-event/') self.assertTrue(pages.filter(id=event.id).exists()) + def test_in_menu(self): + pages = Page.objects.in_menu() + + # All pages must be be in the menus + for page in pages: + self.assertTrue(page.show_in_menus) + + # Check that the events index is in the results + events_index = Page.objects.get(url_path='/home/events/') + self.assertTrue(pages.filter(id=events_index.id).exists()) + + def test_not_in_menu(self): + pages = Page.objects.not_in_menu() + + # All pages must not be in menus + for page in pages: + self.assertFalse(page.show_in_menus) + + # Check that the root page is in the results + self.assertTrue(pages.filter(id=1).exists()) + def test_page(self): homepage = Page.objects.get(url_path='/home/') pages = Page.objects.page(homepage) diff --git a/wagtail/wagtailcore/tests/test_rich_text.py b/wagtail/wagtailcore/tests/test_rich_text.py new file mode 100644 index 000000000..9c98c6d3f --- /dev/null +++ b/wagtail/wagtailcore/tests/test_rich_text.py @@ -0,0 +1,262 @@ +from mock import patch + +from django.test import TestCase + +from wagtail.wagtailcore.rich_text import ( + ImageEmbedHandler, + MediaEmbedHandler, + PageLinkHandler, + DocumentLinkHandler, + DbWhitelister, + extract_attrs, + expand_db_html +) +from bs4 import BeautifulSoup + + +class TestImageEmbedHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.b + result = ImageEmbedHandler.get_db_attributes(tag) + self.assertEqual(result, + {'alt': 'test-alt', + 'id': 'test-id', + 'format': 'test-format'}) + + def test_expand_db_attributes_page_does_not_exist(self): + result = ImageEmbedHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + @patch('wagtail.wagtailimages.models.Image') + @patch('django.core.files.File') + def test_expand_db_attributes_not_for_editor(self, mock_file, mock_image): + result = ImageEmbedHandler.expand_db_attributes( + {'id': 1, + 'alt': 'test-alt', + 'format': 'left'}, + False + ) + self.assertIn('foo' + ) + tag = soup.b + result = MediaEmbedHandler.get_db_attributes(tag) + self.assertEqual(result, + {'url': 'test-url'}) + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_attributes_for_editor(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + result = MediaEmbedHandler.expand_db_attributes( + {'url': 'http://www.youtube.com/watch/'}, + True + ) + self.assertIn('
      ', result) + self.assertIn('

      test title

      ', result) + self.assertIn('

      URL: http://www.youtube.com/watch/

      ', result) + self.assertIn('

      Provider: test provider name

      ', result) + self.assertIn('

      Author: test author name

      ', result) + self.assertIn('test title', result) + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_attributes_not_for_editor(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + result = MediaEmbedHandler.expand_db_attributes( + {'url': 'http://www.youtube.com/watch/'}, + False + ) + self.assertIn('test html', result) + + +class TestPageLinkHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + result = PageLinkHandler.get_db_attributes(tag) + self.assertEqual(result, + {'id': 'test-id'}) + + def test_expand_db_attributes_page_does_not_exist(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + def test_expand_db_attributes_for_editor(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 1}, + True + ) + self.assertEqual(result, + '') + + def test_expand_db_attributes_not_for_editor(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 1}, + False + ) + self.assertEqual(result, '') + + +class TestDocumentLinkHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + result = DocumentLinkHandler.get_db_attributes(tag) + self.assertEqual(result, + {'id': 'test-id'}) + + def test_expand_db_attributes_document_does_not_exist(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + def test_expand_db_attributes_for_editor(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 1}, + True + ) + self.assertEqual(result, + '') + + def test_expand_db_attributes_not_for_editor(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 1}, + False + ) + self.assertEqual(result, + '') + + +class TestDbWhiteLister(TestCase): + def test_clean_tag_node_div(self): + soup = BeautifulSoup( + '
      foo
      ' + ) + tag = soup.div + self.assertEqual(tag.name, 'div') + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(tag.name, 'p') + + def test_clean_tag_node_with_data_embedtype(self): + soup = BeautifulSoup( + '

      foo

      ' + ) + tag = soup.p + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), + '

      ') + + def test_clean_tag_node_with_data_linktype(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), 'foo') + + def test_clean_tag_node(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), 'foo') + + +class TestExtractAttrs(TestCase): + def test_extract_attr(self): + html = 'snowman' + result = extract_attrs(html) + self.assertEqual(result, {'foo': 'bar', 'baz': 'quux'}) + + +class TestExpandDbHtml(TestCase): + def test_expand_db_html_with_linktype(self): + html = 'foo' + result = expand_db_html(html) + self.assertEqual(result, 'foo') + + def test_expand_db_html_no_linktype(self): + html = 'foo' + result = expand_db_html(html) + self.assertEqual(result, 'foo') + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_html_with_embed(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + html = '' + result = expand_db_html(html) + self.assertIn('test html', result) diff --git a/wagtail/wagtailcore/tests/tests.py b/wagtail/wagtailcore/tests/tests.py index d10474bba..5fd5aa802 100644 --- a/wagtail/wagtailcore/tests/tests.py +++ b/wagtail/wagtailcore/tests/tests.py @@ -1,12 +1,7 @@ -from StringIO import StringIO +from django.test import TestCase -from django.test import TestCase, Client -from django.http import HttpRequest, Http404 -from django.core import management -from django.contrib.auth.models import User - -from wagtail.wagtailcore.models import Page, Site, UserPagePermissionsProxy -from wagtail.tests.models import EventPage, EventIndex, SimplePage +from wagtail.wagtailcore.models import Page, Site +from wagtail.tests.models import SimplePage class TestPageUrlTags(TestCase): @@ -15,22 +10,25 @@ class TestPageUrlTags(TestCase): def test_pageurl_tag(self): response = self.client.get('/events/') self.assertEqual(response.status_code, 200) - self.assertContains(response, 'Christmas') + self.assertContains(response, + 'Christmas') def test_slugurl_tag(self): response = self.client.get('/events/christmas/') self.assertEqual(response.status_code, 200) - self.assertContains(response, 'Back to events index') + self.assertContains(response, + 'Back to events index') class TestIssue7(TestCase): """ - This tests for an issue where if a site root page was moved, all the page - urls in that site would change to None. + This tests for an issue where if a site root page was moved, all + the page urls in that site would change to None. - The issue was caused by the 'wagtail_site_root_paths' cache variable not being - cleared when a site root page was moved. Which left all the child pages - thinking that they are no longer in the site and return None as their url. + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was moved. Which + left all the child pages thinking that they are no longer in the + site and return None as their url. Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 Discussion: https://github.com/torchbox/wagtail/issues/7 @@ -67,12 +65,13 @@ class TestIssue7(TestCase): class TestIssue157(TestCase): """ - This tests for an issue where if a site root pages slug was changed, all the page - urls in that site would change to None. + This tests for an issue where if a site root pages slug was + changed, all the page urls in that site would change to None. - The issue was caused by the 'wagtail_site_root_paths' cache variable not being - cleared when a site root page was changed. Which left all the child pages - thinking that they are no longer in the site and return None as their url. + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was changed. + Which left all the child pages thinking that they are no longer in + the site and return None as their url. Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 Discussion: https://github.com/torchbox/wagtail/issues/157 diff --git a/wagtail/wagtailcore/whitelist.py b/wagtail/wagtailcore/whitelist.py index a3d377bd6..4aaff780d 100644 --- a/wagtail/wagtailcore/whitelist.py +++ b/wagtail/wagtailcore/whitelist.py @@ -2,8 +2,9 @@ A generic HTML whitelisting engine, designed to accommodate subclassing to override specific rules. """ +from six.moves.urllib.parse import urlparse + from bs4 import BeautifulSoup, NavigableString, Tag -from urlparse import urlparse ALLOWED_URL_SCHEMES = ['', 'http', 'https', 'ftp', 'mailto', 'tel'] @@ -28,7 +29,7 @@ def attribute_rule(allowed_attrs): * if the lookup returns a truthy value, keep the attribute; if falsy, drop it """ def fn(tag): - for attr, val in tag.attrs.items(): + for attr, val in list(tag.attrs.items()): rule = allowed_attrs.get(attr) if rule: if callable(rule): @@ -65,7 +66,8 @@ class Whitelister(object): 'h6': allow_without_attributes, 'hr': allow_without_attributes, 'i': allow_without_attributes, - 'img': attribute_rule({'src': check_url, 'width': True, 'height': True, 'alt': True}), + 'img': attribute_rule({'src': check_url, 'width': True, 'height': True, + 'alt': True}), 'li': allow_without_attributes, 'ol': allow_without_attributes, 'p': allow_without_attributes, @@ -77,10 +79,11 @@ class Whitelister(object): @classmethod def clean(cls, html): - """Clean up an HTML string to contain just the allowed elements / attributes""" + """Clean up an HTML string to contain just the allowed elements / + attributes""" doc = BeautifulSoup(html, 'lxml') cls.clean_node(doc, doc) - return unicode(doc) + return doc.decode() @classmethod def clean_node(cls, doc, node): diff --git a/wagtail/wagtaildocs/models.py b/wagtail/wagtaildocs/models.py index 2149320f7..a87b9c02b 100644 --- a/wagtail/wagtaildocs/models.py +++ b/wagtail/wagtaildocs/models.py @@ -9,10 +9,12 @@ from django.dispatch import Signal from django.core.urlresolvers import reverse from django.conf import settings from django.utils.translation import ugettext_lazy as _ +from django.utils.encoding import python_2_unicode_compatible from wagtail.wagtailadmin.taggable import TagSearchable +@python_2_unicode_compatible class Document(models.Model, TagSearchable): title = models.CharField(max_length=255, verbose_name=_('Title')) file = models.FileField(upload_to='documents' , verbose_name=_('File')) @@ -30,7 +32,7 @@ class Document(models.Model, TagSearchable): }, } - def __unicode__(self): + def __str__(self): return self.title @property diff --git a/wagtail/wagtaildocs/templates/wagtaildocs/documents/add.html b/wagtail/wagtaildocs/templates/wagtaildocs/documents/add.html index c4466d897..f9a221bb3 100644 --- a/wagtail/wagtaildocs/templates/wagtaildocs/documents/add.html +++ b/wagtail/wagtaildocs/templates/wagtaildocs/documents/add.html @@ -1,6 +1,6 @@ {% extends "wagtailadmin/base.html" %} {% load i18n %} -{% load image_tags %} +{% load wagtailimages_tags %} {% block titletag %}{% trans "Add a document" %}{% endblock %} {% block bodyclass %}menu-documents{% endblock %} {% block extra_css %} diff --git a/wagtail/wagtaildocs/templates/wagtaildocs/documents/edit.html b/wagtail/wagtaildocs/templates/wagtaildocs/documents/edit.html index d27f16155..55734fbbd 100644 --- a/wagtail/wagtaildocs/templates/wagtaildocs/documents/edit.html +++ b/wagtail/wagtaildocs/templates/wagtaildocs/documents/edit.html @@ -1,6 +1,6 @@ {% extends "wagtailadmin/base.html" %} {% load i18n %} -{% load image_tags %} +{% load wagtailimages_tags %} {% block titletag %}{% blocktrans with title=document.title %}Editing {{ title }}{% endblocktrans %}{% endblock %} {% block bodyclass %}menu-documents{% endblock %} {% block extra_css %} diff --git a/wagtail/wagtaildocs/tests.py b/wagtail/wagtaildocs/tests.py index a8420406b..79b799a36 100644 --- a/wagtail/wagtaildocs/tests.py +++ b/wagtail/wagtaildocs/tests.py @@ -1,10 +1,14 @@ +from six import b + from django.test import TestCase -from wagtail.wagtaildocs import models -from wagtail.tests.utils import WagtailTestUtils + from django.contrib.auth.models import User, Group, Permission from django.core.urlresolvers import reverse from django.core.files.base import ContentFile +from wagtail.wagtaildocs import models +from wagtail.tests.utils import WagtailTestUtils + # TODO: Test serve view @@ -112,7 +116,7 @@ class TestDocumentAddView(TestCase, WagtailTestUtils): def test_post(self): # Build a fake file - fake_file = ContentFile("A boring example document") + fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit @@ -134,7 +138,7 @@ class TestDocumentEditView(TestCase, WagtailTestUtils): self.login() # Build a fake file - fake_file = ContentFile("A boring example document") + fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document to edit @@ -147,7 +151,7 @@ class TestDocumentEditView(TestCase, WagtailTestUtils): def test_post(self): # Build a fake file - fake_file = ContentFile("A boring example document") + fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit title change @@ -272,7 +276,7 @@ class TestDocumentChooserUploadView(TestCase, WagtailTestUtils): def test_post(self): # Build a fake file - fake_file = ContentFile("A boring example document") + fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit diff --git a/wagtail/wagtailembeds/embeds.py b/wagtail/wagtailembeds/embeds.py index 03029a23b..40bf48ec9 100644 --- a/wagtail/wagtailembeds/embeds.py +++ b/wagtail/wagtailembeds/embeds.py @@ -1,4 +1,6 @@ import sys +from datetime import datetime +import json try: from importlib import import_module @@ -6,13 +8,20 @@ except ImportError: # for Python 2.6, fall back on django.utils.importlib (deprecated as of Django 1.7) from django.utils.importlib import import_module + +# Needs to be imported like this to allow @patch to work in tests +from six.moves.urllib import request as urllib_request + +from six.moves.urllib.request import Request +from six.moves.urllib.error import URLError +from six.moves.urllib.parse import urlencode + from django.conf import settings -from datetime import datetime from django.utils import six + from wagtail.wagtailembeds.oembed_providers import get_oembed_provider from wagtail.wagtailembeds.models import Embed -import urllib2, urllib -import json + class EmbedNotFoundException(Exception): pass @@ -99,11 +108,11 @@ def oembed(url, max_width=None): params['maxwidth'] = max_width # Perform request - request = urllib2.Request(provider + '?' + urllib.urlencode(params)) + request = Request(provider + '?' + urlencode(params)) request.add_header('User-agent', 'Mozilla/5.0') try: - r = urllib2.urlopen(request) - except urllib2.URLError: + r = urllib_request.urlopen(request) + except URLError: raise EmbedNotFoundException oembed = json.loads(r.read()) @@ -162,6 +171,10 @@ def get_embed(url, max_width=None, finder=None): except (TypeError, ValueError): embed_dict['height'] = None + # Make sure html field is valid + if 'html' not in embed_dict or not embed_dict['html']: + embed_dict['html'] = '' + # Create database record embed, created = Embed.objects.get_or_create( url=url, diff --git a/wagtail/wagtailembeds/format.py b/wagtail/wagtailembeds/format.py index 1654be989..8a73ff524 100644 --- a/wagtail/wagtailembeds/format.py +++ b/wagtail/wagtailembeds/format.py @@ -1,6 +1,5 @@ from __future__ import division # Use true division -from django.utils.html import escape from django.template.loader import render_to_string from wagtail.wagtailembeds import get_embed diff --git a/wagtail/wagtailembeds/models.py b/wagtail/wagtailembeds/models.py index c147860f0..4f61c19d7 100644 --- a/wagtail/wagtailembeds/models.py +++ b/wagtail/wagtailembeds/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils.encoding import python_2_unicode_compatible EMBED_TYPES = ( @@ -9,6 +10,7 @@ EMBED_TYPES = ( ) +@python_2_unicode_compatible class Embed(models.Model): url = models.URLField() max_width = models.SmallIntegerField(null=True, blank=True) @@ -25,5 +27,5 @@ class Embed(models.Model): class Meta: unique_together = ('url', 'max_width') - def __unicode__(self): + def __str__(self): return self.url diff --git a/wagtail/wagtailembeds/templates/wagtailembeds/chooser/chooser.html b/wagtail/wagtailembeds/templates/wagtailembeds/chooser/chooser.html index b914f9c6b..fbc28c425 100644 --- a/wagtail/wagtailembeds/templates/wagtailembeds/chooser/chooser.html +++ b/wagtail/wagtailembeds/templates/wagtailembeds/chooser/chooser.html @@ -1,4 +1,4 @@ -{% load image_tags %} +{% load wagtailimages_tags %} {% load i18n %} {% trans "Insert embed" as ins_emb_str %} {% include "wagtailadmin/shared/header.html" with title=ins_emb_str merged=1 %} diff --git a/wagtail/wagtailembeds/templatetags/embed_filters.py b/wagtail/wagtailembeds/templatetags/embed_filters.py index d916c0ffb..fcc4c2773 100644 --- a/wagtail/wagtailembeds/templatetags/embed_filters.py +++ b/wagtail/wagtailembeds/templatetags/embed_filters.py @@ -1,24 +1,8 @@ -from django import template -from django.utils.safestring import mark_safe +import warnings -from wagtail.wagtailembeds import get_embed +warnings.warn( + "The embed_filters tag library has been moved to wagtailembeds_tags. " + "Use {% load wagtailembeds_tags %} instead.", DeprecationWarning) -register = template.Library() - - -@register.filter -def embed(url, max_width=None): - embed = get_embed(url, max_width=max_width) - try: - if embed is not None: - return mark_safe(embed.html) - else: - return '' - except: - return '' - - -@register.filter -def embedly(url, max_width=None): - return embed(url, max_width) +from wagtail.wagtailembeds.templatetags.wagtailembeds_tags import register, embed, embedly diff --git a/wagtail/wagtailembeds/templatetags/wagtailembeds_tags.py b/wagtail/wagtailembeds/templatetags/wagtailembeds_tags.py new file mode 100644 index 000000000..1ad63cddf --- /dev/null +++ b/wagtail/wagtailembeds/templatetags/wagtailembeds_tags.py @@ -0,0 +1,30 @@ +import warnings + +from django import template +from django.utils.safestring import mark_safe + +from wagtail.wagtailembeds import get_embed + + +register = template.Library() + + +@register.filter +def embed(url, max_width=None): + embed = get_embed(url, max_width=max_width) + try: + if embed is not None: + return mark_safe(embed.html) + else: + return '' + except: + return '' + + +@register.filter +def embedly(url, max_width=None): + warnings.warn( + "The 'embedly' filter has been renamed. " + "Use 'embed' instead.", DeprecationWarning) + + return embed(url, max_width) diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py index 892dde5bd..2c68ef744 100644 --- a/wagtail/wagtailembeds/tests.py +++ b/wagtail/wagtailembeds/tests.py @@ -1,5 +1,8 @@ +from six.moves.urllib.request import urlopen +import six.moves.urllib.request +from six.moves.urllib.error import URLError + from mock import patch -import urllib2 try: import embedly @@ -7,6 +10,7 @@ try: except ImportError: no_embedly = True +from django import template from django.test import TestCase from wagtail.tests.utils import WagtailTestUtils, unittest @@ -18,7 +22,7 @@ from wagtail.wagtailembeds.embeds import ( AccessDeniedEmbedlyException, ) from wagtail.wagtailembeds.embeds import embedly as wagtail_embedly, oembed as wagtail_oembed - +from wagtail.wagtailembeds.templatetags.wagtailembeds_tags import embed as embed_filter, embedly as embedly_filter class TestEmbeds(TestCase): @@ -79,6 +83,19 @@ class TestEmbeds(TestCase): # Width must be set to None self.assertEqual(embed.width, None) + def test_no_html(self) : + def no_html_finder(url, max_width=None): + """ + A finder which returns everything but HTML + """ + embed = self.dummy_finder(url, max_width) + embed['html'] = None + return embed + + embed = get_embed('www.test.com/1234', max_width=400, finder=no_html_finder) + + self.assertEqual(embed.html, '') + class TestChooser(TestCase, WagtailTestUtils): def setUp(self): @@ -203,12 +220,12 @@ class TestOembed(TestCase): self.assertRaises(EmbedNotFoundException, wagtail_oembed, "foo") def test_oembed_invalid_request(self): - config = {'side_effect': urllib2.URLError('foo')} - with patch.object(urllib2, 'urlopen', **config) as urlopen: + config = {'side_effect': URLError('foo')} + with patch.object(six.moves.urllib.request, 'urlopen', **config) as urlopen: self.assertRaises(EmbedNotFoundException, wagtail_oembed, "http://www.youtube.com/watch/") - @patch('urllib2.urlopen') + @patch('six.moves.urllib.request.urlopen') @patch('json.loads') def test_oembed_photo_request(self, loads, urlopen) : urlopen.return_value = self.dummy_response @@ -219,7 +236,7 @@ class TestOembed(TestCase): self.assertEqual(result['html'], '') loads.assert_called_with("foo") - @patch('urllib2.urlopen') + @patch('six.moves.urllib.request.urlopen') @patch('json.loads') def test_oembed_return_values(self, loads, urlopen): urlopen.return_value = self.dummy_response @@ -245,3 +262,81 @@ class TestOembed(TestCase): 'height': 'test_height', 'html': 'test_html' }) + + +class TestEmbedFilter(TestCase): + def setUp(self): + class DummyResponse(object): + def read(self): + return "foo" + self.dummy_response = DummyResponse() + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_valid_embed(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + result = embed_filter('http://www.youtube.com/watch/') + self.assertEqual(result, '') + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_render_filter(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + temp = template.Template('{% load wagtailembeds_tags %}{{ "http://www.youtube.com/watch/"|embed }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_render_filter_nonexistent_type(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'foo', + 'url': 'http://www.example.com'} + temp = template.Template('{% load wagtailembeds_tags %}{{ "http://www.youtube.com/watch/"|embed }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') + + +class TestEmbedlyFilter(TestEmbedFilter): + def setUp(self): + class DummyResponse(object): + def read(self): + return "foo" + self.dummy_response = DummyResponse() + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_valid_embed(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + result = embedly_filter('http://www.youtube.com/watch/') + self.assertEqual(result, '') + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_render_filter(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embedly }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') + + @patch('six.moves.urllib.request.urlopen') + @patch('json.loads') + def test_render_filter_nonexistent_type(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'foo', + 'url': 'http://www.example.com'} + temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embedly }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') diff --git a/wagtail/wagtailforms/forms.py b/wagtail/wagtailforms/forms.py index f17023a77..4f874133a 100644 --- a/wagtail/wagtailforms/forms.py +++ b/wagtail/wagtailforms/forms.py @@ -8,22 +8,9 @@ class BaseForm(django.forms.Form): return super(BaseForm, self).__init__(*args, **kwargs) -class FormBuilder(): - formfields = SortedDict() - +class FormBuilder(object): def __init__(self, fields): - for field in fields: - options = self.get_options(field) - f = getattr(self, "create_"+field.field_type+"_field")(field, options) - self.formfields[field.clean_name] = f - - def get_options(self, field): - options = {} - options['label'] = field.label - options['help_text'] = field.help_text - options['required'] = field.required - options['initial'] = field.default_value - return options + self.fields = fields def create_singleline_field(self, field, options): # TODO: This is a default value - it may need to be changed @@ -72,16 +59,52 @@ class FormBuilder(): def create_checkbox_field(self, field, options): return django.forms.BooleanField(**options) + FIELD_TYPES = { + 'singleline': create_singleline_field, + 'multiline': create_multiline_field, + 'date': create_date_field, + 'datetime': create_datetime_field, + 'email': create_email_field, + 'url': create_url_field, + 'number': create_number_field, + 'dropdown': create_dropdown_field, + 'radio': create_radio_field, + 'checkboxes': create_checkboxes_field, + 'checkbox': create_checkbox_field, + } + + @property + def formfields(self): + formfields = SortedDict() + + for field in self.fields: + options = self.get_field_options(field) + + if field.field_type in self.FIELD_TYPES: + formfields[field.clean_name] = self.FIELD_TYPES[field.field_type](self, field, options) + else: + raise Exception("Unrecognised field type: " + form.field_type) + + return formfields + + def get_field_options(self, field): + options = {} + options['label'] = field.label + options['help_text'] = field.help_text + options['required'] = field.required + options['initial'] = field.default_value + return options + def get_form_class(self): return type('WagtailForm', (BaseForm,), self.formfields) class SelectDateForm(django.forms.Form): - date_from = django.forms.DateField( + date_from = django.forms.DateTimeField( required=False, widget=django.forms.DateInput(attrs={'placeholder': 'Date from'}) ) - date_to = django.forms.DateField( + date_to = django.forms.DateTimeField( required=False, widget=django.forms.DateInput(attrs={'placeholder': 'Date to'}) ) diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py index 75f0eceee..41414d391 100644 --- a/wagtail/wagtailforms/models.py +++ b/wagtail/wagtailforms/models.py @@ -1,11 +1,15 @@ +import json +import re + +from six import text_type + +from unidecode import unidecode + from django.db import models from django.shortcuts import render from django.utils.translation import ugettext_lazy as _ from django.utils.text import slugify - -from unidecode import unidecode -import json -import re +from django.utils.encoding import python_2_unicode_compatible from wagtail.wagtailcore.models import Page, Orderable, UserPagePermissionsProxy, get_page_types from wagtail.wagtailadmin.edit_handlers import FieldPanel @@ -32,6 +36,7 @@ FORM_FIELD_CHOICES = ( HTML_EXTENSION_RE = re.compile(r"(.*)\.html") +@python_2_unicode_compatible class FormSubmission(models.Model): """Data for a Form submission.""" @@ -43,7 +48,7 @@ class FormSubmission(models.Model): def get_data(self): return json.loads(self.form_data) - def __unicode__(self): + def __str__(self): return self.form_data @@ -73,7 +78,7 @@ class AbstractFormField(Orderable): # unidecode will return an ascii string while slugify wants a # unicode string on the other hand, slugify returns a safe-string # which will be converted to a normal str - return str(slugify(unicode(unidecode(self.label)))) + return str(slugify(text_type(unidecode(self.label)))) panels = [ FieldPanel('label'), diff --git a/wagtail/wagtailforms/templates/wagtailforms/index_submissions.html b/wagtail/wagtailforms/templates/wagtailforms/index_submissions.html index ae9572295..8e166111b 100644 --- a/wagtail/wagtailforms/templates/wagtailforms/index_submissions.html +++ b/wagtail/wagtailforms/templates/wagtailforms/index_submissions.html @@ -39,7 +39,7 @@