Skip to content

Commit ee488be

Browse files
renzonrenzon
authored andcommitted
Implemented Cohort Page
close #44
1 parent 9d295fd commit ee488be

File tree

20 files changed

+286
-9
lines changed

20 files changed

+286
-9
lines changed

pythonpro/cohorts/__init__.py

Whitespace-only changes.

pythonpro/cohorts/admin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib import admin
2+
3+
from pythonpro.cohorts.models import Cohort
4+
5+
6+
@admin.register(Cohort)
7+
class ModuleAdmin(admin.ModelAdmin):
8+
list_display = 'title start end'.split()
9+
ordering = ('-start',)

pythonpro/cohorts/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CohortsConfig(AppConfig):
5+
name = 'pythonpro.cohorts'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pythonpro.cohorts.facade import get_all_cohorts_desc
2+
3+
4+
def global_settings(request):
5+
# return any necessary values
6+
if not request.user.is_authenticated:
7+
return {}
8+
9+
return {
10+
'ALL_COHORTS': get_all_cohorts_desc(),
11+
}

pythonpro/cohorts/facade.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pythonpro.cohorts.models import Cohort as _Cohort
2+
3+
4+
def get_all_cohorts_desc():
5+
return tuple(_Cohort.objects.order_by('-start'))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Generated by Django 2.0.6 on 2018-06-03 23:26
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
initial = True
10+
11+
dependencies = [
12+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Cohort',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('title', models.CharField(max_length=50)),
21+
('slug', models.SlugField()),
22+
('image', models.ImageField(upload_to='cohorts/')),
23+
('quote', models.TextField()),
24+
('mail_list', models.URLField()),
25+
('forum_post', models.URLField()),
26+
('start', models.DateField()),
27+
('end', models.DateField()),
28+
],
29+
),
30+
migrations.CreateModel(
31+
name='CohortStudent',
32+
fields=[
33+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
34+
('cohort', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='cohorts.Cohort')),
35+
(
36+
'user',
37+
models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)
38+
),
39+
],
40+
),
41+
migrations.AddField(
42+
model_name='cohort',
43+
name='students',
44+
field=models.ManyToManyField(through='cohorts.CohortStudent', to=settings.AUTH_USER_MODEL),
45+
),
46+
]

pythonpro/cohorts/migrations/__init__.py

Whitespace-only changes.

pythonpro/cohorts/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.contrib.auth import get_user_model
2+
from django.db import models
3+
4+
# Create your models here.
5+
from django.urls import reverse
6+
7+
8+
class Cohort(models.Model):
9+
title = models.CharField(max_length=50)
10+
slug = models.SlugField()
11+
image = models.ImageField(upload_to='cohorts/')
12+
quote = models.TextField()
13+
mail_list = models.URLField()
14+
forum_post = models.URLField()
15+
start = models.DateField()
16+
end = models.DateField()
17+
students = models.ManyToManyField(get_user_model(), through='CohortStudent')
18+
19+
def get_absolute_url(self):
20+
return reverse('cohorts:detail', kwargs={'slug': self.slug})
21+
22+
23+
class CohortStudent(models.Model):
24+
cohort = models.ForeignKey(Cohort, on_delete=models.DO_NOTHING)
25+
user = models.ForeignKey(get_user_model(), on_delete=models.DO_NOTHING)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{% extends 'core/base.html' %}
2+
{% load static %}
3+
4+
{% block title %}Turma {{ cohort.title }}{% endblock %}
5+
6+
{% block body %}
7+
<div class="container">
8+
<div class="row">
9+
<div class="col">
10+
<h1 class="mt-4 mb-3">Turma {{ cohort.title }}</h1>
11+
<img src="{{ cohort.image.url }}" class="rounded float-left mr-3" alt="Foto de {{ cohort.title }}"
12+
width="300px" height="300px">
13+
<blockquote class="blockquote">
14+
<p class="mb-0">"{{ cohort.quote }}"</p>
15+
<footer class="blockquote-footer">{{ cohort.title }}
16+
</footer>
17+
</blockquote>
18+
<dt>
19+
Inicio:
20+
</dt>
21+
<dd>
22+
{{ cohort.start }}
23+
</dd>
24+
<dt>
25+
Fim:
26+
</dt>
27+
<dd>
28+
{{ cohort.end }}
29+
</dd>
30+
31+
</div>
32+
</div>
33+
<div class="row">
34+
<div class="col">
35+
<h2 class="mt-5">Intruções</h2>
36+
<dt>Passo 1</dt>
37+
<dd>Cadastre-se na <a href="{{ cohort.mail_list }}" target="_blank">lista de emails da turma</a></dd>
38+
<dt>Passo 2</dt>
39+
<dd>Se apresente no <a href="{{ cohort.forum_post }}" target="_blank">post do fórum</a></dd>
40+
<dt>Passo 3</dt>
41+
<dd>Entre no nosso <a href="https://t.me/joinchat/DZ2HMkC_wRSHCm5YwIM1UQ" target="_blank">grupo do Telegram</a> para tirar dúvidas e interagir!</dd>
42+
</div>
43+
</div>
44+
</div>
45+
{% endblock body %}

pythonpro/cohorts/tests.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from os import path
2+
3+
import pytest
4+
from django.core.files.uploadedfile import SimpleUploadedFile
5+
from django.template.defaultfilters import date
6+
from django.urls import reverse
7+
from model_mommy import mommy
8+
9+
from pythonpro import settings
10+
from pythonpro.cohorts.models import Cohort
11+
from pythonpro.django_assertions import dj_assert_contains, dj_assert_not_contains
12+
13+
_img_path = path.join(settings.BASE_DIR, 'pythonpro', 'core', 'static', 'img', 'instructors', 'renzo-nuccitelli.png')
14+
15+
16+
@pytest.fixture
17+
def cohort(client, django_user_model):
18+
user = mommy.make(django_user_model)
19+
client.force_login(user)
20+
image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(),
21+
content_type='image/png')
22+
cohort = mommy.make(Cohort, slug='guido-van-rossum', students=[user], image=image)
23+
return cohort
24+
25+
26+
@pytest.fixture
27+
def resp(client, cohort):
28+
resp = client.get(reverse('cohorts:detail', kwargs={'slug': cohort.slug}), secure=True)
29+
return resp
30+
31+
32+
def test_cohort_links_for_logged_user(client, django_user_model):
33+
user = mommy.make(django_user_model)
34+
client.force_login(user)
35+
image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(),
36+
content_type='image/png')
37+
cohorts = mommy.make(Cohort, 4, image=image)
38+
resp = client.get('/', secure=True)
39+
for c in cohorts:
40+
dj_assert_contains(resp, c.get_absolute_url())
41+
42+
43+
@pytest.mark.django_db
44+
def test_cohort_links_not_avaliable_for_no_user(client):
45+
image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(),
46+
content_type='image/png')
47+
cohorts = mommy.make(Cohort, 4, image=image)
48+
resp = client.get('/', secure=True)
49+
for c in cohorts:
50+
dj_assert_not_contains(resp, c.get_absolute_url())
51+
52+
53+
def test_status_code(resp):
54+
assert 200 == resp.status_code
55+
56+
57+
@pytest.mark.parametrize('property_name', 'title mail_list forum_post'.split())
58+
def test_cohort_propeties(cohort, resp, property_name):
59+
dj_assert_contains(resp, getattr(cohort, property_name))
60+
61+
62+
def test_cohort_img(cohort: Cohort, resp):
63+
dj_assert_contains(resp, cohort.image.url)
64+
65+
66+
def test_cohort_start(cohort: Cohort, resp):
67+
dj_assert_contains(resp, date(cohort.start))
68+
69+
70+
def test_cohort_end(cohort: Cohort, resp):
71+
dj_assert_contains(resp, date(cohort.end))

0 commit comments

Comments
 (0)