diff --git a/src/database/admin.py b/src/database/admin.py index ee2e6207d..2d9e1cef0 100644 --- a/src/database/admin.py +++ b/src/database/admin.py @@ -5,9 +5,22 @@ from _main_.utils.constants import GLOBAL_SITE_SETTINGS from _main_.utils.utils import get_all_models +from database.models import Media, UserMediaUpload +from database.views import make_UserMediaUpload_for_every_Media + #changing the default django site name admin.site.site_header = GLOBAL_SITE_SETTINGS["ADMIN_SITE_HEADER"] +class MediaAdmin(admin.ModelAdmin): + actions = [make_UserMediaUpload_for_every_Media] +admin.site.register(Media, MediaAdmin) + +class UserMediaUploadAdmin(admin.ModelAdmin): + # can filter with email or full name of attached user using search box + search_fields = ['user__email', 'user__full_name'] + list_filter = ['communities', 'is_community_image', 'is_action_image', 'is_event_image', 'is_testimonial_image', 'is_vendor_image'] +admin.site.register(UserMediaUpload, UserMediaUploadAdmin) + def register_all_models(): """ diff --git a/src/database/migrations/0118_auto_20220802_1946.py b/src/database/migrations/0118_auto_20220802_1946.py new file mode 100644 index 000000000..33f1444cb --- /dev/null +++ b/src/database/migrations/0118_auto_20220802_1946.py @@ -0,0 +1,38 @@ +# Generated by Django 3.1.14 on 2022-08-02 19:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('database', '0117_message_parent'), + ] + + operations = [ + migrations.AddField( + model_name='usermediaupload', + name='is_action_image', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='usermediaupload', + name='is_community_image', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='usermediaupload', + name='is_event_image', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='usermediaupload', + name='is_testimonial_image', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='usermediaupload', + name='is_vendor_image', + field=models.BooleanField(default=False), + ), + ] diff --git a/src/database/models.py b/src/database/models.py index 9fec5b683..5ca982adb 100644 --- a/src/database/models.py +++ b/src/database/models.py @@ -799,6 +799,13 @@ class UserMediaUpload(models.Model): is_universal = BooleanField( default=False ) # True value here means image is available to EVERYONE, and EVERY COMMUNITY + + is_community_image = BooleanField(default=False) + is_action_image = BooleanField(default=False) + is_event_image = BooleanField(default=False) + is_testimonial_image = BooleanField(default=False) + is_vendor_image = BooleanField(default=False) + settings = models.JSONField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/src/database/views.py b/src/database/views.py index 4e4ab95cb..f761953ce 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -1,7 +1,174 @@ from django.shortcuts import render +from database.models import * # Create your views here. #class UserAvatarUpload(APIView): # permission_classes = [permissions.IsAuthenticated] # parser_classes - [MultiPartParser, FormParser] + + +def make_UserMediaUpload_for_every_Media(self, request, qs): + make_UserMediaUpload_for_Communities() + make_UserMediaUpload_for_Actions_Events_Testimonials(Action.objects.values_list('community', 'image', 'user'), "action") + make_UserMediaUpload_for_Actions_Events_Testimonials(Event.objects.values_list('community', 'image', 'user'), "event") + make_UserMediaUpload_for_Actions_Events_Testimonials(Testimonial.objects.values_list('community', 'image', 'user'), "testimonial") + make_UserMediaUpload_for_Vendors() + + +def make_UserMediaUpload_for_Communities(): + community_media = Community.objects.order_by('id').values_list('id', 'logo', 'banner', 'favicon') + + for comm_id, logo, banner, favicon in community_media: + community = Community.objects.get(id = comm_id) + + if logo and UserMediaUpload.objects.filter(media__id = logo).exists(): + media = UserMediaUpload.objects.get(media__id = logo) + media_communities = media.communities.all() + + if community not in media_communities: + media.communities.add(community) + media.is_community_image = True + + elif logo: + # 'user' field must be UserProfile but Community.owner_email may not be connected to a UserProfile + # using brad as interim owner for now + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + media = Media.objects.get(id = logo) + + new_media = UserMediaUpload(user = user, media = media, is_community_image = True) + new_media.save() + + new_media.communities.add(community) + new_media.save() + + + if banner and UserMediaUpload.objects.filter(media__id = banner).exists(): + media = UserMediaUpload.objects.get(media__id = banner) + + if community not in media.communities: + media.communities.add(community) + media.is_community_image = True + elif banner: + # 'user' field must be UserProfile but Community.owner_email may not be connected to a UserProfile + # using brad as catch all case + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + media = Media.objects.get(id = banner) + + new_media = UserMediaUpload(user = user, media = media, is_community_image = True) + new_media.save() + + new_media.communities.add(community) + new_media.save() + + + if favicon and UserMediaUpload.objects.filter(media__id = favicon).exists(): + media = UserMediaUpload.objects.get(media__id = favicon) + + if community not in media.communities: + media.communities.add(community) + media.is_community_image = True + elif favicon: + # 'user' field must be UserProfile but Community.owner_email may not be connected to a UserProfile + # using brad as catch all case + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + media = Media.objects.get(id = favicon) + + new_media = UserMediaUpload(user = user, media = media, is_community_image = True) + new_media.save() + + new_media.communities.add(community) + new_media.save() + +def make_UserMediaUpload_for_Actions_Events_Testimonials(all_media, type): + # print("{} media: {}".format(type, list(all_media))) + for comm_id, m_id, user in all_media: + if m_id: + community = Community.objects.get(id = comm_id) if comm_id else None + + if UserMediaUpload.objects.filter(media__id = m_id).exists(): + media = UserMediaUpload.objects.get(media__id = m_id) + + if community and community not in media.communities.all(): + media.communities.add(community) + + media.is_action_image = True if type == "action" else media.is_action_image + media.is_event_image = True if type == "event" else media.is_event_image + media.is_testimonial_image = True if type == "testimonial" else media.is_testimonial_image + else: + media = Media.objects.get(id = m_id) + + # not all actions (barely any) have a user, so community owner becomes user + # not all actions (barely any) have a community, so brad becomes user as catch all case + if user: + user = UserProfile.objects.get(id = str(user)) + elif community: + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + else: + user = UserProfile.objects.get(email = 'brad@massenergize.org') + + new_media = UserMediaUpload(user = user, media = media) + new_media.is_action_image = True if type == "action" else False + new_media.is_event_image = True if type == "event" else False + new_media.is_testimonial_image = True if type == "testimonial" else False + + new_media.save() + + if community: + new_media.communities.add(community) + new_media.save() + +def make_UserMediaUpload_for_Vendors(): + vendor_media = Vendor.objects.values_list('communities', 'logo', 'banner', 'user') + # print("vendor media:", list(vendor_media)) + + for comm_id, logo, banner, user in vendor_media: + community = Community.objects.get(id = comm_id) if comm_id else None + + if logo and UserMediaUpload.objects.filter(media__id = logo).exists(): + media = UserMediaUpload.objects.get(media__id = logo) + + if community and community not in media.communities.all(): + media.communities.add(community) + media.is_vendor_image = True + elif logo: + media = Media.objects.get(id = logo) + + # not all actions (barely any) have a user, so community owner becomes user + # not all actions (barely any) have a community, so brad becomes user as catch all case + if user: + user = UserProfile.objects.get(id = str(user)) + elif community: + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + else: + user = UserProfile.objects.get(email = 'brad@massenergize.org') + + new_media = UserMediaUpload(user = user, media = media, is_vendor_image = True) + new_media.save() + + if community: + new_media.communities.add(community) + new_media.save() + if banner and UserMediaUpload.objects.filter(media__id = banner).exists(): + media = UserMediaUpload.objects.get(media__id = banner) + + if community and community not in media.communities.all(): + media.communities.add(community) + media.is_vendor_image = True + elif banner: + media = Media.objects.get(id = banner) + + # not all actions (barely any) have a user, so community owner becomes user + # not all actions (barely any) have a community, so brad becomes user as catch all case + if user: + user = UserProfile.objects.get(id = str(user)) + elif community: + user = UserProfile.objects.get(email = community.owner_email) if UserProfile.objects.filter(email = community.owner_email).exists() else UserProfile.objects.get(email = 'brad@massenergize.org') + else: + user = UserProfile.objects.get(email = 'brad@massenergize.org') + + new_media = UserMediaUpload(user = user, media = media, is_vendor_image = True) + new_media.save() + + if community: + new_media.communities.ad