mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-03-16 21:30:24 +00:00
Drop all code related to deprecated versions of Python and Django
This commit is contained in:
parent
041281bced
commit
ab82bcb1d4
10 changed files with 45 additions and 171 deletions
|
|
@ -1,9 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# pylint: disable=too-many-lines
|
||||
from distutils.version import \
|
||||
StrictVersion # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
from django import get_version
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import Group
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
|
@ -17,18 +13,9 @@ from notifications import settings as notifications_settings
|
|||
from notifications.signals import notify
|
||||
from notifications.utils import id2slug
|
||||
from swapper import load_model
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
|
||||
if StrictVersion(get_version()) >= StrictVersion('1.8.0'):
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey # noqa
|
||||
else:
|
||||
from django.contrib.contenttypes.generic import GenericForeignKey # noqa
|
||||
|
||||
try:
|
||||
# Django >= 1.7
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
except ImportError:
|
||||
# Django <= 1.6
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
EXTRA_DATA = notifications_settings.get_config()['USE_JSONFIELD']
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from swapper import swappable_setting
|
||||
|
||||
from .base.models import AbstractNotification, notify_handler # noqa
|
||||
from .base.models import AbstractNotification
|
||||
|
||||
|
||||
class Notification(AbstractNotification):
|
||||
|
|
@ -8,16 +8,16 @@ class Notification(AbstractNotification):
|
|||
class Meta(AbstractNotification.Meta):
|
||||
abstract = False
|
||||
swappable = swappable_setting('notifications', 'Notification')
|
||||
|
||||
|
||||
def naturalday(self):
|
||||
"""
|
||||
Shortcut for the ``humanize``.
|
||||
Take a parameter humanize_type. This parameter control the which humanize method use.
|
||||
Return ``today``, ``yesterday`` ,``now``, ``2 seconds ago``etc.
|
||||
Return ``today``, ``yesterday`` ,``now``, ``2 seconds ago``etc.
|
||||
"""
|
||||
from django.contrib.humanize.templatetags.humanize import naturalday
|
||||
return naturalday(self.timestamp)
|
||||
|
||||
def naturaltime(self):
|
||||
from django.contrib.humanize.templatetags.humanize import naturaltime
|
||||
return naturaltime(self.timestamp)
|
||||
return naturaltime(self.timestamp)
|
||||
|
|
|
|||
|
|
@ -1,18 +1,10 @@
|
|||
''' Django notifications template tags file '''
|
||||
# -*- coding: utf-8 -*-
|
||||
from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
from django import get_version
|
||||
from django.template import Library
|
||||
from django.utils.html import format_html
|
||||
from django.core.cache import cache
|
||||
from notifications import settings
|
||||
from notifications.settings import get_config
|
||||
|
||||
try:
|
||||
from django.urls import reverse
|
||||
except ImportError:
|
||||
from django.core.urlresolvers import reverse # pylint: disable=no-name-in-module,import-error
|
||||
from django.urls import reverse
|
||||
|
||||
register = Library()
|
||||
|
||||
|
|
@ -25,6 +17,7 @@ def get_cached_notification_unread_count(user):
|
|||
settings.get_config()['CACHE_TIMEOUT']
|
||||
)
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def notifications_unread(context):
|
||||
user = user_context(context)
|
||||
if not user:
|
||||
|
|
@ -32,12 +25,6 @@ def notifications_unread(context):
|
|||
return get_cached_notification_unread_count(user)
|
||||
|
||||
|
||||
if StrictVersion(get_version()) >= StrictVersion('2.0'):
|
||||
notifications_unread = register.simple_tag(takes_context=True)(notifications_unread) # pylint: disable=invalid-name
|
||||
else:
|
||||
notifications_unread = register.assignment_tag(takes_context=True)(notifications_unread) # noqa
|
||||
|
||||
|
||||
@register.filter
|
||||
def has_notification(user):
|
||||
if user:
|
||||
|
|
@ -118,11 +105,7 @@ def user_context(context):
|
|||
|
||||
request = context['request']
|
||||
user = request.user
|
||||
try:
|
||||
user_is_anonymous = user.is_anonymous()
|
||||
except TypeError: # Django >= 1.11
|
||||
user_is_anonymous = user.is_anonymous
|
||||
|
||||
if user_is_anonymous:
|
||||
if user.is_anonymous:
|
||||
return None
|
||||
return user
|
||||
|
|
|
|||
|
|
@ -6,12 +6,6 @@ import django.db.models.deletion
|
|||
import django.utils.timezone
|
||||
|
||||
|
||||
try:
|
||||
from jsonfield.fields import JSONField
|
||||
except ModuleNotFoundError:
|
||||
JSONField = models.JSONField
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
|
@ -37,7 +31,7 @@ class Migration(migrations.Migration):
|
|||
('public', models.BooleanField(db_index=True, default=True)),
|
||||
('deleted', models.BooleanField(db_index=True, default=False)),
|
||||
('emailed', models.BooleanField(db_index=True, default=False)),
|
||||
('data', JSONField(blank=True, null=True)),
|
||||
('data', models.JSONField(blank=True, null=True)),
|
||||
('details', models.CharField(blank=True, max_length=64, null=True)),
|
||||
('action_object_content_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notify_action_object', to='contenttypes.ContentType')),
|
||||
('actor_content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notify_actor', to='contenttypes.ContentType')),
|
||||
|
|
|
|||
|
|
@ -16,16 +16,12 @@ DATABASES = {
|
|||
}
|
||||
}
|
||||
|
||||
# Django < 2.0
|
||||
MIDDLEWARE_CLASSES = (
|
||||
MIDDLEWARE = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware'
|
||||
)
|
||||
|
||||
# Django >= 2.0
|
||||
MIDDLEWARE = MIDDLEWARE_CLASSES
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
|
|
@ -78,4 +74,4 @@ if os.environ.get('SAMPLE_APP', False):
|
|||
NOTIFICATIONS_NOTIFICATION_MODEL = 'sample_notifications.Notification'
|
||||
TEMPLATES[0]['DIRS'] += [os.path.join(BASE_DIR, '../templates')]
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
ALLOWED_HOSTS = ["*"]
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@ from django.conf import settings
|
|||
from django.contrib.auth.models import Group, User
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import connection
|
||||
from django.test import override_settings # noqa
|
||||
from django.template import Context, Template
|
||||
from django.test import Client, RequestFactory, TestCase
|
||||
from django.test import RequestFactory, TestCase
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.timezone import localtime, utc
|
||||
from notifications.base.models import notify_handler
|
||||
|
|
@ -25,19 +27,6 @@ from swapper import load_model
|
|||
|
||||
Notification = load_model('notifications', 'Notification')
|
||||
|
||||
try:
|
||||
# Django >= 1.7
|
||||
from django.test import override_settings # noqa
|
||||
except ImportError:
|
||||
# Django <= 1.6
|
||||
from django.test.utils import override_settings # noqa
|
||||
|
||||
try:
|
||||
# Django >= 1.7
|
||||
from django.urls import reverse
|
||||
except ImportError:
|
||||
# Django <= 1.6
|
||||
from django.core.urlresolvers import reverse # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
MALICIOUS_NEXT_URLS = [
|
||||
"http://bla.com",
|
||||
|
|
|
|||
|
|
@ -1,39 +1,15 @@
|
|||
''' Django notification urls for tests '''
|
||||
# -*- coding: utf-8 -*-
|
||||
from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
from django import get_version
|
||||
from django.contrib import admin
|
||||
from notifications.tests.views import (live_tester, # pylint: disable=no-name-in-module,import-error
|
||||
make_notification)
|
||||
from notifications.tests.views import live_tester, make_notification
|
||||
from django.urls import include, path
|
||||
from django.contrib.auth.views import LoginView
|
||||
|
||||
if StrictVersion(get_version()) >= StrictVersion('2.1'):
|
||||
from django.urls import include, path # noqa
|
||||
from django.contrib.auth.views import LoginView
|
||||
urlpatterns = [
|
||||
path('test_make/', make_notification),
|
||||
path('test/', live_tester),
|
||||
path('login/', LoginView.as_view(), name='login'), # reverse for django login is not working
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include('notifications.urls', namespace='notifications')),
|
||||
]
|
||||
elif StrictVersion(get_version()) >= StrictVersion('2.0') and StrictVersion(get_version()) < StrictVersion('2.1'):
|
||||
from django.urls import include, path # noqa
|
||||
from django.contrib.auth.views import login
|
||||
urlpatterns = [
|
||||
path('test_make/', make_notification),
|
||||
path('test/', live_tester),
|
||||
path('login/', login, name='login'), # reverse for django login is not working
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include('notifications.urls', namespace='notifications')),
|
||||
]
|
||||
else:
|
||||
from django.conf.urls import include, url
|
||||
from django.contrib.auth.views import login
|
||||
urlpatterns = [
|
||||
url(r'^login/$', login, name='login'), # reverse for django login is not working
|
||||
url(r'^test_make/', make_notification),
|
||||
url(r'^test/', live_tester),
|
||||
url(r'^', include('notifications.urls', namespace='notifications')),
|
||||
url(r'^admin/', admin.site.urls),
|
||||
]
|
||||
|
||||
urlpatterns = [
|
||||
path('test_make/', make_notification),
|
||||
path('test/', live_tester),
|
||||
path('login/', LoginView.as_view(), name='login'), # reverse for django login is not working
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include('notifications.urls', namespace='notifications')),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,28 +1,20 @@
|
|||
''' Django notification urls file '''
|
||||
# -*- coding: utf-8 -*-
|
||||
from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
from django import get_version
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
if StrictVersion(get_version()) >= StrictVersion('2.0'):
|
||||
from django.urls import re_path as pattern
|
||||
else:
|
||||
from django.conf.urls import url as pattern
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
pattern(r'^$', views.AllNotificationsList.as_view(), name='all'),
|
||||
pattern(r'^unread/$', views.UnreadNotificationsList.as_view(), name='unread'),
|
||||
pattern(r'^mark-all-as-read/$', views.mark_all_as_read, name='mark_all_as_read'),
|
||||
pattern(r'^mark-as-read/(?P<slug>\d+)/$', views.mark_as_read, name='mark_as_read'),
|
||||
pattern(r'^mark-as-unread/(?P<slug>\d+)/$', views.mark_as_unread, name='mark_as_unread'),
|
||||
pattern(r'^delete/(?P<slug>\d+)/$', views.delete, name='delete'),
|
||||
pattern(r'^api/unread_count/$', views.live_unread_notification_count, name='live_unread_notification_count'),
|
||||
pattern(r'^api/all_count/$', views.live_all_notification_count, name='live_all_notification_count'),
|
||||
pattern(r'^api/unread_list/$', views.live_unread_notification_list, name='live_unread_notification_list'),
|
||||
pattern(r'^api/all_list/', views.live_all_notification_list, name='live_all_notification_list'),
|
||||
]
|
||||
|
||||
app_name = 'notifications'
|
||||
urlpatterns = [
|
||||
path('', views.AllNotificationsList.as_view(), name='all'),
|
||||
path('unread/', views.UnreadNotificationsList.as_view(), name='unread'),
|
||||
path('mark-all-as-read/', views.mark_all_as_read, name='mark_all_as_read'),
|
||||
path('mark-as-read/<int:slug>)/', views.mark_as_read, name='mark_as_read'),
|
||||
path('mark-as-unread/<int:slug>/', views.mark_as_unread, name='mark_as_unread'),
|
||||
path('delete/<int:slug>/', views.delete, name='delete'),
|
||||
path('api/unread_count/', views.live_unread_notification_count, name='live_unread_notification_count'),
|
||||
path('api/all_count/', views.live_all_notification_count, name='live_all_notification_count'),
|
||||
path('api/unread_list/', views.live_unread_notification_list, name='live_unread_notification_list'),
|
||||
path('api/all_list/', views.live_all_notification_list, name='live_all_notification_list'),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -3,12 +3,8 @@
|
|||
import sys
|
||||
|
||||
|
||||
if sys.version > '3':
|
||||
long = int # pylint: disable=invalid-name
|
||||
|
||||
|
||||
def slug2id(slug):
|
||||
return long(slug) - 110909
|
||||
return int(slug) - 110909
|
||||
|
||||
|
||||
def id2slug(notification_id):
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
''' Django Notifications example views '''
|
||||
from distutils.version import \
|
||||
StrictVersion # pylint: disable=no-name-in-module,import-error
|
||||
|
||||
from django import get_version
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.encoding import iri_to_uri
|
||||
|
|
@ -20,22 +17,6 @@ from notifications.utils import slug2id
|
|||
|
||||
Notification = load_model('notifications', 'Notification')
|
||||
|
||||
if StrictVersion(get_version()) >= StrictVersion('1.7.0'):
|
||||
from django.http import JsonResponse # noqa
|
||||
else:
|
||||
# Django 1.6 doesn't have a proper JsonResponse
|
||||
import json
|
||||
|
||||
from django.http import HttpResponse # noqa
|
||||
|
||||
def date_handler(obj):
|
||||
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
|
||||
|
||||
def JsonResponse(data): # noqa
|
||||
return HttpResponse(
|
||||
json.dumps(data, default=date_handler),
|
||||
content_type="application/json")
|
||||
|
||||
|
||||
class NotificationViewList(ListView):
|
||||
template_name = 'notifications/list.html'
|
||||
|
|
@ -133,12 +114,7 @@ def delete(request, slug=None):
|
|||
|
||||
@never_cache
|
||||
def live_unread_notification_count(request):
|
||||
try:
|
||||
user_is_authenticated = request.user.is_authenticated()
|
||||
except TypeError: # Django >= 1.11
|
||||
user_is_authenticated = request.user.is_authenticated
|
||||
|
||||
if not user_is_authenticated:
|
||||
if not request.user.is_authenticated:
|
||||
data = {
|
||||
'unread_count': 0
|
||||
}
|
||||
|
|
@ -152,12 +128,7 @@ def live_unread_notification_count(request):
|
|||
@never_cache
|
||||
def live_unread_notification_list(request):
|
||||
''' Return a json with a unread notification list '''
|
||||
try:
|
||||
user_is_authenticated = request.user.is_authenticated()
|
||||
except TypeError: # Django >= 1.11
|
||||
user_is_authenticated = request.user.is_authenticated
|
||||
|
||||
if not user_is_authenticated:
|
||||
if not request.user.is_authenticated:
|
||||
data = {
|
||||
'unread_count': 0,
|
||||
'unread_list': []
|
||||
|
|
@ -176,12 +147,7 @@ def live_unread_notification_list(request):
|
|||
@never_cache
|
||||
def live_all_notification_list(request):
|
||||
''' Return a json with a unread notification list '''
|
||||
try:
|
||||
user_is_authenticated = request.user.is_authenticated()
|
||||
except TypeError: # Django >= 1.11
|
||||
user_is_authenticated = request.user.is_authenticated
|
||||
|
||||
if not user_is_authenticated:
|
||||
if not request.user.is_authenticated:
|
||||
data = {
|
||||
'all_count': 0,
|
||||
'all_list': []
|
||||
|
|
@ -198,12 +164,7 @@ def live_all_notification_list(request):
|
|||
|
||||
|
||||
def live_all_notification_count(request):
|
||||
try:
|
||||
user_is_authenticated = request.user.is_authenticated()
|
||||
except TypeError: # Django >= 1.11
|
||||
user_is_authenticated = request.user.is_authenticated
|
||||
|
||||
if not user_is_authenticated:
|
||||
if not request.user.is_authenticated:
|
||||
data = {
|
||||
'all_count': 0
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue