From: Patrick Samson Date: Sun, 21 Jul 2013 18:36:23 +0000 (+0200) Subject: Convert all function-based views to class-based views X-Git-Tag: 3.0.0 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=b3c7be8377dfe2341c73b01351ebdc5dbac9061e;p=django-postman.git Convert all function-based views to class-based views --- diff --git a/CHANGELOG b/CHANGELOG index ef98f13..02a64b6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,7 @@ Version 3.0.0, July 2013 to fix the performances problem of issue #15. Note that the counting of messages by thread is no more global (all folders) but is now limited to the only targeted folder. +* Convert all function-based views to class-based views. * Extend the support of django-notification from version 0.2.0 to 1.0. * Avoid the 'Enter text to search.' help text imposed in version 1.2.5 of ajax_select. diff --git a/docs/conf.py b/docs/conf.py index 63e7ec4..c704077 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -47,7 +47,7 @@ copyright = u'2010, Patrick Samson' # The short X.Y version. version = '3.0' # The full version, including alpha/beta/rc tags. -release = '3.0.0a1' +release = '3.0.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/features.rst b/docs/features.rst index 4b44527..0167ab8 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -35,7 +35,7 @@ you can pass the optional ``max`` parameter to the view. There is no parameter for a minimum number, but you can code a custom form and pass a ``min`` parameter to the recipient field (see Advanced Usage below for details). -Views supporting the parameter are: ``write``, ``reply``. +Views supporting the parameter are: ``WriteView``, ``ReplyView``. But this parameter does not apply to the default ``AnonymousWriteForm`` for visitors: The maximum is enforced to 1 (see Advanced Usage below for knowing how), @@ -45,8 +45,9 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'max': 3}, name='postman_write'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(max=3), + name='postman_write'), # ... ) @@ -81,7 +82,7 @@ User filter If there are some situations where a user should not be a recipient, you can write a filter and pass it to the view. -Views supporting a user filter are: ``write``, ``reply``. +Views supporting a user filter are: ``WriteView``, ``ReplyView``. Example:: @@ -92,8 +93,9 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'user_filter': my_user_filter}, name='postman_write'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(user_filter=my_user_filter), + name='postman_write'), # ... ) @@ -139,7 +141,7 @@ If there are some situations where an exchange should not take place, you can wr and pass it to the view. Typical usages would be: blacklists, users that do not want solicitation from visitors. -Views supporting an exchange filter are: ``write``, ``reply``. +Views supporting an exchange filter are: ``WriteView``, ``ReplyView``. An example, with the django-relationships application:: @@ -150,8 +152,9 @@ An example, with the django-relationships application:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'exchange_filter': my_exchange_filter}, name='postman_write'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(exchange_filter=my_exchange_filter), + name='postman_write'), # ... ) @@ -242,26 +245,28 @@ Customization You may attach a specific channel, different from the default one, to a particular view. -Views supporting an auto-complete parameter are: ``write``, ``reply``. +Views supporting an auto-complete parameter are: ``WriteView``, ``ReplyView``. -For the ``write`` view, the parameter is named ``autocomplete_channels`` (note the plural). +For the ``WriteView`` view, the parameter is named ``autocomplete_channels`` (note the plural). It supports two variations: * a 2-tuple of channels names: the first one for authenticated users, the second for visitors. Specify ``None`` if you let the default channel name for one of the tuple parts. * a single channel name: the same for users and visitors -For the ``reply`` view, the parameter is named ``autocomplete_channel`` (note the singular). +For the ``ReplyView`` view, the parameter is named ``autocomplete_channel`` (note the singular). The value is the channel name. Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'autocomplete_channels': (None,'anonymous_ac')}, name='postman_write'), - url(r'^reply/(?P[\d]+)/$', 'reply', - {'autocomplete_channel': 'reply_ac'}, name='postman_reply'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(autocomplete_channels=(None,'anonymous_ac')), + name='postman_write'), + url(r'^reply/(?P[\d]+)/$', + ReplyView.as_view(autocomplete_channel='reply_ac'), + name='postman_reply'), # ... ) @@ -269,8 +274,9 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'autocomplete_channels': 'write_ac'}, name='postman_write'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(autocomplete_channels='write_ac'), + name='postman_write'), # ... ) diff --git a/docs/moderation.rst b/docs/moderation.rst index 92de994..e133324 100644 --- a/docs/moderation.rst +++ b/docs/moderation.rst @@ -21,7 +21,7 @@ You may automate the moderation by giving zero, one, or many auto-moderator func to the views. The value of the parameter can be one single function or a sequence of functions as a tuple or a list. -Views supporting an ``auto-moderators`` parameter are: ``write``, ``reply``. +Views supporting an ``auto-moderators`` parameter are: ``WriteView``, ``ReplyView``. Example:: @@ -36,10 +36,12 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'auto_moderators': (mod1, mod2)}, name='postman_write'), - url(r'^reply/(?P[\d]+)/$', 'reply', - {'auto_moderators': mod1}, name='postman_reply'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(auto_moderators=(mod1, mod2)), + name='postman_write'), + url(r'^reply/(?P[\d]+)/$', + ReplyView.as_view(auto_moderators=mod1), + name='postman_reply'), # ... ) @@ -78,5 +80,3 @@ At the end of the loop, if the decision is not final, the sequence is: #. An average rating is computed: if greater or equal to 50, the message is accepted. #. The message is rejected. The final reason is a comma separated collection of reasons coming from moderators having returned a rating lesser than 50. - - diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 62666c8..221b85e 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -12,11 +12,11 @@ Some reasons: * use of ``str.format()`` -Django version >= 1.2.2 +Django version >= 1.3 Some reasons: -* use of ``self.stdout`` in management commands +* use of class-based views Installation ------------ @@ -157,11 +157,6 @@ Permute the comment tags for the lines denoted by the marks: {# dj v1.x #} in: * base_write.html -In case you run a Django 1.2 version, perform these additional steps for any template: - -* Remove {% load url from future %} -* Change any {% url 'XX' %} to {% url XX %} - Relations between templates:: base.html @@ -213,18 +208,6 @@ See also :ref:`styles` for the stylesheets of views. For Django 1.3+, just follow the instructions related to the staticfiles app. -For Django 1.2: - It's up to you to make the files visible to the URL resolver. - - For example: - - * Rename the path to :file:`postman/medias/` - * In a production environment, set :file:`//postman/` as a symlink to :file:`/medias/postman/` - * In a development environment (django's runserver), you can put in the URLconf, something like:: - - ('^' + settings.MEDIA_URL.strip('/') + r'/(?Ppostman/.*)$', 'django.views.static.serve', - {'document_root': os.path.join(imp.find_module('postman')[1], 'medias')}), - Examples -------- diff --git a/docs/views.rst b/docs/views.rst index f1b4454..5364199 100644 --- a/docs/views.rst +++ b/docs/views.rst @@ -25,12 +25,15 @@ Examples:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', 'write', - {'form_classes': (MyCustomWriteForm, MyCustomAnonymousWriteForm)}, name='postman_write'), - url(r'^reply/(?P[\d]+)/$', 'reply', - {'form_class': MyCustomFullReplyForm}, name='postman_reply'), - url(r'^view/(?P[\d]+)/$', 'view', - {'form_class': MyCustomQuickReplyForm}, name='postman_view'), + url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)), + name='postman_write'), + url(r'^reply/(?P[\d]+)/$', + ReplyView.as_view(form_class=MyCustomFullReplyForm), + name='postman_reply'), + url(r'^view/(?P[\d]+)/$', + MessageView.as_view(form_class=MyCustomQuickReplyForm), + name='postman_view'), # ... ) @@ -43,8 +46,9 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^view/(?P[\d]+)/$', 'view', - {'template_name': 'my_custom_view.html'}, name='postman_view'), + url(r'^view/(?P[\d]+)/$', + MessageView.as_view(template_name='my_custom_view.html'), + name='postman_view'), # ... ) @@ -62,18 +66,19 @@ The default algorithm is: The parameter ``success_url`` is available to these views: -* ``write`` -* ``reply`` -* ``archive`` -* ``delete`` -* ``undelete`` +* ``WriteView`` +* ``ReplyView`` +* ``ArchiveView`` +* ``DeleteView`` +* ``UndeleteView`` Example:: urlpatterns = patterns('postman.views', # ... - url(r'^reply/(?P[\d]+)/$', 'reply', - {'success_url': 'postman_inbox'}, name='postman_reply'), + url(r'^reply/(?P[\d]+)/$', + ReplyView.as_view(success_url='postman_inbox'), + name='postman_reply'), # ... ) @@ -96,9 +101,11 @@ Examples:: urlpatterns = patterns('postman.views', # ... - url(r'^reply/(?P[\d]+)/$', 'reply', - {'formatters': (format_subject,format_body)}, name='postman_reply'), - url(r'^view/(?P[\d]+)/$', 'view', - {'formatters': (format_subject,format_body)}, name='postman_view'), + url(r'^reply/(?P[\d]+)/$', + ReplyView.as_view(formatters=(format_subject, format_body)), + name='postman_reply'), + url(r'^view/(?P[\d]+)/$', + MessageView.as_view(formatters=(format_subject, format_body)), + name='postman_view'), # ... ) diff --git a/postman/__init__.py b/postman/__init__.py index eef3e20..0d02e1b 100644 --- a/postman/__init__.py +++ b/postman/__init__.py @@ -5,10 +5,14 @@ from __future__ import unicode_literals # following PEP 386: N.N[.N]+[{a|b|c|rc}N[.N]+][.postN][.devN] VERSION = (3, 0, 0) -PREREL = ('a', 1) +PREREL = () POST = 0 DEV = 0 +# options +OPTION_MESSAGES = 'm' +OPTIONS = OPTION_MESSAGES # may be extended in future + def get_version(): version = '.'.join(map(str, VERSION)) diff --git a/postman/models.py b/postman/models.py index fbe514b..cbacb82 100644 --- a/postman/models.py +++ b/postman/models.py @@ -13,16 +13,16 @@ try: from django.utils.text import Truncator # Django 1.4 except ImportError: from postman.future_1_4 import Truncator -from django.utils.translation import ugettext, ugettext_lazy as _ try: from django.utils.timezone import now # Django 1.4 aware datetimes except ImportError: from datetime import datetime now = datetime.now +from django.utils.translation import ugettext, ugettext_lazy as _ -from postman.query import PostmanQuery -from postman.urls import OPTION_MESSAGES -from postman.utils import email_visitor, notify_user +from . import OPTION_MESSAGES +from .query import PostmanQuery +from .utils import email_visitor, notify_user # moderation constants STATUS_PENDING = 'p' diff --git a/postman/test_urls.py b/postman/test_urls.py index dff34ca..3aaca9c 100644 --- a/postman/test_urls.py +++ b/postman/test_urls.py @@ -9,14 +9,13 @@ try: from django.conf.urls import patterns, include, url # django 1.4 except ImportError: from django.conf.urls.defaults import * # "patterns, include, url" is enough for django 1.3, "*" for django 1.2 -try: - from django.contrib.auth import get_user_model # Django 1.5 -except ImportError: - from postman.future_1_5 import get_user_model from django.forms import ValidationError from django.views.generic.base import RedirectView -from postman.urls import OPTIONS +from . import OPTIONS +from .views import (InboxView, SentView, ArchivesView, TrashView, + WriteView, ReplyView, MessageView, ConversationView, + ArchiveView, DeleteView, UndeleteView) # user_filter function set @@ -62,67 +61,67 @@ def format_body(sender, body): postman_patterns = patterns('postman.views', # Basic set - url(r'^inbox/(?:(?P