Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agregados campos phone y bio a "Usuario" #208

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,061 changes: 2,061 additions & 0 deletions locale/es/LC_MESSAGES/django.po

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions temii/templates/users/user_detail.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load static %}
{% load static i18n %}

{% block title %}User: {{ object.username }}{% endblock %}

Expand All @@ -8,11 +8,17 @@

<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 %}
{% if object.bio %}
<p>{% translate "Bio" %}: {{ object.bio }}</p>
{% endif %}
{% if object.phone %}
<p>{% translate "Phone" %}: {{ object.phone }}</p>
{% endif %}
</div>
</div>

Expand All @@ -21,8 +27,8 @@ <h2>{{ object.username }}</h2>
<div class="row">

<div class="col-sm-12">
<a class="btn btn-primary" href="{% url 'users:update' %}" role="button">My Info</a>
<a class="btn btn-primary" href="{% url 'account_email' %}" role="button">E-Mail</a>
<a class="btn btn-primary" href="{% url 'users:update' %}" role="button">{% translate "Update" %}</a>
<a class="btn btn-primary" href="{% url 'account_email' %}" role="button">{% translate "E-Mail" %}</a>
<!-- Your Stuff: Custom user template urls -->
</div>

Expand Down
6 changes: 3 additions & 3 deletions temii/templates/users/user_form.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load crispy_forms_tags i18n %}

{% block title %}{{ user.username }}{% endblock %}

{% 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">
<div class="controls">
<button type="submit" class="btn btn-primary">Update</button>
<button type="submit" class="btn btn-primary">{% translate "Save" %}</button>
</div>
</div>
</form>
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
13 changes: 13 additions & 0 deletions temii/users/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from allauth.account.forms import SignupForm
from allauth.socialaccount.forms import SignupForm as SocialSignupForm
from django import forms
from django.contrib.auth import forms as admin_forms
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -32,6 +33,18 @@ class UserSignupForm(SignupForm):
Check UserSocialSignupForm for accounts created from social.
"""

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


class UserSocialSignupForm(SocialSignupForm):
"""
Expand Down
30 changes: 30 additions & 0 deletions temii/users/migrations/0002_user_bio_user_image_user_phone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.1.8 on 2023-08-12 16:41

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


class Migration(migrations.Migration):
dependencies = [
("users", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="user",
name="bio",
field=models.CharField(blank=True, max_length=255, verbose_name="Bio"),
),
migrations.AddField(
model_name="user",
name="image",
field=models.ImageField(
default="default.jpg", upload_to=temii.users.models.upload_to, verbose_name="Image"
),
),
migrations.AddField(
model_name="user",
name="phone",
field=models.CharField(blank=True, max_length=50, verbose_name="Phone"),
),
]
9 changes: 8 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 @@ -15,6 +19,9 @@ class User(AbstractUser):
name = CharField(_("Name of User"), blank=True, max_length=255)
first_name = None # type: ignore
last_name = None # type: ignore
phone = CharField(_("Phone"), blank=True, max_length=50)
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
8 changes: 6 additions & 2 deletions temii/users/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from typing import Any

from django.contrib.auth import get_user_model
from factory import Faker, post_generation
from factory.django import DjangoModelFactory
from django.core.files.base import ContentFile
from factory import Faker, LazyAttribute, post_generation
from factory.django import DjangoModelFactory, ImageField


class UserFactory(DjangoModelFactory):
username = Faker("user_name")
email = Faker("email")
name = Faker("name")
phone = Faker("phone_number")
bio = Faker("text")
image = LazyAttribute(lambda _: ContentFile(ImageField()._make_data({"width": 300, "height": 300}), "example.jpg"))

@post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs):
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"]
fields = ["name", "phone", "bio", "image"]
success_message = _("Information successfully updated")

def get_success_url(self):
Expand Down