Commit 824b1c7f authored by ibuler's avatar ibuler

update user update view

parent bcae7bea
......@@ -125,10 +125,11 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
AUTH_USER_MODEL = 'users.User'
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
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
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 = [
url(r'^users/', include('users.urls')),
url(r'^assets/', include('assets.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 {
border-style: solid;
border-width: 1px
}
......@@ -6,11 +6,11 @@ from django import forms
from .models import User, UserGroup
class UserForm(ModelForm):
class UserAddForm(ModelForm):
class Meta:
model = User
fields = [
'username', 'name', 'email', 'groups', 'wechat', 'avatar',
'username', 'name', 'email', 'groups', 'wechat',
'phone', 'enable_2FA', 'role', 'date_expired', 'comment',
]
# widgets = {
......@@ -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):
name = models.CharField(max_length=20, verbose_name='姓名', help_text='* required')
email = models.EmailField(max_length=30, unique=True, verbose_name='邮件', help_text='* required')
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='微信')
phone = models.CharField(max_length=20, blank=True, verbose_name='手机号')
enable_2FA = models.BooleanField(default=False, verbose_name='启用二次验证')
......
......@@ -26,7 +26,7 @@
</div>
</div>
<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 %}
<h3>账户</h3>
{% block username %} {% endblock %}
......
This diff is collapsed.
......@@ -3,7 +3,7 @@
<div class="form-group">
<label for="{{ form.username.id_for_label }}" class="col-sm-2 control-label">用户名</label>
<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>
{% 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
from django.views.generic.detail import DetailView
from .models import User, UserGroup, Role
from .forms import UserForm
from .forms import UserAddForm, UserUpdateForm
class UserListView(ListView):
......@@ -33,7 +33,7 @@ class UserListView(ListView):
class UserAddView(CreateView):
model = User
form_class = UserForm
form_class = UserAddForm
initial = {'role': Role.objects.get(name='User')}
template_name = 'users/user_add.html'
success_url = reverse_lazy('users:user-list')
......@@ -52,17 +52,27 @@ class UserAddView(CreateView):
class UserUpdateView(UpdateView):
model = User
form_class = UserForm
form_class = UserUpdateForm
template_name = 'users/user_edit.html'
context_object_name = 'user'
success_url = reverse_lazy('users:user-list')
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', '')
if password:
user.set_password(password)
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):
model = User
......@@ -70,7 +80,7 @@ class UserDeleteView(DeleteView):
template_name = 'users/user_delete_confirm.html'
class UserDetailView(DeleteView):
class UserDetailView(DetailView):
model = User
template_name = 'users/user_detail.html'
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