From 101b69b1837b3322dd3019e67a391f82d62c7f97 Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Tue, 2 Aug 2022 17:42:23 -0400 Subject: [PATCH 1/8] created UserMediaUploads for all community and action media --- src/database/admin.py | 7 + .../migrations/0118_auto_20220802_1946.py | 38 +++++ src/database/models.py | 7 + src/database/views.py | 136 ++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 src/database/migrations/0118_auto_20220802_1946.py diff --git a/src/database/admin.py b/src/database/admin.py index ee2e6207d..a9cfe0252 100644 --- a/src/database/admin.py +++ b/src/database/admin.py @@ -5,9 +5,16 @@ from _main_.utils.constants import GLOBAL_SITE_SETTINGS from _main_.utils.utils import get_all_models +from database.models import Media +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) + 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..772a3104c 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -1,7 +1,143 @@ +from uuid import UUID 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() + + +def make_UserMediaUpload_for_Communities(): + print("IN 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 UserMediaUpload.objects.filter(media__id = logo).exists(): + media = UserMediaUpload.objects.get(media__id = logo) + + if community not in media.communities: + media.communities.add(community) + media.is_community_image = True + + elif logo is not None: + # '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 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 is not None: + # '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 = banner) + + new_media = UserMediaUpload(user = user, media = media, is_community_image = True) + new_media.save() + + new_media.communities.add(community) + new_media.save() + + + if 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 is not None: + # '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 = 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(): + action_media = Action.objects.values_list('community', 'image', 'user') + print("action media:", list(action_media)) + + for comm_id, m_id, user in action_media: + if m_id is not None: + 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 + else: + media = Media.objects.get(id = m_id) + user = UserProfile.objects.get(id = str(user)) + + new_media = UserMediaUpload(user = user, media = media, is_action_image = True) + new_media.save() + + if community: + new_media.communities.add(community) + new_media.save() + + + + +# MEDIA +# id = models.AutoField(primary_key=True) +# name = models.SlugField(max_length=SHORT_STR_LEN, blank=True) +# file = models.FileField(upload_to="media/") +# media_type = models.CharField(max_length=SHORT_STR_LEN, blank=True) +# is_deleted = models.BooleanField(default=False, blank=True) +# order = models.PositiveIntegerField(default=0, blank=True, null=True) + + + +# USER MEDIA UPLOAD: +# id = models.AutoField(primary_key=True) +# user = models.ForeignKey( +# UserProfile, +# null=True, +# related_name="uploads", +# on_delete=models.DO_NOTHING, +# ) +# communities = models.ManyToManyField( +# Community, +# related_name="community_uploads", +# ) +# media = models.OneToOneField( +# Media, +# null=True, +# related_name="user_upload", +# on_delete=models.CASCADE, +# ) +# is_universal = BooleanField( +# default=False +# ) # True value here means image is available to EVERYONE, and EVERY COMMUNITY +# settings = models.JSONField(null=True, blank=True) +# created_at = models.DateTimeField(auto_now_add=True) +# updated_at = models.DateTimeField(auto_now=True) \ No newline at end of file From 6b34a52bbd711be2a474a28b421b335c35415da5 Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 00:04:21 -0400 Subject: [PATCH 2/8] finished UserMediaUpload for Actions --- src/database/views.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/database/views.py b/src/database/views.py index 772a3104c..8412adac5 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -95,7 +95,15 @@ def make_UserMediaUpload_for_Actions(): media.is_action_image = True else: media = Media.objects.get(id = m_id) - user = UserProfile.objects.get(id = str(user)) + + # 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 + 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_action_image = True) new_media.save() From dce8654d831ab64015776ab5d7787815dab0bcaf Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 00:54:17 -0400 Subject: [PATCH 3/8] finished UserMediaUpload for Events, Testimonials, and Vendors --- src/database/views.py | 129 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 4 deletions(-) diff --git a/src/database/views.py b/src/database/views.py index 8412adac5..0c5a84de2 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -11,12 +11,13 @@ def make_UserMediaUpload_for_every_Media(self, request, qs): # make_UserMediaUpload_for_Communities() - make_UserMediaUpload_for_Actions() + # make_UserMediaUpload_for_Actions() + # make_UserMediaUpload_for_Events() + # make_UserMediaUpload_for_Testimonials() + make_UserMediaUpload_for_Vendors() def make_UserMediaUpload_for_Communities(): - print("IN COMMUNITIES") - community_media = Community.objects.order_by('id').values_list('id', 'logo', 'banner', 'favicon') for comm_id, logo, banner, favicon in community_media: @@ -84,7 +85,7 @@ def make_UserMediaUpload_for_Actions(): print("action media:", list(action_media)) for comm_id, m_id, user in action_media: - if m_id is not None: + if m_id: community = Community.objects.get(id = comm_id) if comm_id else None if UserMediaUpload.objects.filter(media__id = m_id).exists(): @@ -111,7 +112,127 @@ def make_UserMediaUpload_for_Actions(): if community: new_media.communities.add(community) new_media.save() +def make_UserMediaUpload_for_Events(): + event_media = Event.objects.values_list('community', 'image', 'user') + print("event media:", list(event_media)) + for comm_id, m_id, user in event_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_event_image = True + 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 + 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_event_image = True) + new_media.save() + + if community: + new_media.communities.add(community) + new_media.save() + +def make_UserMediaUpload_for_Testimonials(): + testimonial_media = Testimonial.objects.values_list('community', 'image', 'user') + print("testimonial media:", list(testimonial_media)) + + for comm_id, m_id, user in testimonial_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_testimonial_image = True + 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 + 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_testimonial_image = True) + 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 + 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() + elif 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 + 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 + From 69dfc7a11a098310d507a4686a8f433b29d3063e Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 00:59:39 -0400 Subject: [PATCH 4/8] logic fix in make_UserMediaUpload_for_Communities --- src/database/views.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/database/views.py b/src/database/views.py index 0c5a84de2..6a881f1d9 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -10,11 +10,11 @@ def make_UserMediaUpload_for_every_Media(self, request, qs): - # make_UserMediaUpload_for_Communities() + make_UserMediaUpload_for_Communities() # make_UserMediaUpload_for_Actions() # make_UserMediaUpload_for_Events() # make_UserMediaUpload_for_Testimonials() - make_UserMediaUpload_for_Vendors() + # make_UserMediaUpload_for_Vendors() def make_UserMediaUpload_for_Communities(): @@ -23,14 +23,14 @@ def make_UserMediaUpload_for_Communities(): for comm_id, logo, banner, favicon in community_media: community = Community.objects.get(id = comm_id) - if UserMediaUpload.objects.filter(media__id = logo).exists(): + if logo and UserMediaUpload.objects.filter(media__id = logo).exists(): media = UserMediaUpload.objects.get(media__id = logo) if community not in media.communities: media.communities.add(community) media.is_community_image = True - elif logo is not None: + 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') @@ -43,13 +43,13 @@ def make_UserMediaUpload_for_Communities(): new_media.save() - if UserMediaUpload.objects.filter(media__id = banner).exists(): + 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 is not None: + elif banner: # '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') @@ -62,13 +62,13 @@ def make_UserMediaUpload_for_Communities(): new_media.save() - if UserMediaUpload.objects.filter(media__id = favicon).exists(): + 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 is not None: + elif favicon: # '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') @@ -209,7 +209,7 @@ def make_UserMediaUpload_for_Vendors(): if community: new_media.communities.add(community) new_media.save() - elif banner and UserMediaUpload.objects.filter(media__id = banner).exists(): + 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(): From 99c9c0f4cfc36ceb4b4dfc1dc2484ccabe4e611f Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 14:15:40 -0400 Subject: [PATCH 5/8] updated UserMediaUpload admin page filtering --- src/database/admin.py | 8 +++++++- src/database/views.py | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/database/admin.py b/src/database/admin.py index a9cfe0252..2d9e1cef0 100644 --- a/src/database/admin.py +++ b/src/database/admin.py @@ -5,7 +5,7 @@ from _main_.utils.constants import GLOBAL_SITE_SETTINGS from _main_.utils.utils import get_all_models -from database.models import Media +from database.models import Media, UserMediaUpload from database.views import make_UserMediaUpload_for_every_Media #changing the default django site name @@ -15,6 +15,12 @@ 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/views.py b/src/database/views.py index 6a881f1d9..3a838a379 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -1,4 +1,3 @@ -from uuid import UUID from django.shortcuts import render from database.models import * From 5a7e05f12c2fbe11af79efe825a7b55b1b422627 Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 14:32:17 -0400 Subject: [PATCH 6/8] consolidated UserMediaUpload fr Actions, Events, and Testimonials into one function --- src/database/views.py | 98 +++++++++---------------------------------- 1 file changed, 19 insertions(+), 79 deletions(-) diff --git a/src/database/views.py b/src/database/views.py index 3a838a379..5c73e0311 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -9,10 +9,10 @@ def make_UserMediaUpload_for_every_Media(self, request, qs): - make_UserMediaUpload_for_Communities() - # make_UserMediaUpload_for_Actions() - # make_UserMediaUpload_for_Events() - # make_UserMediaUpload_for_Testimonials() + # 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() @@ -50,7 +50,7 @@ def make_UserMediaUpload_for_Communities(): 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 interim owner for now + # 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) @@ -69,7 +69,7 @@ def make_UserMediaUpload_for_Communities(): 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 interim owner for now + # 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) @@ -79,11 +79,9 @@ def make_UserMediaUpload_for_Communities(): new_media.communities.add(community) new_media.save() -def make_UserMediaUpload_for_Actions(): - action_media = Action.objects.values_list('community', 'image', 'user') - print("action media:", list(action_media)) - - for comm_id, m_id, user in action_media: +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 @@ -92,44 +90,15 @@ def make_UserMediaUpload_for_Actions(): if community and community not in media.communities.all(): media.communities.add(community) - media.is_action_image = True - 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 - 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_action_image = True) - new_media.save() - - if community: - new_media.communities.add(community) - new_media.save() -def make_UserMediaUpload_for_Events(): - event_media = Event.objects.values_list('community', 'image', 'user') - print("event media:", list(event_media)) - - for comm_id, m_id, user in event_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_event_image = True + 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 + # 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: @@ -137,40 +106,11 @@ def make_UserMediaUpload_for_Events(): else: user = UserProfile.objects.get(email = 'brad@massenergize.org') - new_media = UserMediaUpload(user = user, media = media, is_event_image = True) - new_media.save() - - if community: - new_media.communities.add(community) - new_media.save() - -def make_UserMediaUpload_for_Testimonials(): - testimonial_media = Testimonial.objects.values_list('community', 'image', 'user') - print("testimonial media:", list(testimonial_media)) - - for comm_id, m_id, user in testimonial_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_testimonial_image = True - 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 - 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 = UserMediaUpload(user = user, media = media, is_testimonial_image = True) new_media.save() if community: @@ -194,7 +134,7 @@ def make_UserMediaUpload_for_Vendors(): 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 + # 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: @@ -218,7 +158,7 @@ def make_UserMediaUpload_for_Vendors(): 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 + # 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: From 705c1ecaad3eaf89b8097946c81281daf65a77ce Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Wed, 3 Aug 2022 16:44:00 -0400 Subject: [PATCH 7/8] views.py cleanup --- src/database/views.py | 44 +++---------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/src/database/views.py b/src/database/views.py index 5c73e0311..cb269657f 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -9,11 +9,11 @@ def make_UserMediaUpload_for_every_Media(self, request, qs): - # make_UserMediaUpload_for_Communities() + 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() + make_UserMediaUpload_for_Vendors() def make_UserMediaUpload_for_Communities(): @@ -119,7 +119,7 @@ def make_UserMediaUpload_for_Actions_Events_Testimonials(all_media, type): def make_UserMediaUpload_for_Vendors(): vendor_media = Vendor.objects.values_list('communities', 'logo', 'banner', 'user') - print("vendor media:", list(vendor_media)) + # 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 @@ -171,41 +171,3 @@ def make_UserMediaUpload_for_Vendors(): if community: new_media.communities.ad - - - - -# MEDIA -# id = models.AutoField(primary_key=True) -# name = models.SlugField(max_length=SHORT_STR_LEN, blank=True) -# file = models.FileField(upload_to="media/") -# media_type = models.CharField(max_length=SHORT_STR_LEN, blank=True) -# is_deleted = models.BooleanField(default=False, blank=True) -# order = models.PositiveIntegerField(default=0, blank=True, null=True) - - - -# USER MEDIA UPLOAD: -# id = models.AutoField(primary_key=True) -# user = models.ForeignKey( -# UserProfile, -# null=True, -# related_name="uploads", -# on_delete=models.DO_NOTHING, -# ) -# communities = models.ManyToManyField( -# Community, -# related_name="community_uploads", -# ) -# media = models.OneToOneField( -# Media, -# null=True, -# related_name="user_upload", -# on_delete=models.CASCADE, -# ) -# is_universal = BooleanField( -# default=False -# ) # True value here means image is available to EVERYONE, and EVERY COMMUNITY -# settings = models.JSONField(null=True, blank=True) -# created_at = models.DateTimeField(auto_now_add=True) -# updated_at = models.DateTimeField(auto_now=True) \ No newline at end of file From cd4c91153242f3e5eb0dc7285edc3cb6d92ae005 Mon Sep 17 00:00:00 2001 From: Mattia Danese Date: Fri, 26 Aug 2022 12:08:29 -0400 Subject: [PATCH 8/8] little logic fix --- src/database/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/database/views.py b/src/database/views.py index cb269657f..f761953ce 100644 --- a/src/database/views.py +++ b/src/database/views.py @@ -24,8 +24,9 @@ def make_UserMediaUpload_for_Communities(): 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: + if community not in media_communities: media.communities.add(community) media.is_community_image = True