SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 2ac78ba5 authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Work on forums: presentation, etc

parent 7a621e32
No related branches found
No related tags found
No related merge requests found
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
from django.db import models
class ForumQuerySet(models.QuerySet):
def anchors(self):
"""Return only the Forums which do not have a parent."""
return self.filter(parent_object_id__isnull=True)
...@@ -8,6 +8,8 @@ from django.core.urlresolvers import reverse ...@@ -8,6 +8,8 @@ from django.core.urlresolvers import reverse
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from .managers import ForumQuerySet
class Forum(models.Model): class Forum(models.Model):
""" """
...@@ -42,6 +44,8 @@ class Forum(models.Model): ...@@ -42,6 +44,8 @@ class Forum(models.Model):
object_id_field='parent_object_id', object_id_field='parent_object_id',
related_query_name='parent_forums') related_query_name='parent_forums')
objects = ForumQuerySet.as_manager()
class Meta: class Meta:
ordering = ['name',] ordering = ['name',]
...@@ -61,6 +65,21 @@ class Forum(models.Model): ...@@ -61,6 +65,21 @@ class Forum(models.Model):
nr += self.posts.all().count() nr += self.posts.all().count()
return nr return nr
def posts_hierarchy_id_list(self):
id_list = []
for post in self.posts.all():
id_list += post.posts_hierarchy_id_list()
return id_list
@property
def latest_post(self):
id_list = self.posts_hierarchy_id_list()
print ('forum post id_list: %s' % id_list)
try:
return Post.objects.filter(id__in=id_list).order_by('-posted_on').first()
except:
return None
class Post(models.Model): class Post(models.Model):
""" """
...@@ -109,3 +128,10 @@ class Post(models.Model): ...@@ -109,3 +128,10 @@ class Post(models.Model):
if self.followup_posts: if self.followup_posts:
nr += self.followup_posts.all().count() nr += self.followup_posts.all().count()
return nr return nr
def posts_hierarchy_id_list(self):
id_list = [self.id]
for post in self.followup_posts.all():
id_list += post.posts_hierarchy_id_list()
print ('post %s id_list: %s' % (self.id, id_list))
return id_list
<li>
<a href="{{ forum.get_absolute_url }}">{{ forum }}</a>
{% if forum.child_forums.all|length > 0 %}
<ul class="list-unstyled forumList">
{% for child in forum.child_forums.all %}
{% include 'forums/forum_as_li.html' with forum=child %}
{% endfor %}
</ul>
{% endif %}
</li>
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
<div class="col-12"> <div class="col-12">
<h3 class="highlight">{{ forum.name }}</h3> <h3 class="highlight">{{ forum.name }}</h3>
{% if perms.forums.can_add_forum %}
<p>
<a href="{% url 'forums:forum_create' parent_model='forum' parent_id=forum.id %}">Create a (sub)Forum within this one</a>
</p>
{% endif %}
{% for post in forum.posts.all %} {% for post in forum.posts.all %}
{% include 'forums/post_card.html' with post=post %} {% include 'forums/post_card.html' with post=post %}
{% endfor %} {% endfor %}
......
...@@ -34,5 +34,4 @@ $("#id_name").keyup(function() { ...@@ -34,5 +34,4 @@ $("#id_name").keyup(function() {
</div> </div>
</div> </div>
{% endblock content %} {% endblock content %}
...@@ -15,10 +15,39 @@ ...@@ -15,10 +15,39 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<h3 class="highlight">Forums</h3> <h3 class="highlight">Forums</h3>
<ul> <ul>
<li><a href="{% url 'forums:forum_create' %}">Create a new Forum</a></li> <li><a href="{% url 'forums:forum_create' %}">Create a new Forum</a></li>
</ul> </ul>
<div class="card-columns">
{% for forum in object_list %}
<div class="card">
<div class="card-header">
<a href="{{ forum.get_absolute_url }}">{{ forum }}</a>
</div>
<div class="card-body">
Forum description
{% if forum.child_forums.all|length > 0 %}
<hr/>
<p>Descendants:</p>
<ul class="list-unstyled forumList">
{% for child in forum.child_forums.all %}
{% include 'forums/forum_as_li.html' with forum=child %}
{% endfor %}
</ul>
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<h3 class="highlight">Latest postings</h3>
<table class="table"> <table class="table">
<thead class="thead-default"> <thead class="thead-default">
<tr> <tr>
...@@ -32,8 +61,8 @@ ...@@ -32,8 +61,8 @@
{% for forum in object_list %} {% for forum in object_list %}
<tr> <tr>
<td><a href="{{ forum.get_absolute_url }}">{{ forum }}</a></td> <td><a href="{{ forum.get_absolute_url }}">{{ forum }}</a></td>
<td>{% if forum.posts.all|length > 0 %}{{ forum.posts.first }}{% else %}No posts yet{% endif %}</td> <td>{{ forum.latest_post }}</td>
<td>{% if forum.posts.all|length > 0 %}{{ forum.posts.first.posted_on|date:"Y-m-d" }}{% else %}-{% endif %}</td> <td>{{ forum.latest_post.posted_on|date:"Y-m-d" }}</td>
<td>{{ forum.nr_posts }}</td> <td>{{ forum.nr_posts }}</td>
</tr> </tr>
{% empty %} {% empty %}
......
...@@ -7,6 +7,11 @@ from django.conf.urls import url ...@@ -7,6 +7,11 @@ from django.conf.urls import url
from . import views from . import views
urlpatterns = [ urlpatterns = [
url(
r'^forum/(?P<parent_model>[a-z]+)/(?P<parent_id>[0-9]+)/add/$',
views.ForumCreateView.as_view(),
name='forum_create'
),
url( url(
r'^add/$', r'^add/$',
views.ForumCreateView.as_view(), views.ForumCreateView.as_view(),
......
...@@ -24,8 +24,14 @@ class ForumCreateView(PermissionsMixin, CreateView): ...@@ -24,8 +24,14 @@ class ForumCreateView(PermissionsMixin, CreateView):
def get_initial(self): def get_initial(self):
initial = super().get_initial() initial = super().get_initial()
parent_model = self.kwargs.get('parent_model')
parent_object_id = self.kwargs.get('parent_id')
if parent_model == 'forum':
parent_content_type = ContentType.objects.get(app_label='forums', model='forum')
initial.update({ initial.update({
'moderators': self.request.user 'moderators': self.request.user,
'parent_content_type': parent_content_type,
'parent_object_id': parent_object_id,
}) })
return initial return initial
...@@ -39,6 +45,11 @@ class ForumListView(ListView): ...@@ -39,6 +45,11 @@ class ForumListView(ListView):
model = Forum model = Forum
template_name = 'forum_list.html' template_name = 'forum_list.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['latest_'] = Forum.objects.order_by
return context
class PostCreateView(CreateView): class PostCreateView(CreateView):
model = Post model = Post
......
...@@ -12,3 +12,7 @@ ...@@ -12,3 +12,7 @@
margin: 1rem; margin: 1rem;
padding-left: 0.4rem; padding-left: 0.4rem;
} }
.forumList {
padding-left: 0.4rem;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment