Skip to content

Commit

Permalink
Merge pull request #174 from Ecotrust/bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
pollardld authored Oct 21, 2024
2 parents c927241 + 5d37198 commit d9d2898
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 5 deletions.
113 changes: 113 additions & 0 deletions TEKDB/TEKDB/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
from django.utils.html import format_html
from django.utils.translation import ugettext, ugettext_lazy as _
from dal import autocomplete
from mimetypes import guess_type
from django.templatetags.static import static
# from moderation.admin import ModerationAdmin
import nested_admin
from ckeditor.widgets import CKEditorWidget
from reversion.admin import VersionAdmin

from .forms import MediaBulkUploadForm
from .models import *

from TEKDB.settings import ADMIN_SITE_HEADER
Expand Down Expand Up @@ -440,6 +443,116 @@ class CitationsAdmin(RecordAdminProxy, RecordModelAdmin):
)
form = CitationsForm


# * Bulk Media Upload Admin
class MediaBulkUploadAdmin(admin.ModelAdmin):
form = MediaBulkUploadForm

list_display = ('mediabulkname','mediabulkdate','modifiedbydate','enteredbydate',)

def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
places = form.cleaned_data.get('places')
resources = form.cleaned_data.get('resources')
citations = form.cleaned_data.get('citations')
activities = form.cleaned_data.get('activities')
placeresources = form.cleaned_data.get('placeresources')

for file in request.FILES.getlist('files'):
media_instance = Media(
medianame=obj.mediabulkname,
mediadescription=obj.mediabulkdescription,
mediafile=file,
)
media_instance.save()
obj.mediabulkupload.add(media_instance)

# Add relationships
if places:
for place in places:
PlacesMediaEvents.objects.create(placeid=place, mediaid=media_instance)
if resources:
for resource in resources:
ResourcesMediaEvents.objects.create(resourceid=resource, mediaid=media_instance)
if citations:
for citation in citations:
MediaCitationEvents.objects.create(citationid=citation, mediaid=media_instance)
if activities:
for activity in activities:
ResourceActivityMediaEvents.objects.create(resourceactivityid=activity, mediaid=media_instance)
if placeresources:
for placeresource in placeresources:
PlaceResourceMediaEvents.objects.create(placeresourceid=placeresource, mediaid=media_instance)


def thumbnail_gallery(self, obj):
thumbnails = []
for media in obj.mediabulkupload.all():
# Guess the MIME type of the file
mime_type, _ = guess_type(media.mediafile.url)

file_name = media.mediafile.name

if mime_type:
if mime_type.startswith('image'):
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<img src="{}" width="100" height="100" />'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', media.mediafile.url, file_name))
elif mime_type.startswith('video'):
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<video width="100" height="100" controls>'
'<source src="{}" type="{}">'
'Your browser does not support the video tag.'
'</video>'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', media.mediafile.url, mime_type, file_name))
elif mime_type.startswith('audio'):
generic_audio_icon = static('assets/audio-x-generic.svg')
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<img src="{}" width="100" height="100" alt="Audio File" />'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', generic_audio_icon, file_name))
elif mime_type.startswith('text') or 'application/msword' in mime_type or 'application/vnd' in mime_type:
generic_doc_icon = static('assets/doc-text.svg')
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<img src="{}" width="100" height="100" alt="Document File" />'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', generic_doc_icon, file_name))
else:
# For unknown or other file types, show a generic file image
generic_file_icon = static('assets/unknown-mail.png')
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<img src="{}" width="100" height="100" alt="File" />'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', generic_file_icon, file_name))
else:
# In case the MIME type could not be determined, use a generic file icon
generic_file_icon = static('assets/unknown-mail.png')
thumbnails.append(format_html(
'<div style="display:inline-block; text-align:center; margin: 10px;">'
'<img src="{}" width="100" height="100" alt="File" />'
'<br /><span style="display:block; width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{}</span>'
'</div>', generic_file_icon, file_name))

return format_html(''.join(thumbnails))

thumbnail_gallery.short_description = 'Thumbnails'
readonly_fields = ('thumbnail_gallery',)
fieldsets = (
(None, {
'fields': ('mediabulkname', 'mediabulkdescription', 'files', 'mediabulkdate', 'places', 'resources', 'citations', 'activities', 'placeresources', 'thumbnail_gallery')
}),
)

admin.site.register(MediaBulkUpload, MediaBulkUploadAdmin)


class MediaAdmin(RecordAdminProxy, RecordModelAdmin):
readonly_fields = ('medialink',
'enteredbyname', 'enteredbytribe','enteredbytitle','enteredbydate',
Expand Down
35 changes: 35 additions & 0 deletions TEKDB/TEKDB/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django import forms
from .models import MediaBulkUpload, Media, Places, Resources, Citations, ResourcesActivityEvents, PlacesResourceMediaEvents
from .widgets import ThumbnailFileInput

class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True

class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)

def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = [single_file_clean(data, initial)]
return result

# class MultipleFileField(forms.FileField):
# widget = ThumbnailFileInput

class MediaBulkUploadForm(forms.ModelForm):
files = MultipleFileField()
mediabulkdate = forms.DateField(widget=forms.SelectDateWidget)
places = forms.ModelMultipleChoiceField(queryset=Places.objects.all(), required=False)
resources = forms.ModelMultipleChoiceField(queryset=Resources.objects.all(), required=False)
citations = forms.ModelMultipleChoiceField(queryset=Citations.objects.all(), required=False)
activities = forms.ModelMultipleChoiceField(queryset=ResourcesActivityEvents.objects.all(), required=False)
placeresources = forms.ModelMultipleChoiceField(queryset=PlacesResourceMediaEvents.objects.all(), required=False)

class Meta:
model = MediaBulkUpload
fields = ['mediabulkname', 'mediabulkdescription', 'files', 'mediabulkdate', 'places', 'resources', 'citations', 'activities', 'placeresources']
32 changes: 32 additions & 0 deletions TEKDB/TEKDB/migrations/0013_auto_20241004_1547.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.25 on 2024-10-04 22:47

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('Lookup', '0005_alter_lookupuserinfo_id'),
('TEKDB', '0012_auto_20240926_1440'),
]

operations = [
migrations.CreateModel(
name='MediaBulkUpload',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=255, null=True)),
('description', models.TextField(blank=True, null=True)),
('date', models.DateField(blank=True, default=None, null=True)),
('created_at', models.DateTimeField(auto_now_add=True, null=True)),
('updated_at', models.DateTimeField(auto_now=True, null=True)),
('user', models.ForeignKey(blank=True, db_column='user', default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, to='Lookup.lookupuserinfo', verbose_name='user')),
],
),
migrations.AddField(
model_name='media',
name='mediauploadevent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mediabulkupload', to='TEKDB.mediabulkupload'),
),
]
19 changes: 19 additions & 0 deletions TEKDB/TEKDB/migrations/0014_alter_media_mediauploadevent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-10-04 22:55

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('TEKDB', '0013_auto_20241004_1547'),
]

operations = [
migrations.AlterField(
model_name='media',
name='mediauploadevent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mediauploadevent', to='TEKDB.mediabulkupload'),
),
]
23 changes: 23 additions & 0 deletions TEKDB/TEKDB/migrations/0015_auto_20241004_1559.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-10-04 22:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('TEKDB', '0014_alter_media_mediauploadevent'),
]

operations = [
migrations.RemoveField(
model_name='media',
name='mediauploadevent',
),
migrations.AddField(
model_name='media',
name='mediabulkupload',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mediabulkupload', to='TEKDB.mediabulkupload'),
),
]
71 changes: 71 additions & 0 deletions TEKDB/TEKDB/migrations/0016_auto_20241016_1549.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Generated by Django 3.2.25 on 2024-10-16 22:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('TEKDB', '0015_auto_20241004_1559'),
]

operations = [
migrations.RemoveField(
model_name='mediabulkupload',
name='created_at',
),
migrations.RemoveField(
model_name='mediabulkupload',
name='updated_at',
),
migrations.AddField(
model_name='mediabulkupload',
name='enteredbydate',
field=models.DateTimeField(auto_now_add=True, db_column='enteredbydate', null=True, verbose_name='entered by date'),
),
migrations.AddField(
model_name='mediabulkupload',
name='enteredbyname',
field=models.CharField(blank=True, db_column='enteredbyname', max_length=25, null=True, verbose_name='entered by name'),
),
migrations.AddField(
model_name='mediabulkupload',
name='enteredbytitle',
field=models.CharField(blank=True, db_column='enteredbytitle', max_length=100, null=True, verbose_name='entered by title'),
),
migrations.AddField(
model_name='mediabulkupload',
name='enteredbytribe',
field=models.CharField(blank=True, db_column='enteredbytribe', max_length=100, null=True, verbose_name='entered by tribe'),
),
migrations.AddField(
model_name='mediabulkupload',
name='modifiedbydate',
field=models.DateTimeField(auto_now=True, db_column='modifiedbydate', null=True, verbose_name='modified by date'),
),
migrations.AddField(
model_name='mediabulkupload',
name='modifiedbyname',
field=models.CharField(blank=True, db_column='modifiedbyname', max_length=25, null=True, verbose_name='modified by name'),
),
migrations.AddField(
model_name='mediabulkupload',
name='modifiedbytitle',
field=models.CharField(blank=True, db_column='modifiedbytitle', max_length=100, null=True, verbose_name='modified by title'),
),
migrations.AddField(
model_name='mediabulkupload',
name='modifiedbytribe',
field=models.CharField(blank=True, db_column='modifiedbytribe', max_length=100, null=True, verbose_name='modified by tribe'),
),
migrations.AddField(
model_name='mediabulkupload',
name='needsReview',
field=models.BooleanField(db_column='needsreview', default=True, verbose_name='Needs Review'),
),
migrations.AddField(
model_name='mediabulkupload',
name='researchComments',
field=models.TextField(blank=True, db_column='researchcomments', default=None, null=True, verbose_name='Research Comments'),
),
]
32 changes: 32 additions & 0 deletions TEKDB/TEKDB/migrations/0017_auto_20241016_1552.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.25 on 2024-10-16 22:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('TEKDB', '0016_auto_20241016_1549'),
]

operations = [
migrations.RenameField(
model_name='mediabulkupload',
old_name='date',
new_name='mediabulkdate',
),
migrations.RenameField(
model_name='mediabulkupload',
old_name='description',
new_name='mediabulkdescription',
),
migrations.RenameField(
model_name='mediabulkupload',
old_name='name',
new_name='mediabulkname',
),
migrations.RemoveField(
model_name='mediabulkupload',
name='user',
),
]
Loading

0 comments on commit d9d2898

Please sign in to comment.