Commit ff8c830a authored by freetux's avatar freetux

extend user model add some fields to user and first try to add avatar.

parent 41c5d5e8
......@@ -8,7 +8,6 @@ from django.contrib.auth.decorators import login_required
from body.models import Weight
from django.http import JsonResponse
from django.template.loader import render_to_string
from .forms import WeightForm
from users.models import User
from django.shortcuts import render, redirect
......
......@@ -73,7 +73,7 @@
</li>S
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle nav-link" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
<img src="{{user.avatar.url}}" class="img-avatar" alt="{{user.email}}">
<img src="{{user.avatar_url}}" class="img-avatar" alt="{{user.email}}">
<span class="hidden-md-down">{{user.name}}</span>
</a>
<div class="dropdown-menu dropdown-menu-right">
......
......@@ -33,7 +33,7 @@
<div class="card">
<div class="card-block p-a-2">
<h1>عضویت</h1>
<form method="post" action=".">
<form method="post" action="." enctype="multipart/form-data">
<p class="text-muted">حساب کاربری خود را بسازید!</p>
{% if form.errors %}
......
......@@ -130,3 +130,6 @@ validate_email_domain = EmailDomainValidator()
class UsersEmailField(forms.EmailField):
default_validators = [validate_email, validate_email_domain]
class ImageField(forms.ImageField):
widget = forms.ClearableFileInput()
\ No newline at end of file
......@@ -4,21 +4,22 @@ from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.utils.translation import ugettext_lazy as _
from .conf import settings
from .fields import HoneyPotField, PasswordField, UsersEmailField
from .fields import HoneyPotField, PasswordField, UsersEmailField, ImageField
from django.core.files.images import get_image_dimensions
class UserCreationForm(forms.ModelForm):
error_messages = {
'duplicate_email': _('A user with that email already exists.'),
'password_mismatch': _('The two password fields didn\'t match.'),
'duplicate_email': _('کاربری با این ایمیل قبلا ثبت شده است'),
'password_mismatch': _('گذرواژه تطابق ندارد'),
}
email = UsersEmailField(label=_('Email Address'), max_length=255)
password1 = PasswordField(label=_('Password'))
email = UsersEmailField(label=_('ایمیل'), max_length=255)
password1 = PasswordField(label=_('گذرواژه'))
password2 = PasswordField(
label=_('Password Confirmation'),
label=_('تایید گذرواژه'),
help_text=_('Enter the same password as above, for verification.'))
avatar = ImageField(label=_('avatar'))
class Meta:
model = get_user_model()
......@@ -48,6 +49,39 @@ class UserCreationForm(forms.ModelForm):
code='password_mismatch',
)
return password2
def clean_avatar(self):
avatar = self.cleaned_data['avatar']
try:
w, h = get_image_dimensions(avatar)
#validate dimensions
max_width = max_height = 100
if w > max_width or h > max_height:
raise forms.ValidationError(
u'Please use an image that is '
'%s x %s pixels or smaller.' % (max_width, max_height))
#validate content type
main, sub = avatar.content_type.split('/')
if not (main == 'image' and sub in ['jpeg', 'pjpeg', 'gif', 'png']):
raise forms.ValidationError(u'Please use a JPEG, '
'GIF or PNG image.')
#validate file size
if len(avatar) > (20 * 1024):
raise forms.ValidationError(
u'Avatar file size may not exceed 20k.')
except AttributeError:
"""
Handles case when we are updating the user profile
and do not supply a new avatar
"""
pass
return avatar
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-05-19 20:48
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('users', '0002_alter_user_last_login_null'),
]
operations = [
migrations.AddField(
model_name='user',
name='avatar',
field=models.ImageField(null=True, upload_to='', verbose_name='نمایه'),
),
migrations.AddField(
model_name='user',
name='birthday',
field=models.DateTimeField(null=True, verbose_name='تاریخ تولد'),
),
migrations.AddField(
model_name='user',
name='first_name',
field=models.CharField(max_length=255, null=True, verbose_name='نام'),
),
migrations.AddField(
model_name='user',
name='gender',
field=models.CharField(choices=[('M', 'مرد'), ('F', 'زن')], default=django.utils.timezone.now, max_length=1, verbose_name='جنسیت'),
preserve_default=False,
),
migrations.AddField(
model_name='user',
name='last_name',
field=models.CharField(max_length=255, null=True, verbose_name='نام خانوادگی'),
),
]
......@@ -10,8 +10,19 @@ from .managers import UserInheritanceManager, UserManager
class AbstractUser(AbstractBaseUser, PermissionsMixin):
GENDER_CHOICES = (
('M', 'مرد'),
('F', 'زن'),
)
USERS_AUTO_ACTIVATE = not settings.USERS_VERIFY_EMAIL
first_name = models.CharField(_('نام'), max_length=255 , null=True)
last_name = models.CharField(_('نام خانوادگی'), max_length=255 , null=True)
birthday = models.DateTimeField(_('تاریخ تولد'), null=True)
gender = models.CharField(_('جنسیت'), max_length=1, choices=GENDER_CHOICES)
avatar = models.ImageField(_('نمایه'), null=True)
email = models.EmailField(
_('email address'), max_length=255, unique=True, db_index=True)
is_staff = models.BooleanField(
......@@ -35,6 +46,10 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
verbose_name = _('User')
verbose_name_plural = _('Users')
abstract = True
def avatar_url(self):
if self.avatar and hasattr(self.avatar, 'url'):
return self.avatar.url
def get_full_name(self):
""" Return the email."""
......
......@@ -51,7 +51,7 @@ def register(request,
post_registration_redirect = reverse('users_registration_complete')
if request.method == 'POST':
form = registration_form(request.POST)
form = registration_form(request.POST, request.FILES)
if form.is_valid():
user = form.save()
if settings.USERS_AUTO_LOGIN_AFTER_REGISTRATION:
......
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