Skip to content

Commit

Permalink
Many-to-one relationships e atributo _set
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspdf committed Oct 19, 2023
1 parent f608d29 commit 3a97555
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 5 deletions.
10 changes: 9 additions & 1 deletion pypro/modulos/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from django.contrib import admin
from ordered_model.admin import OrderedModelAdmin

from pypro.modulos.models import Modulo
from pypro.modulos.models import Aula, Modulo


@admin.register(Modulo)
class ModuloAdmin(OrderedModelAdmin):
list_display = ('titulo', 'publico', 'move_up_down_links')
prepopulated_fields = {'slug': ('titulo',)}


@admin.register(Aula)
class AulaAdmin(OrderedModelAdmin):
list_display = ('titulo', 'modulo', 'order', 'move_up_down_links')
list_filter = ('modulo',)
ordering = ('modulo', 'order')
prepopulated_fields = {'slug': ('titulo',)}
4 changes: 4 additions & 0 deletions pypro/modulos/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ def listar_modulos_ordenados() -> List[Modulo]:

def encontrar_modulo(slug: str) -> Modulo:
return Modulo.objects.get(slug=slug)


def listar_aulas_de_modulo_ordenadas(modulo: Modulo):
return list(modulo.aula_set.order_by('order').all())
46 changes: 46 additions & 0 deletions pypro/modulos/migrations/0005_aula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 4.2.6 on 2023-10-19 22:41

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


class Migration(migrations.Migration):

dependencies = [
("modulos", "0004_slug_unico_e_nao_nulo"),
]

operations = [
migrations.CreateModel(
name="Aula",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"order",
models.PositiveIntegerField(
db_index=True, editable=False, verbose_name="order"
),
),
("titulo", models.CharField(max_length=64)),
("slug", models.SlugField(unique=True)),
(
"modulo",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="modulos.modulo"
),
),
],
options={
"ordering": ("order",),
"abstract": False,
},
),
]
19 changes: 19 additions & 0 deletions pypro/modulos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@ class Modulo(OrderedModel):
class Meta(OrderedModel.Meta):
pass

# def __init__(self, *args, **kwargs):
# super().__init__(args, kwargs)

def __str__(self):
return self.titulo

def get_absolute_url(self):
return reverse('modulos:detalhe', kwargs={'slug': self.slug})


class Aula(OrderedModel):
titulo = models.CharField(max_length=64)
slug = models.SlugField(unique=True)
modulo = models.ForeignKey('Modulo', on_delete=models.PROTECT)
order_with_respect_to = 'modulo'

class Meta(OrderedModel.Meta):
pass

def __str__(self):
return self.titulo

def get_absolute_url(self):
return reverse('modulos:aula', kwargs={'slug': self.slug})
6 changes: 6 additions & 0 deletions pypro/modulos/templates/modulos/modulo_detalhe.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ <h1 class="mt-4 mb-3">{{ modulo.titulo }}</h1>
<dt>Descrição</dt>
<dd>{{ modulo.descricao }}</dd>
</dl>
<h2>Aulas</h2>
<ol>
{% for aula in aulas %}
<li>{{ aula.titulo }}</li>
{% endfor %}
</ol>
</div>
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions pypro/modulos/tests/test_modulo_detalhe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pypro.django_assertions import assert_contains
from model_mommy import mommy

from pypro.modulos.models import Modulo
from pypro.modulos.models import Modulo, Aula


@pytest.fixture
Expand All @@ -12,7 +12,12 @@ def modulo(db):


@pytest.fixture
def resp(client, modulo):
def aulas(modulo):
return mommy.make(Aula, 3, modulo=modulo)


@pytest.fixture
def resp(client, modulo, aulas):
resp = client.get(reverse('modulos:detalhe', kwargs={'slug': modulo.slug}))
return resp

Expand All @@ -27,3 +32,8 @@ def test_descricao(resp, modulo: Modulo):

def test_publico(resp, modulo: Modulo):
assert_contains(resp, modulo.publico)


def test_aulas_titulos(resp, aulas):
for aula in aulas:
assert_contains(resp, aula.titulo)
3 changes: 2 additions & 1 deletion pypro/modulos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

def detalhe(request, slug):
modulo = facade.encontrar_modulo(slug)
return render(request, 'modulos/modulo_detalhe.html', {'modulo': modulo})
aulas = facade.listar_aulas_de_modulo_ordenadas(modulo)
return render(request, 'modulos/modulo_detalhe.html', {'modulo': modulo, 'aulas': aulas})
2 changes: 1 addition & 1 deletion pypro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@


# Configuração do ambiente de desenvolvimento
STATIC_URL = 'static/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
Expand Down

0 comments on commit 3a97555

Please sign in to comment.