Commit 824b1c7f authored by ibuler's avatar ibuler

update user update view

parent bcae7bea
...@@ -125,10 +125,11 @@ USE_TZ = True ...@@ -125,10 +125,11 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.10/howto/static-files/ # https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = ( STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), os.path.join(BASE_DIR, "static"),
) )
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, 'img').replace('\\', '/') + '/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') + '/'
...@@ -14,10 +14,15 @@ Including another URLconf ...@@ -14,10 +14,15 @@ Including another URLconf
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" """
from django.conf.urls import url, include from django.conf.urls import url, include
#from django.contrib import admin from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
url(r'^users/', include('users.urls')), url(r'^users/', include('users.urls')),
url(r'^assets/', include('assets.urls')), url(r'^assets/', include('assets.urls')),
# url(r'^admin/', admin.site.urls), # url(r'^admin/', admin.site.urls),
] ]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
...@@ -4583,3 +4583,4 @@ body.skin-3 { ...@@ -4583,3 +4583,4 @@ body.skin-3 {
border-style: solid; border-style: solid;
border-width: 1px border-width: 1px
} }
...@@ -6,11 +6,11 @@ from django import forms ...@@ -6,11 +6,11 @@ from django import forms
from .models import User, UserGroup from .models import User, UserGroup
class UserForm(ModelForm): class UserAddForm(ModelForm):
class Meta: class Meta:
model = User model = User
fields = [ fields = [
'username', 'name', 'email', 'groups', 'wechat', 'avatar', 'username', 'name', 'email', 'groups', 'wechat',
'phone', 'enable_2FA', 'role', 'date_expired', 'comment', 'phone', 'enable_2FA', 'role', 'date_expired', 'comment',
] ]
# widgets = { # widgets = {
...@@ -18,3 +18,11 @@ class UserForm(ModelForm): ...@@ -18,3 +18,11 @@ class UserForm(ModelForm):
# } # }
class UserUpdateForm(ModelForm):
class Meta:
model = User
fields = [
'name', 'email', 'groups', 'wechat', 'avatar',
'phone', 'enable_2FA', 'role', 'date_expired', 'comment',
]
...@@ -61,7 +61,7 @@ class User(AbstractUser): ...@@ -61,7 +61,7 @@ class User(AbstractUser):
name = models.CharField(max_length=20, verbose_name='姓名', help_text='* required') name = models.CharField(max_length=20, verbose_name='姓名', help_text='* required')
email = models.EmailField(max_length=30, unique=True, verbose_name='邮件', help_text='* required') email = models.EmailField(max_length=30, unique=True, verbose_name='邮件', help_text='* required')
groups = models.ManyToManyField(UserGroup, verbose_name='用户组') groups = models.ManyToManyField(UserGroup, verbose_name='用户组')
avatar = models.ImageField(upload_to="avatar", verbose_name='头像', blank=True) avatar = models.ImageField(upload_to="avatar", verbose_name='头像')
wechat = models.CharField(max_length=30, blank=True, verbose_name='微信') wechat = models.CharField(max_length=30, blank=True, verbose_name='微信')
phone = models.CharField(max_length=20, blank=True, verbose_name='手机号') phone = models.CharField(max_length=20, blank=True, verbose_name='手机号')
enable_2FA = models.BooleanField(default=False, verbose_name='启用二次验证') enable_2FA = models.BooleanField(default=False, verbose_name='启用二次验证')
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
</div> </div>
</div> </div>
<div class="ibox-content"> <div class="ibox-content">
<form method="post" id="userForm" class="form-horizontal" action=""> <form method="post" id="userForm" class="form-horizontal" action="" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
<h3>账户</h3> <h3>账户</h3>
{% block username %} {% endblock %} {% block username %} {% endblock %}
......
{% extends 'base.html' %} {% extends 'base.html' %}
{% load common_tags %} {% load common_tags %}
{% load users_tags %}
{% load static %} {% load static %}
{% block content %}
<div class="wrapper wrapper-content animated fadeInRight"> {% block content %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
<div class="ibox float-e-margins"> <div class="ibox float-e-margins">
{# <div class="ibox-content">#} {# <div class="ibox-content">#}
<div class="panel-options"> <div class="panel-options">
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="active"><a href="" class="text-center"><i class="fa fa-laptop"></i> 用户信息 </a></li> <li class="active"><a href="" class="text-center"><i class="fa fa-laptop"></i> 用户信息 </a>
</li>
<li><a href="" class="text-center"><i class="fa fa-bar-chart-o"></i> 用户资产</a></li> <li><a href="" class="text-center"><i class="fa fa-bar-chart-o"></i> 用户资产</a></li>
<li><a href="" class="text-center"><i class="fa fa-bar-chart-o"></i> 登录记录 </a></li> <li><a href="" class="text-center"><i class="fa fa-bar-chart-o"></i> 登录记录 </a></li>
<div class="" style="float: right"> <div class="" style="float: right">
<form id="search_form" method="get" action="" class="pull-right mail-search"> <form id="search_form" method="get" action="" class="pull-right mail-search">
<div class="input-group"> <div class="input-group">
<input type="text" class="form-control input-sm" id="keyword" name="keyword" value="{{ keyword }}" placeholder="Search"> <input type="text" class="form-control input-sm" id="keyword" name="keyword"
value="{{ keyword }}" placeholder="Search">
<div class="input-group-btn"> <div class="input-group-btn">
<button id='search_btn' type="submit" class="btn btn-sm btn-primary"> <button id='search_btn' type="submit" class="btn btn-sm btn-primary">
搜索 搜索
...@@ -55,60 +58,60 @@ ...@@ -55,60 +58,60 @@
<tbody> <tbody>
<tr> <tr>
<td colspan="2"> <td colspan="2">
<img src="{% static "img/root.png" %}" class="img-circle", width="64" height="64"> <img src="{{ user | user_avatar_url }}" class="img-circle" width="64"
height="64">
</td> </td>
</tr> </tr>
<tr> <tr>
<td width="20%"><b>姓名:</b></td> <td width="20%">姓名:</td>
<td>{{ user.name }}</td> <td><b>{{ user.name }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>用户名:</b></td> <td>用户名:</td>
<td>{{ user.username }}</td> <td><b>{{ user.username }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>邮件:</b></td> <td>邮件:</td>
<td>{{ user.email }}</td> <td><b>{{ user.email }}</b></td>
</tr> </tr>
{% if user.phone %} {% if user.phone %}
<tr> <tr>
<td><b>手机:</b></td> <td>手机:</td>
<td>{{ user.phone }}</td> <td><b>{{ user.phone }}</b></td>
</tr> </tr>
{% endif %} {% endif %}
{% if user.wechat %} {% if user.wechat %}
<tr> <tr>
<td><b>微信:</b></td> <td>微信:</td>
<td>{{ user.wechat }}</td> <td><b>{{ user.wechat }}</b></td>
</tr> </tr>
{% endif %} {% endif %}
<tr> <tr>
<td><b>角色:</b></td> <td>角色:</td>
<td>{{ user.role.name }}</td> <td><b>{{ user.role.name }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>有效期:</b></td> <td>有效期:</td>
<td>{{ user.date_expired|date:"Y-m-j H:i:s" }}</td> <td><b>{{ user.date_expired|date:"Y-m-j H:i:s" }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>创建者:</b></td> <td>创建者:</td>
<td>{{ user.created_by }}</td> <td><b>{{ user.created_by }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>创建日期:</b></td> <td>创建日期:</td>
<td>{{ user.date_joined|date:"Y-m-j H:i:s" }}</td> <td><b>{{ user.date_joined|date:"Y-m-j H:i:s" }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>最后登录:</b></td> <td>最后登录:</td>
<td>{{ user.last_login|date:"Y-m-j H:i:s" }}</td> <td><b>{{ user.last_login|date:"Y-m-j H:i:s" }}</b></td>
</tr> </tr>
<tr> <tr>
<td><b>描述:</b></td> <td>描述:</td>
<td>{{ user.comment }}</td> <td><b>{{ user.comment }}</b></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>
</div> </div>
...@@ -119,21 +122,54 @@ ...@@ -119,21 +122,54 @@
<div class="ibox-tools"> <div class="ibox-tools">
</div> </div>
</div> </div>
<div class="ibox-content primary-panel" > <div class="ibox-content primary-panel">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> <tr>
<td colspan="2"> <td width="50%">Active:</td>
<img src="{% static "img/root.png" %}" class="img-circle" width="64" height="64"> <td><span style="float: right">
</td> <div class="switch">
<div class="onoffswitch">
<input type="checkbox" checked class="onoffswitch-checkbox"
id="example1">
<label class="onoffswitch-label" for="example1">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
</span></td>
</tr> </tr>
<tr> <tr>
<td width="20%"><b>姓名:</b></td> <td>二次验证:</td>
<td>{{ user.name }}</td> <td><span style="float: right">
<div class="switch">
<div class="onoffswitch">
<input type="checkbox" class="onoffswitch-checkbox"
id="example2">
<label class="onoffswitch-label" for="example2">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
</span></td>
</tr> </tr>
<tr> <tr>
<td><b>描述:</b></td> <td>重置密码:</td>
<td>{{ user.comment }}</td> <td>
<span style="float: right">
<button type="button" class="btn btn-primary btn-xs" style="width: 54px">重置</button>
</span>
</td>
</tr>
<tr>
<td class="no-borders">重置密钥:</td>
<td class="no-borders">
<span style="float: right">
<button type="button" class="btn btn-primary btn-xs" style="width: 54px;">重置</button>
</span>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
...@@ -150,16 +186,17 @@ ...@@ -150,16 +186,17 @@
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> <tr>
<td colspan="2"> <td>
<img src="{% static "img/root.png" %}" class="img-circle", width="64" height="64">
</td> </td>
</tr> </tr>
<tr> <tr>
<td width="20%"><b>姓名:</b></td> <td width="20%">姓名:</td>
<td>{{ user.name }}</td> <td>{{ user.name }}</td>
</tr> </tr>
<tr> <tr>
<td><b>描述:</b></td> <td>描述:</td>
<td>{{ user.comment }}</td> <td>{{ user.comment }}</td>
</tr> </tr>
</tbody> </tbody>
...@@ -172,6 +209,12 @@ ...@@ -172,6 +209,12 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
{% block custom_footer_js %}
<script>
$(document).ready(function () {
})
</script>
{% endblock %}
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="form-group"> <div class="form-group">
<label for="{{ form.username.id_for_label }}" class="col-sm-2 control-label">用户名</label> <label for="{{ form.username.id_for_label }}" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-9 controls" > <div class="col-sm-9 controls" >
<input id="{{ form.username.id_for_label }}" name="username" type="text" value="{{ form.username.value }}" readonly class="form-control"> <input id="{{ form.username.id_for_label }}" name="username" type="text" value="{{ user.username }}" readonly class="form-control">
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
......
# ~*~ coding: utf-8 ~*~
import urllib
import hashlib
from django import template
from django.utils import timezone
from django.conf import settings
from django.conf.urls.static import static
register = template.Library()
@register.filter
def join_queryset_attr(queryset, attr, delimiter=', '):
return delimiter.join([getattr(obj, attr, '') for obj in queryset])
@register.filter
def is_expired(datetime):
if datetime > timezone.now():
return False
else:
return True
@register.filter
def user_avatar_url(user, size=64):
if user.avatar:
return user.avatar.url
gravatar_url = "https://www.gravatar.com/avatar/" \
+ hashlib.md5(user.email.lower()).hexdigest() + "?"
gravatar_url += urllib.urlencode({'d': 'identicon', 's': str(size)})
return gravatar_url
...@@ -7,7 +7,7 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView ...@@ -7,7 +7,7 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .models import User, UserGroup, Role from .models import User, UserGroup, Role
from .forms import UserForm from .forms import UserAddForm, UserUpdateForm
class UserListView(ListView): class UserListView(ListView):
...@@ -33,7 +33,7 @@ class UserListView(ListView): ...@@ -33,7 +33,7 @@ class UserListView(ListView):
class UserAddView(CreateView): class UserAddView(CreateView):
model = User model = User
form_class = UserForm form_class = UserAddForm
initial = {'role': Role.objects.get(name='User')} 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')
...@@ -52,17 +52,27 @@ class UserAddView(CreateView): ...@@ -52,17 +52,27 @@ class UserAddView(CreateView):
class UserUpdateView(UpdateView): class UserUpdateView(UpdateView):
model = User model = User
form_class = UserForm form_class = UserUpdateForm
template_name = 'users/user_edit.html' template_name = 'users/user_edit.html'
context_object_name = 'user'
success_url = reverse_lazy('users:user-list') success_url = reverse_lazy('users:user-list')
def form_valid(self, form): def form_valid(self, form):
user = form.save() username = self.object.username
user = form.save(commit=False)
user.username = username
user.save()
password = self.request.POST.get('password', '') password = self.request.POST.get('password', '')
if password: if password:
user.set_password(password) user.set_password(password)
return super(UserUpdateView, self).form_valid(form) return super(UserUpdateView, self).form_valid(form)
def form_invalid(self, form):
print(self.request.FILES)
print(form['avatar'].value())
print(form.errors)
return super(UserUpdateView, self).form_invalid(form)
class UserDeleteView(DeleteView): class UserDeleteView(DeleteView):
model = User model = User
...@@ -70,7 +80,7 @@ class UserDeleteView(DeleteView): ...@@ -70,7 +80,7 @@ class UserDeleteView(DeleteView):
template_name = 'users/user_delete_confirm.html' template_name = 'users/user_delete_confirm.html'
class UserDetailView(DeleteView): class UserDetailView(DetailView):
model = User model = User
template_name = 'users/user_detail.html' template_name = 'users/user_detail.html'
context_object_name = "user" context_object_name = "user"
......
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