Commit e355c7b8 authored by ibuler's avatar ibuler

Modify pagination and Role model

parent 7789c8d1
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from django import template from django import template
from django.utils import timezone from django.utils import timezone
from django.conf import settings
register = template.Library() register = template.Library()
...@@ -18,3 +19,29 @@ def is_expired(datetime): ...@@ -18,3 +19,29 @@ def is_expired(datetime):
return False return False
else: else:
return True return True
@register.filter
def pagination_range(total_page, current_num=1, display=5):
"""Return Page range
:param total_page: Total numbers of paginator
:param current_num: current display page num
:param display: Display as many as [:display:] page
In order to display many page num on web like:
< 1 2 3 4 5 >
"""
try:
current_num = int(current_num)
except ValueError:
current_num = 1
start = current_num - display/2 if current_num > display/2 else 1
end = start + display if start + display <= total_page else total_page + 1
return range(start, end)
...@@ -11,21 +11,32 @@ https://docs.djangoproject.com/en/1.10/ref/settings/ ...@@ -11,21 +11,32 @@ https://docs.djangoproject.com/en/1.10/ref/settings/
""" """
import os import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.dirname(BASE_DIR))
# Import project config setting
try:
from config import config as env_config, env
CONFIG = env_config.get(env, 'default')()
except ImportError:
CONFIG = type('_', (), {'__getattr__': None})()
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2vym+ky!997d5kkcc64mnz06y1mmui3lut#(^wd=%s_qj$1%xv' SECRET_KEY = CONFIG.SECRET_KEY or '2vym+ky!997d5kkcc64mnz06y1mmui3lut#(^wd=%s_qj$1%x'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = CONFIG.DEBUG or False
ALLOWED_HOSTS = [] ALLOWED_HOSTS = CONFIG.ALLOWED_HOSTS or []
# Application definition # Application definition
...@@ -80,12 +91,24 @@ WSGI_APPLICATION = 'jumpserver.wsgi.application' ...@@ -80,12 +91,24 @@ WSGI_APPLICATION = 'jumpserver.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases # https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = { if CONFIG.DB_ENGINE == 'sqlite':
'default': { DATABASES = {
'ENGINE': 'django.db.backends.sqlite3', 'default': {
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.sqlite3',
'NAME': CONFIG.DB_NAME or os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.%s' % CONFIG.DB_ENGINE,
'NAME': CONFIG.DB_NAME,
'HOST': CONFIG.DB_HOST,
'PORT': CONFIG.DB_PORT,
'USER': CONFIG.DB_USERNAME,
'PASSWORD': CONFIG.DB_PASSWORD,
}
} }
}
# Password validation # Password validation
...@@ -133,3 +156,5 @@ STATICFILES_DIRS = ( ...@@ -133,3 +156,5 @@ STATICFILES_DIRS = (
AUTH_USER_MODEL = 'users.User' AUTH_USER_MODEL = 'users.User'
BOOTSTRAP_COLUMN_COUNT = 11 BOOTSTRAP_COLUMN_COUNT = 11
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') + '/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') + '/'
{% load common_tags %}
{% if is_paginated %} {% if is_paginated %}
<div class="row"> <div class="row">
<div class="col-sm-6"> <div class="col-sm-6">
...@@ -9,17 +10,24 @@ ...@@ -9,17 +10,24 @@
<div class="dataTables_paginate paging_simple_numbers" id="editable_paginate"> <div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
<ul class="pagination" style="margin-top: 0; float: right"> <ul class="pagination" style="margin-top: 0; float: right">
{% if page_obj.has_previous %} {% if page_obj.has_previous %}
<li class="paginate_button previous" aria-controls="editable" tabindex="0" <li class="paginate_button previous" aria-controls="editable" tabindex="0" id="previous">
id="editable_previous"> <a data-page="next" href="?page={{ page_obj.previous_page_number}}"></a>
<a class="page" href="?page={{ page_obj.previous_page_number }}">Previous</a>
</li> </li>
{% endif %} {% endif %}
<li class="paginate_button active" aria-controls="editable" tabindex="0">
<a class="page" href="?page={{ page_obj.number }}" title="第{{ page_obj.number }}页">{{ page_obj.number }}</a> {% for page in paginator.num_pages|pagination_range:page_obj.number %}
</li> {% if page == page_obj.number %}
<li class="paginate_button active" aria-controls="editable" tabindex="0">
{% else %}
<li class="paginate_button" aria-controls="editable" tabindex="0">
{% endif %}
<a class="page" href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a>
</li>
{% endfor %}
{% if page_obj.has_next %} {% if page_obj.has_next %}
<li class="paginate_button next" aria-controls="editable" tabindex="0" id="editable_next"> <li class="paginate_button next" aria-controls="editable" tabindex="0" id="next">
<a class="page" href="?page={{ page_obj.next_page_number }}">Next</a> <a data-page="next" href="?page={{ page_obj.next_page_number }}"></a>
</li> </li>
{% endif %} {% endif %}
</ul> </ul>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
from django.forms import ModelForm from django.forms import ModelForm
from django import forms from django import forms
from .models import User, UserGroup from .models import User, UserGroup, Role
class UserAddForm(ModelForm): class UserAddForm(ModelForm):
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
from django.db.models.signals import post_migrate
from django.dispatch import receiver
from django.utils import timezone from django.utils import timezone
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractUser, Permission from django.contrib.auth.models import AbstractUser, Permission
...@@ -18,6 +21,9 @@ class Role(AbstractGroup): ...@@ -18,6 +21,9 @@ class Role(AbstractGroup):
class Meta: class Meta:
db_table = 'role' db_table = 'role'
def init(self):
pass
@classmethod @classmethod
def init(cls): def init(cls):
roles = { roles = {
......
...@@ -6,6 +6,7 @@ from django.db.models import Q ...@@ -6,6 +6,7 @@ from django.db.models import Q
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.conf import settings
from .models import User, UserGroup, Role from .models import User, UserGroup, Role
from .forms import UserAddForm, UserUpdateForm, UserGroupForm from .forms import UserAddForm, UserUpdateForm, UserGroupForm
...@@ -13,7 +14,7 @@ from .forms import UserAddForm, UserUpdateForm, UserGroupForm ...@@ -13,7 +14,7 @@ from .forms import UserAddForm, UserUpdateForm, UserGroupForm
class UserListView(ListView): class UserListView(ListView):
model = User model = User
paginate_by = 20 paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'user_list' context_object_name = 'user_list'
template_name = 'users/user_list.html' template_name = 'users/user_list.html'
ordering = '-date_joined' ordering = '-date_joined'
...@@ -39,7 +40,6 @@ class UserListView(ListView): ...@@ -39,7 +40,6 @@ class UserListView(ListView):
class UserAddView(CreateView): class UserAddView(CreateView):
model = User model = User
form_class = UserAddForm form_class = UserAddForm
initial = {'role': Role.objects.get(name='User')}
template_name = 'users/user_add.html' template_name = 'users/user_add.html'
success_url = reverse_lazy('users:user-list') success_url = reverse_lazy('users:user-list')
......
58
/root/env2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.pyc in get_prep_value(self, value)
944 if value is None:
945 return None
--> 946 return int(value)
947
948 def contribute_to_class(self, cls, name, **kwargs):
TypeError: int() argument must be a string or a number, not 'QuerySet'
In [6]: group.user_set.update(users)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-94520763e8e7> in <module>()
----> 1 group.user_set.update(users)
/root/env2.7/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
83 def create_method(name, method):
84 def manager_method(self, *args, **kwargs):
---> 85 return getattr(self.get_queryset(), name)(*args, **kwargs)
86 manager_method.__name__ = method.__name__
87 manager_method.__doc__ = method.__doc__
TypeError: update() takes exactly 1 argument (2 given)
In [7]: users
Out[7]: <QuerySet [<User: christina86>, <User: tina71>, <User: emily71>, <User: tammy66>, <User: sarah66>, <User: linda75>, <User: ev
elyn88>, <User: mildred72>, <User: sharon93>, <User: christina88>, <User: heather80>, <User: amy80>, <User: julia81>, <User: sarah89>
, <User: phillip74>, <User: kimberly74>, <User: ann73>, <User: helen63>, <User: joyce77>, <User: debra67>, '...(remaining elements tr
uncated)...']>
In [8]: group.user_set
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment