Skip to content

Commit

Permalink
#636: enable large custom seed upload through a TXT file, don't displ…
Browse files Browse the repository at this point in the history
…ay in an HTML field if too large, back up before overwriting, some optimizing
  • Loading branch information
Fasand committed Jul 24, 2023
1 parent 6f2a390 commit 5bd14fc
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 170 deletions.
20 changes: 19 additions & 1 deletion Seeder/harvests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,34 @@ class Meta:

class InternalTopicCollectionEditForm(InternalTopicCollectionForm):
files_to_delete = forms.MultipleChoiceField(required=False)
custom_seeds_file = forms.FileField(required=False, help_text=_L(
"Provide a text file with one seed per line which will overwrite "
"all custom_seeds. The original custom_seeds will be backed up to "
"the media/seeds/backup folder."))
''' Maximum length of custom seeds (chars) to be displayed & editable '''
CUSTOM_SEEDS_MAXLEN = 1 * 1000 * 1000 # 1MB

def __init__(self, attachment_list, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['files_to_delete']._set_choices(
[(file.id, str(file)) for file in attachment_list]
)
# Don't show custom seeds textfield if there's too many of them
self.custom_seeds_too_large = (
len(self.initial.get("custom_seeds", ""))
> self.CUSTOM_SEEDS_MAXLEN)
if self.custom_seeds_too_large:
del self.fields["custom_seeds"]
# Remove initial value so it doesn't get "re-saved"
if "custom_seeds" in self.initial:
del self.initial["custom_seeds"]
self.fields["custom_seeds_file"].help_text += "<br /><b>" + _(
"Custom seeds field is disabled because there are too many "
"seeds to be displayed in an HTML text field.") + "</b>"

class Meta(InternalTopicCollectionForm.Meta):
fields = InternalTopicCollectionForm.Meta.fields + \
('files_to_delete',)
('files_to_delete', 'custom_seeds_file')


class ExternalTopicCollectionForm(forms.ModelForm):
Expand Down
42 changes: 35 additions & 7 deletions Seeder/harvests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.views.generic.base import TemplateView
from django.views.generic import DetailView, FormView, View
from django.conf import settings
from django.utils.safestring import mark_safe

from core import generic_views
from comments.views import CommentViewGeneric
Expand Down Expand Up @@ -475,19 +476,46 @@ def get_form(self, form_class=None):
return form_class(files, **self.get_form_kwargs())

def form_valid(self, form):
topic = form.save()
topic.pair_custom_seeds()

topic = form.save(commit=False)
form.save_m2m() # must save m2m when commit=False
# Create and delete attachments
ids_to_delete = form.cleaned_data['files_to_delete']
for att in models.Attachment.objects.filter(id__in=ids_to_delete):
att.file.delete()
att.delete()

for each in form.cleaned_data["attachments"]:
models.Attachment.objects.create(
file=each,
topic_collection=topic
)
file=each, topic_collection=topic)
# If a custom seeds file is uploaded, backup and replace TC.custom_seeds
new_custom_seeds = form.cleaned_data.get("custom_seeds_file")
if new_custom_seeds:
url = topic.backup_custom_seeds()
try:
topic.custom_seeds = new_custom_seeds.read().decode("utf-8")
topic.save() # full save, including new large custom seeds
except UnicodeDecodeError:
messages.error(self.request, _("Cannot decode file to UTF-8"))
messages.success(self.request, mark_safe(_(
"Original custom_seeds have been backed to: <a href='%(url)s' "
"target='_blank'>%(url)s</a>"
) % {"url": url}))
messages.warning(self.request, _(
"Uploaded custom seeds will not be paired automatically"))
else:
# Small edits shouldn't touch custom_seeds because it'll never load
if form.custom_seeds_too_large:
topic.save(update_fields=(
set(form.fields.keys()) - set([
"custom_seeds", "custom_sources", "attachments",
"files_to_delete", "custom_seeds_file",
])
))
# Only pair custom seeds if there aren't too many of them and they
# haven't just been imported
else:
topic.save() # full save, not many custom seeds
topic.pair_custom_seeds()

return HttpResponseRedirect(topic.get_absolute_url())


Expand Down
Loading

0 comments on commit 5bd14fc

Please sign in to comment.