Skip to content
Open

WIP #5122

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mydatabase
node_modules/
src/plugins/*
src/media/profile_images/*
src/files/discussions

# PyCharm
.idea
Expand Down
11 changes: 7 additions & 4 deletions src/discussion/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ class ThreadAdmin(admin_utils.ArticleFKModelAdmin):
"owner__first_name",
"owner__last_name",
"owner__email",
"post__body",
"posts_related__body",
)
raw_id_fields = ("owner", "article", "preprint")
filter_horizontal = ("participants",)

inlines = [admin_utils.PostInline]


class PostAdmin(admin.ModelAdmin):
list_display = ("_post", "thread", "owner", "posted", "_journal")
list_filter = ("thread__article__journal", "posted")
list_display = ("_post", "thread", "owner", "posted", "edited", "is_system_message", "_journal")
list_filter = ("thread__article__journal", "posted", "is_system_message")
search_fields = (
"pk",
"body",
Expand All @@ -50,7 +51,9 @@ def _post(self, obj):
return truncatewords_html(obj.body, 10) if obj else ""

def _journal(self, obj):
return obj.thread.article.journal if obj else ""
if obj and obj.thread and obj.thread.article:
return obj.thread.article.journal
return ""


admin_list = [
Expand Down
39 changes: 26 additions & 13 deletions src/discussion/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@
class ThreadForm(forms.ModelForm):
class Meta:
model = models.Thread
fields = ("subject",)

def __init__(self, *args, **kwargs):
fields = (
"subject",
)

def __init__(
self,
*args,
**kwargs,
):
self.object = kwargs.pop("object")
self.object_type = kwargs.pop("object_type")
self.owner = kwargs.pop("owner")
super(ThreadForm, self).__init__(*args, **kwargs)

def save(self, commit=True):
thread = super(ThreadForm, self).save(commit=False)

# Attach FK + owner BEFORE validation so model.clean() passes
if self.object_type == "article":
thread.article = self.object
self.instance.article = self.object
self.instance.preprint = None
else:
thread.preprint = self.object

thread.owner = self.owner

self.instance.preprint = self.object
self.instance.article = None

self.instance.owner = self.owner

def save(
self,
commit=True,
):
thread = super(ThreadForm, self).save(
commit=False,
)
# Instance already has article/preprint/owner set in __init__
if commit:
thread.save()

return thread
return thread
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.2.20 on 2025-10-10 12:23

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('discussion', '0004_auto_20200925_1933'),
]

operations = [
migrations.AddField(
model_name='thread',
name='participants',
field=models.ManyToManyField(blank=True, help_text='Users who are allowed to access this thread.', related_name='accessible_threads', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='post',
name='thread',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='posts_related', to='discussion.thread'),
),
migrations.AlterField(
model_name='thread',
name='last_updated',
field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),
),
]
19 changes: 19 additions & 0 deletions src/discussion/migrations/0006_post_is_system_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('discussion', '0005_thread_participants_alter_post_thread_and_more'),
]

operations = [
migrations.AddField(
model_name='post',
name='is_system_message',
field=models.BooleanField(
default=False,
help_text='System-generated message, e.g. title change or participant added.',
),
),
]
24 changes: 24 additions & 0 deletions src/discussion/migrations/0007_post_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0001_initial'),
('discussion', '0006_post_is_system_message'),
]

operations = [
migrations.AddField(
model_name='post',
name='file',
field=models.ForeignKey(
blank=True,
help_text='Optional file attachment.',
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to='core.file',
),
),
]
20 changes: 20 additions & 0 deletions src/discussion/migrations/0008_post_edited.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('discussion', '0007_post_file'),
]

operations = [
migrations.AddField(
model_name='post',
name='edited',
field=models.DateTimeField(
blank=True,
help_text='Timestamp of the last edit, if any.',
null=True,
),
),
]
Loading