Skip to content

Commit

Permalink
urlize description and splitting courses into active and finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeygrigorev committed Oct 15, 2024
1 parent df4091b commit 4b0166b
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 7 deletions.
9 changes: 9 additions & 0 deletions add_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
title="Fake Course",
description="This is a fake course.",
slug="fake-course",
finished=False,
)
course.save()

Expand Down Expand Up @@ -386,3 +387,11 @@ def join_possible_answers(answers: list) -> str:
options=criterion["options"],
)


course2 = Course(
title="Fake Course 2",
description="This is a fake course.",
slug="fake-course-2",
finished=True,
)
course2.save()
18 changes: 18 additions & 0 deletions courses/migrations/0018_course_finished.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.14 on 2024-10-15 12:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('courses', '0017_alter_projectsubmission_learning_in_public_links_and_more'),
]

operations = [
migrations.AddField(
model_name='course',
name='finished',
field=models.BooleanField(default=False, help_text='Whether the course has finished.'),
),
]
6 changes: 6 additions & 0 deletions courses/models/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class Course(models.Model):
+ "We use that for deciding whether to show the leaderboard.",
)

finished = models.BooleanField(
default=False,
blank=False,
help_text="Whether the course has finished.",
)

faq_document_url = models.URLField(
blank=True,
validators=[URLValidator()],
Expand Down
4 changes: 3 additions & 1 deletion courses/templates/courses/course.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% extends 'base.html' %}

{% load tz %}
{% load custom_filters %}

{% block breadcrumbs %}
<li><a href="{% url 'course' course.slug %}">{{ course.title }}</a></li>
Expand All @@ -9,7 +11,7 @@
<h2>{{ course.title }}</h2>

<div>
<p>{{ course.description }}</p>
<p>{{ course.description | urlize_target_blank }}</p>
</div>

<div>
Expand Down
18 changes: 16 additions & 2 deletions courses/templates/courses/course_list.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
{% extends 'base.html' %}

{% block content %}
<h2>Courses</h2>
<h2>DataTalks.Club courses</h2>
<div>
Welcome to our course management platform! You can learn more about
our courses in our webpage <a href="https://datatalks.club/" target="_blank">https://datatalks.club/</a>.
</div>

<h3 class="pt-4">Active courses</h3>
<ul>
{% for course in active_courses %}
<li><a href="{% url 'course' course.slug %}">{{ course.title }}</a></li>
{% endfor %}
</ul>

<h3 class="pt-3">Finished courses</h3>
<ul>
{% for course in courses %}
{% for course in finished_courses %}
<li><a href="{% url 'course' course.slug %}">{{ course.title }}</a></li>
{% endfor %}
</ul>

{% endblock %}
4 changes: 3 additions & 1 deletion courses/templates/homework/homework.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends 'base.html' %}

{% load static %}
{% load custom_filters %}


{% block breadcrumbs %}
<li><a href="{% url 'course' course.slug %}">{{ course.title }}</a></li>
Expand Down Expand Up @@ -48,7 +50,7 @@ <h2 class="mb-3 text-center">
{% endif %}

<div>
<p class="text-muted">{{ homework.description }}</p>
<p class="text-muted">{{ homework.description | urlize_target_blank }}</p>
</div>

<div>
Expand Down
4 changes: 3 additions & 1 deletion courses/templates/projects/project.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends 'base.html' %}

{% load static %}
{% load custom_filters %}


{% block breadcrumbs %}
<li><a href="{% url 'course' course.slug %}">{{ course.title }}</a></li>
Expand Down Expand Up @@ -51,7 +53,7 @@ <h2 class="mb-3 text-center">{{ project.title }} for <a href="{% url 'course' co
{% endif %}
{% endif %}

<p class="text-muted">{{ project.description }}</p>
<p class="text-muted">{{ project.description | urlize_target_blank }}</p>

<h3 class="mb-3">Project submission</h3>

Expand Down
Empty file.
19 changes: 19 additions & 0 deletions courses/templatetags/custom_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from django.utils.html import urlize as urlize_impl

register = template.Library()


@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def urlize_target_blank(value, limit=30, autoescape=None):
return mark_safe(
urlize_impl(
value,
trim_url_limit=int(limit),
nofollow=True,
autoescape=autoescape,
).replace("<a", '<a target="_blank"')
)
21 changes: 19 additions & 2 deletions courses/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,26 @@


def course_list(request):
courses = Course.objects.all()
courses = Course.objects.all().order_by("-id")

active_courses = []
finished_courses = []

for course in courses:
if course.finished:
finished_courses.append(course)
else:
active_courses.append(course)

context = {
"active_courses": active_courses,
"finished_courses": finished_courses,
}

return render(
request, "courses/course_list.html", {"courses": courses}
request,
"courses/course_list.html",
context,
)


Expand Down

0 comments on commit 4b0166b

Please sign in to comment.