8# added profile model

This commit is contained in:
Eligiusz Kurzawa 2020-12-31 15:56:44 +01:00
parent 538f3a52c7
commit e159892f44
4 changed files with 50 additions and 1 deletions

BIN
media/users/person_1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -1,7 +1,7 @@
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from .models import Account from .models import Account, Profile
class MyAdminAccounts(UserAdmin): class MyAdminAccounts(UserAdmin):
@ -27,3 +27,4 @@ class MyAdminAccounts(UserAdmin):
admin.site.register(Account, MyAdminAccounts) admin.site.register(Account, MyAdminAccounts)
admin.site.register(Profile)

View File

@ -0,0 +1,27 @@
# Generated by Django 3.1.4 on 2020-12-31 14:51
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(upload_to='media/users')),
('birth_day', models.DateField(blank=True, default=None, null=True)),
('location', models.CharField(blank=True, max_length=100)),
('resume', models.TextField(blank=True)),
('company', models.CharField(blank=True, max_length=250)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -2,6 +2,7 @@ from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin from django.contrib.auth.models import PermissionsMixin
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from PIL import Image
class UserManager(BaseUserManager): class UserManager(BaseUserManager):
@ -45,3 +46,23 @@ class Account(AbstractBaseUser, PermissionsMixin):
class Meta: class Meta:
verbose_name = _('user') verbose_name = _('user')
verbose_name_plural = _('users') verbose_name_plural = _('users')
class Profile(models.Model):
user = models.OneToOneField(Account, on_delete=models.CASCADE, related_name="profile")
image = models.ImageField(upload_to="media/users")
birth_day = models.DateField(default=None, blank=True, null=True)
location = models.CharField(max_length=100, blank=True)
resume = models.TextField(blank=True)
company = models.CharField(max_length=250, blank=True)
def __str__(self):
return self.user.first_name + " " + self.user.last_name + " " + self.user.email
def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)
img = Image.open(self.image)
if img.height > 200 or img.width > 200:
new_size = (200, 200)
img.thumbnail(new_size)
img.save(self.image.path)