Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UserMediaUpload for every existing Media #576

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions src/database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down
38 changes: 38 additions & 0 deletions src/database/migrations/0118_auto_20220802_1946.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
7 changes: 7 additions & 0 deletions src/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
167 changes: 167 additions & 0 deletions src/database/views.py
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]')
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 = '[email protected]')
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 = '[email protected]')
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 = '[email protected]')
else:
user = UserProfile.objects.get(email = '[email protected]')

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 = '[email protected]')
else:
user = UserProfile.objects.get(email = '[email protected]')

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 = '[email protected]')
else:
user = UserProfile.objects.get(email = '[email protected]')

new_media = UserMediaUpload(user = user, media = media, is_vendor_image = True)
new_media.save()

if community:
new_media.communities.ad