Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Some Views as CBV #487

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions userena/contrib/umessages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from userena.contrib.umessages.models import Message, MessageRecipient, MessageContact
from userena.contrib.umessages.forms import ComposeForm
from userena.utils import get_datetime_now, get_user_model
from userena.utils import get_datetime_now, get_user_model, get_request_field
from userena import settings as userena_settings


Expand Down Expand Up @@ -115,17 +115,14 @@ def message_compose(request, recipients=None, compose_form=ComposeForm,
if request.method == "POST":
form = compose_form(request.POST)
if form.is_valid():
requested_redirect = request.REQUEST.get("next", False)

message = form.save(request.user)
recipients = form.cleaned_data['to']

if userena_settings.USERENA_USE_MESSAGES:
messages.success(request, _('Message is sent.'),
fail_silently=True)

requested_redirect = request.REQUEST.get(REDIRECT_FIELD_NAME,
False)
requested_redirect = get_request_field(request, REDIRECT_FIELD_NAME, False)

# Redirect mechanism
redirect_to = reverse('userena_umessages_list')
Expand Down Expand Up @@ -163,7 +160,7 @@ def message_remove(request, undo=False):

"""
message_pks = request.POST.getlist('message_pks')
redirect_to = request.REQUEST.get('next', False)
redirect_to = get_request_field(request, "next", False)

if message_pks:
# Check that all values are integers.
Expand Down
14 changes: 14 additions & 0 deletions userena/runtests/urls_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.conf.urls import *

from userena import views

PROFILE = [
# View profiles
url(r'^(?P<username>(?!signout|signup|signin)[\@\.\w-]+)/$',
views.profile_detail,
name='userena_profile_detail'),
]

urlpatterns = patterns('',
url(r'^accounts/', include(PROFILE)),
)
3 changes: 2 additions & 1 deletion userena/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
from .tests_middleware import *
from .tests_models import *
from .tests_utils import *
from .tests_views import *
from .tests_views import *
from .tests_views_with_new_urls import *
31 changes: 31 additions & 0 deletions userena/tests/tests_views_with_new_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import re

from datetime import datetime, timedelta
from django.core.urlresolvers import reverse
from django.core import mail
from django.contrib.auth.forms import PasswordChangeForm
from django.test import TestCase

try:
from django.test import override_settings
except Exception, e:
from django.test.utils import override_settings

from userena import forms
from userena import settings as userena_settings
from userena.utils import get_user_model, get_user_profile

User = get_user_model()


@override_settings(ROOT_URLCONF='urls_change')
class UserenaViewsTests(TestCase):
""" Test the account views """
fixtures = ['users', 'profiles']

def test_profile_detail_view(self):
""" A ``GET`` to the detailed view of a user """
response = self.client.get(reverse('userena_profile_detail',
kwargs={'username': 'john'}))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'userena/profile_detail.html')
12 changes: 6 additions & 6 deletions userena/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def merged_dict(dict_a, dict_b):
urlpatterns = patterns('',
# Signup, signin and signout
url(r'^signup/$',
userena_views.signup,
userena_views.SignupView.as_view(),
name='userena_signup'),
url(r'^signin/$',
userena_views.signin,
userena_views.SigninView.as_view(),
name='userena_signin'),
url(r'^signout/$',
userena_views.signout,
Expand Down Expand Up @@ -67,7 +67,7 @@ def merged_dict(dict_a, dict_b):

# Change email and confirm it
url(r'^(?P<username>[\@\.\w-]+)/email/$',
userena_views.email_change,
userena_views.EmailChangeView.as_view(),
name='userena_email_change'),
url(r'^(?P<username>[\@\.\w-]+)/email/complete/$',
userena_views.direct_to_user_template,
Expand All @@ -89,7 +89,7 @@ def merged_dict(dict_a, dict_b):

# Change password
url(r'^(?P<username>[\@\.\w-]+)/password/$',
userena_views.password_change,
userena_views.PasswordChangeView.as_view(),
name='userena_password_change'),
url(r'^(?P<username>[\@\.\w-]+)/password/complete/$',
userena_views.direct_to_user_template,
Expand All @@ -98,12 +98,12 @@ def merged_dict(dict_a, dict_b):

# Edit profile
url(r'^(?P<username>[\@\.\w-]+)/edit/$',
userena_views.profile_edit,
userena_views.ProfileEditView.as_view(),
name='userena_profile_edit'),

# View profiles
url(r'^(?P<username>(?!signout|signup|signin)[\@\.\w-]+)/$',
userena_views.profile_detail,
userena_views.ProfileDetailView.as_view(),
name='userena_profile_detail'),
url(r'^page/(?P<page>[0-9]+)/$',
userena_views.ProfileListView.as_view(),
Expand Down
27 changes: 27 additions & 0 deletions userena/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,30 @@ def get_datetime_now():
except ImportError:
def get_user_model():
return get_model(*user_model_label.rsplit('.', 1))


def get_request_field(request, name, default=None):

"""
This method try to replace request.REQUEST because was depreced
in django 1.7

Try to get a field in http (GET, POST) methods

:param request:
request.

:param name:
String defining the name of the field to get.

:param default:
Var which contains the default value to return when the field is not in
the request. Defaults to ``None``
"""

for method_name in ("GET", "POST"):
method = getattr(request, method_name)
field = method.get(name, None)
if field:
break
return field if field else default
Loading