Skip to content

Commit

Permalink
Managers
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspdf committed Jun 21, 2024
1 parent addc504 commit c0e6cd4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
13 changes: 12 additions & 1 deletion eventex/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ContactInLine(admin.TabularInline):
class SpeakerModelAdmin(admin.ModelAdmin):
inlines = [ContactInLine]
prepopulated_fields = {'slug': ('name',)}
list_display = ['name', 'photo_img', 'website_link']
list_display = ['name', 'photo_img', 'website_link', 'email', 'phone']

def website_link(self, obj):
return format_html('<a href="{0}">{0}</a>', obj.website)
Expand All @@ -21,8 +21,19 @@ def website_link(self, obj):
def photo_img(self, obj):
return format_html('<img width="32px" src="{}" />', obj.photo)

photo_img.allow_tags = True
photo_img.short_description = 'foto'

def email(self, obj):
return obj.contact_set.emails().first()

email.short_description = 'e-mail'

def phone(self, obj):
return obj.contact_set.phones().first()

email.short_description = 'telefone'


admin.site.register(Speaker, SpeakerModelAdmin)
admin.site.register(Talk)
19 changes: 19 additions & 0 deletions eventex/core/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db import models


class KindQuerySet(models.QuerySet):
def emails(self):
return self.filter(kind=self.model.EMAIL)

def phones(self):
return self.filter(kind=self.model.PHONE)


class PeriodManager(models.Manager):
MIDDAY = '12:00'

def at_morning(self):
return self.filter(start__lt=self.MIDDAY)

def at_afternoon(self):
return self.filter(start__gte=self.MIDDAY)
7 changes: 7 additions & 0 deletions eventex/core/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import models
from django.shortcuts import resolve_url as r

from eventex.core.managers import KindQuerySet, PeriodManager


class Speaker(models.Model):
name = models.CharField('nome', max_length=255)
Expand All @@ -23,6 +25,7 @@ def get_absolute_url(self):
class Contact(models.Model):
EMAIL = 'E'
PHONE = 'P'

KINDS = (
(EMAIL, 'Email'),
(PHONE, 'Phone'),
Expand All @@ -31,6 +34,8 @@ class Contact(models.Model):
kind = models.CharField('tipo', max_length=1, choices=KINDS)
value = models.CharField('valor', max_length=255)

objects = KindQuerySet.as_manager()

class Meta:
verbose_name = 'contato'
verbose_name_plural = 'contatos'
Expand All @@ -45,6 +50,8 @@ class Talk(models.Model):
description = models.TextField('descrição', blank=True)
speakers = models.ManyToManyField('Speaker', verbose_name='palestrantes', blank=True)

objects = PeriodManager()

class Meta:
verbose_name_plural = 'palestras'
verbose_name = 'palestra'
Expand Down
22 changes: 22 additions & 0 deletions eventex/core/tests/test_model_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ def test_str(self):
value='[email protected]'
)
self.assertEqual('[email protected]', str(contact))


class ContactManagerTest(TestCase):
def setUp(self):
s = Speaker.objects.create(
name='Henrique Bastos',
slug='henrique-bastos',
photo='http://hbn.link/hb-pic'
)

s.contact_set.create(kind=Contact.EMAIL, value='[email protected]')
s.contact_set.create(kind=Contact.PHONE, value='21-996186180')

def test_email(self):
qs = Contact.objects.emails()
expected = ['[email protected]']
self.assertQuerysetEqual(qs, expected, lambda o: o.value)

def test_phones(self):
qs = Contact.objects.phones()
expected = ['21-996186180']
self.assertQuerysetEqual(qs, expected, lambda o: o.value)
21 changes: 21 additions & 0 deletions eventex/core/tests/test_model_talk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.test import TestCase

from eventex.core.managers import PeriodManager
from eventex.core.models import Talk


Expand Down Expand Up @@ -38,3 +40,22 @@ def test_start_null(self):

def test_str(self):
self.assertEqual('Título da Palestra', str(self.talk))


class PeriodManagerTest(TestCase):
def setUp(self):
Talk.objects.create(title='Morning Talk', start='11:59')
Talk.objects.create(title='Afternoon Talk', start='12:00')

def test_manager(self):
self.assertIsInstance(Talk.objects, PeriodManager)

def test_at_morning(self):
qs = Talk.objects.at_morning()
expected = ['Morning Talk']
self.assertQuerysetEqual(qs, expected, lambda o: o.title)

def test_at_afternoon(self):
qs = Talk.objects.at_afternoon()
expected = ['Afternoon Talk']
self.assertQuerysetEqual(qs, expected, lambda o: o.title)
4 changes: 2 additions & 2 deletions eventex/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def speaker_detail(request, slug):

def talk_list(request):
context = {
'morning_talks': Talk.objects.filter(start__lt='12:00'),
'afternoon_talks': Talk.objects.filter(start__gte='12:00')
'morning_talks': Talk.objects.at_morning(),
'afternoon_talks': Talk.objects.at_afternoon()
}

return render(request, 'core/talk_list.html', context)

0 comments on commit c0e6cd4

Please sign in to comment.