Skip to content

Commit

Permalink
Contatos
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspdf committed Jun 6, 2024
1 parent ae4bc3b commit eee8398
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[flake8]
max-line-length = 270
exclude=.venv
exclude=.venv
ignore = F841
8 changes: 7 additions & 1 deletion eventex/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from eventex.core.models import Speaker
from eventex.core.models import Speaker, Contact
from django.contrib import admin
from django.utils.html import format_html


class ContactInLine(admin.TabularInline):
model = Contact
extra = 1


class SpeakerModelAdmin(admin.ModelAdmin):
inlines = [ContactInLine]
prepopulated_fields = {'slug': ('name',)}
list_display = ['name', 'photo_img', 'website_link']

Expand Down
41 changes: 41 additions & 0 deletions eventex/core/migrations/0003_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 5.0.3 on 2024-06-06 22:32

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0002_alter_speaker_options_alter_speaker_description_and_more"),
]

operations = [
migrations.CreateModel(
name="Contact",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"kind",
models.CharField(
choices=[("E", "Email"), ("P", "Phone")], max_length=1
),
),
("value", models.CharField(max_length=255)),
(
"speaker",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.speaker"
),
),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 5.0.3 on 2024-06-06 23:15

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0003_contact"),
]

operations = [
migrations.AlterModelOptions(
name="contact",
options={"verbose_name": "contato", "verbose_name_plural": "contatos"},
),
migrations.AlterField(
model_name="contact",
name="kind",
field=models.CharField(
choices=[("E", "Email"), ("P", "Phone")],
max_length=1,
verbose_name="tipo",
),
),
migrations.AlterField(
model_name="contact",
name="speaker",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.speaker",
verbose_name="palestrante",
),
),
migrations.AlterField(
model_name="contact",
name="value",
field=models.CharField(max_length=255, verbose_name="valor"),
),
]
19 changes: 19 additions & 0 deletions eventex/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,22 @@ def __str__(self):

def get_absolute_url(self):
return r('speaker_detail', slug=self.slug)


class Contact(models.Model):
EMAIL = 'E'
PHONE = 'P'
KINDS = (
(EMAIL, 'Email'),
(PHONE, 'Phone'),
)
speaker = models.ForeignKey('Speaker', on_delete=models.CASCADE, verbose_name='palestrante')
kind = models.CharField('tipo', max_length=1, choices=KINDS)
value = models.CharField('valor', max_length=255)

class Meta:
verbose_name = 'contato'
verbose_name_plural = 'contatos'

def __str__(self):
return self.value
44 changes: 44 additions & 0 deletions eventex/core/tests/test_model_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase
from eventex.core.models import Speaker, Contact
from django.core.exceptions import ValidationError


class ContactModelTest(TestCase):
def setUp(self):
self.speaker = Speaker.objects.create(
name='Henrique Bastos',
slug='henrique-bastos',
photo='https://cdn.hashnode.com/res/hashnode/image/upload/v1679856878830/hVpMcHC'
'Cz.jpeg?w=500&h=500&fit=crop&crop=faces&auto=compress,format&format=webp'
)

def test_email(self):
contact = Contact.objects.create(
speaker=self.speaker,
kind=Contact.EMAIL,
value='[email protected]'
)

self.assertTrue(Contact.objects.exists())

def test_phone(self):
contact = Contact.objects.create(
speaker=self.speaker,
kind=Contact.PHONE,
value='21-996186180'
)

self.assertTrue(Contact.objects.exists())

def test_choices(self):
"""Contact kind should be limited to E or P"""
contact = Contact(speaker=self.speaker, kind='A', value='B')
self.assertRaises(ValidationError, contact.full_clean)

def test_str(self):
contact = Contact(
speaker=self.speaker,
kind=Contact.EMAIL,
value='[email protected]'
)
self.assertEqual('[email protected]', str(contact))

0 comments on commit eee8398

Please sign in to comment.