Skip to content

Commit

Permalink
Made user avatars.
Browse files Browse the repository at this point in the history
  • Loading branch information
slavistoev committed Apr 29, 2022
1 parent 12cc784 commit 0e0453e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,7 @@ venv.bak/
dmypy.json

# Pyre type checker
.pyre/
.pyre/

# Media
media
4 changes: 3 additions & 1 deletion eduzec/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
path("admin/", admin.site.urls),
path('', TemplateView.as_view(template_name="index.html"), name='landing'),
path("", include(("forum.urls", "forum"), namespace="forum")),
path("", include(("users.urls", "users"), namespace="users")),
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ coverage==6.3.2
selenium==4.1.3 #install webdriver for specific broswer
six==1.16.0
python-dotenv==0.20.0
Pillow==9.1.0 # Python Imaging Library
django-imagekit==4.1.0 # easier image manipulation
7 changes: 6 additions & 1 deletion settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
'django.contrib.staticfiles',
'users',
'vote',
'imagekit',
]

# Add static file directory
Expand Down Expand Up @@ -163,4 +164,8 @@
EMAIL_HOST_USER = os.getenv('EMAIL_FROM_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_PORT = 587

# User-uploaded static files
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
Binary file added static/images/user_icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions templates/forum/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2 class='my-4'>{{quest.title}}</h2>
<p>
<span class='mr-2'>{{quest.add_time|date:'d/m/y'}}</span>
<text>by</text>
{% include 'users/includes/avatar.html' with avatar=quest.user.avatar %}
<a href='#' class='ml-2'>{{quest.user}}</a>
<button type='button' class='btn btn-dark'><a href='{% url 'forum:answer_with_pk' pk=quest.id %}'>Answer</a></button>
</p>
Expand Down Expand Up @@ -63,6 +64,7 @@ <h2 class='my-4'>{{quest.title}}</h2>
<p>
<span class='mr-2'>{{answer.add_time|date:'d/m/y'}}</span>
<text>by</text>
{% include 'users/includes/avatar.html' with avatar=answer.user.avatar %}
<a href='#' class='mr-2'>{{ answer.user }}</a>
<span>{{ comments|length }} comments</span>
</p>
Expand Down
2 changes: 1 addition & 1 deletion templates/users/edit_account.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% block content %}
<h1>Edit you profile</h1>
<form method="post">
<form method="post" enctype='multipart/form-data'>
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Update Profile" />
Expand Down
6 changes: 6 additions & 0 deletions templates/users/includes/avatar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load static %}
{% if avatar %}
<img src="{{ avatar.url }}" alt="avatar">
{% else %}
<img src="{% static 'images/user_icon.jpg' %}" alt="avatar">
{% endif %}
1 change: 1 addition & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {"fields": ("username", "password")}),
(_("Personal info"), {"fields": ("first_name", "last_name", "email")}),
(_("Profile info"), {"fields": ("avatar",)}),
(
_("Permissions"),
{
Expand Down
2 changes: 1 addition & 1 deletion users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Meta:
class CustomUserChangeForm(ModelForm):
class Meta:
model = CustomUser
fields = ("username", "email", "first_name", "last_name", "hide_email",)
fields = ("username", "email", "avatar", "first_name", "last_name", "hide_email",)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
9 changes: 9 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from email.policy import default
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager
Expand All @@ -6,6 +7,8 @@
from django.core.validators import validate_email
from django.utils import timezone

from imagekit.models import ProcessedImageField

# Create your models here.

class CustomUserManager(BaseUserManager):
Expand Down Expand Up @@ -33,9 +36,15 @@ def create_superuser(self, email, username, password=None):
user.save(using=self._db)
return user

from imagekit.processors import ResizeToFill, Thumbnail, ResizeToFit

class CustomUser(AbstractBaseUser):
username = models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 40 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=40, unique=True, validators=[UnicodeUsernameValidator, ], verbose_name='username')
email = models.EmailField(max_length=254,help_text='Required. 254 characters or fewer.', validators=[validate_email] ,verbose_name='email address', unique=True)
avatar = ProcessedImageField(null=True, blank=True, upload_to='avatars',
processors=[ResizeToFit(50, 100)],
format='JPEG',
options={'quality': 60})
first_name = models.CharField(blank=True, max_length=150, verbose_name='first name')
last_name = models.CharField(blank=True, max_length=150, verbose_name='last name')
last_login = models.DateTimeField(blank=True, null=True, verbose_name='last login')
Expand Down

0 comments on commit 0e0453e

Please sign in to comment.