Skip to content

Commit

Permalink
Imagen de perfil
Browse files Browse the repository at this point in the history
  • Loading branch information
Scot3004 committed Aug 12, 2023
1 parent 4a70b24 commit 4191db7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion temii/templates/users/user_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<div class="row">
<div class="col-sm-12">

<h2>{{ object.username }}</h2>
<img class="img-fluid" src="{{ object.image.url }}">
{% if object.name %}
<p>{{ object.name }}</p>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion temii/templates/users/user_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% block content %}
<h1>{{ user.username }}</h1>
<form class="form-horizontal" method="post" action="{% url 'users:update' %}">
<form class="form-horizontal" method="post" action="{% url 'users:update' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<div class="control-group">
Expand Down
2 changes: 1 addition & 1 deletion temii/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UserAdmin(auth_admin.UserAdmin):
add_form = UserAdminCreationForm
fieldsets = (
(None, {"fields": ("username", "password")}),
(_("Personal info"), {"fields": ("name", "email")}),
(_("Personal info"), {"fields": ("name", "email", "phone", "bio", "image")}),
(
_("Permissions"),
{
Expand Down
2 changes: 2 additions & 0 deletions temii/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ class UserSignupForm(SignupForm):

phone = forms.CharField(max_length=20, label=_("Phone"), required=False)
bio = forms.CharField(max_length=255, label=_("Bio"), required=False)
image = forms.ImageField(label=_("Image"), required=False)

def save(self, request):
user = super().save(request)
user.phone = self.cleaned_data["phone"]
user.bio = self.cleaned_data["bio"]
user.image = self.cleaned_data["image"]
user.save()
return user

Expand Down
20 changes: 20 additions & 0 deletions temii/users/migrations/0003_user_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.1.8 on 2023-08-12 05:01

from django.db import migrations, models
import temii.users.models


class Migration(migrations.Migration):
dependencies = [
("users", "0002_user_bio_user_phone"),
]

operations = [
migrations.AddField(
model_name="user",
name="image",
field=models.ImageField(
default="default.jpg", upload_to=temii.users.models.upload_to, verbose_name="Image"
),
),
]
7 changes: 6 additions & 1 deletion temii/users/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.contrib.auth.models import AbstractUser
from django.db.models import CharField
from django.db.models import CharField, ImageField
from django.urls import reverse
from django.utils.translation import gettext_lazy as _


def upload_to(instance, filename):
return f"profile_pics/{instance.username}/{filename}"


class User(AbstractUser):
"""
Default custom user model for temii.
Expand All @@ -17,6 +21,7 @@ class User(AbstractUser):
last_name = None # type: ignore
phone = CharField(_("Phone"), blank=True, max_length=20)
bio = CharField(_("Bio"), blank=True, max_length=255)
image = ImageField(_("Image"), default="default.jpg", upload_to=upload_to)

def get_absolute_url(self) -> str:
"""Get URL for user's detail view.
Expand Down
2 changes: 1 addition & 1 deletion temii/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserDetailView(LoginRequiredMixin, DetailView):

class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = User
fields = ["name", "phone", "bio"]
fields = ["name", "phone", "bio", "image"]
success_message = _("Information successfully updated")

def get_success_url(self):
Expand Down

0 comments on commit 4191db7

Please sign in to comment.