From 2ac458adcf9400d9b5e9222d1e1a7a719b60bba2 Mon Sep 17 00:00:00 2001 From: Aashish <8xdeadshot@gmail.com> Date: Sat, 2 Dec 2023 21:28:28 +0530 Subject: [PATCH 1/2] feat(profile):Added profile pic url to models --- alter-scripts/{alter-1.0.0.py => alter-1.39.py} | 4 +++- api/dashboard/user/dash_user_serializer.py | 9 --------- requirements.txt | 3 ++- 3 files changed, 5 insertions(+), 11 deletions(-) rename alter-scripts/{alter-1.0.0.py => alter-1.39.py} (96%) diff --git a/alter-scripts/alter-1.0.0.py b/alter-scripts/alter-1.39.py similarity index 96% rename from alter-scripts/alter-1.0.0.py rename to alter-scripts/alter-1.39.py index 90cf3293..93a85ef0 100644 --- a/alter-scripts/alter-1.0.0.py +++ b/alter-scripts/alter-1.39.py @@ -8,7 +8,9 @@ import django import sys -sys.path.insert(0, 'path_to_mulearnbackend') + +os.chdir('..') +sys.path.append(os.getcwd()) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mulearnbackend.settings') django.setup() diff --git a/api/dashboard/user/dash_user_serializer.py b/api/dashboard/user/dash_user_serializer.py index 3f3d70b5..880152f2 100644 --- a/api/dashboard/user/dash_user_serializer.py +++ b/api/dashboard/user/dash_user_serializer.py @@ -56,15 +56,6 @@ class Meta: "profile_pic", ] - def get_profile_pic(self, obj): - fs = FileSystemStorage() - path = f'user/profile/{obj.id}.png' - if fs.exists(path): - profile_pic = f"{BE_DOMAIN_NAME}{fs.url(path)}" - else: - profile_pic = obj.profile_pic - return profile_pic - def get_roles(self, obj): return [ user_role_link.role.title diff --git a/requirements.txt b/requirements.txt index d769c13b..c4e1c106 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,5 @@ requests==2.31.0 service-identity==23.1.0 tzdata==2023.3 zope.interface==6.1 -qrcode==7.4.2 \ No newline at end of file +qrcode==7.4.2 +pymysql==1.0.2 \ No newline at end of file From 90ca6b2055e3d139291c268544e0570f0fd07a4d Mon Sep 17 00:00:00 2001 From: Aashish <8xdeadshot@gmail.com> Date: Sat, 2 Dec 2023 21:43:04 +0530 Subject: [PATCH 2/2] feat(profile):Added profile pic url to models --- alter-scripts/alter-1.39.py | 27 ++++++--------------- api/dashboard/profile/profile_serializer.py | 9 +++++++ api/dashboard/user/dash_user_serializer.py | 9 +++++++ db/settings.py | 12 +++++++++ db/user.py | 13 +++++----- requirements.txt | 3 +-- 6 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 db/settings.py diff --git a/alter-scripts/alter-1.39.py b/alter-scripts/alter-1.39.py index 93a85ef0..4d087944 100644 --- a/alter-scripts/alter-1.39.py +++ b/alter-scripts/alter-1.39.py @@ -1,31 +1,24 @@ import os -import pymysql import requests from io import BytesIO from os.path import splitext from django.core.files.storage import FileSystemStorage -from decouple import config import django import sys - os.chdir('..') sys.path.append(os.getcwd()) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mulearnbackend.settings') django.setup() - -def fetch_data(connection, query): - with connection.cursor() as cursor: - cursor.execute(query) - return cursor.fetchall() +from db.user import User +from db.settings import SystemSetting def save_image(user_id, url): try: response = requests.get(url) response.raise_for_status() - extension = splitext(url)[-1].split("?")[0] pic = BytesIO(response.content) fs = FileSystemStorage() @@ -36,14 +29,10 @@ def save_image(user_id, url): if __name__ == "__main__": - db_config = { - 'host': config('DATABASE_HOST'), - 'user': config('DATABASE_USER'), - 'password': config('DATABASE_PASSWORD'), - 'db': config('DATABASE_NAME'), - } + users = User.objects.filter(profile_pic__isnull=False) + for user in users: + save_image(user.id, user.profile_pic) - with pymysql.connect(**db_config) as connection: - data = fetch_data(connection, "SELECT id, profile_pic FROM user WHERE profile_pic IS NOT NULL;") - for user_id, profile_pic in data: - img = save_image(user_id, profile_pic) + settings = SystemSetting.objects.filter(key="db.version").first() + settings.value = "1.39" + settings.save() diff --git a/api/dashboard/profile/profile_serializer.py b/api/dashboard/profile/profile_serializer.py index 5b2f0352..1eb7cb92 100644 --- a/api/dashboard/profile/profile_serializer.py +++ b/api/dashboard/profile/profile_serializer.py @@ -90,6 +90,15 @@ def get_percentile(self, obj): except Exception as e: return 0 + def get_profile_pic(self, obj): + fs = FileSystemStorage() + path = f'user/profile/{obj.id}.png' + if fs.exists(path): + profile_pic = f"{BE_DOMAIN_NAME}{fs.url(path)}" + else: + profile_pic = obj.profile_pic + return profile_pic + def get_roles(self, obj): return list({link.role.title for link in obj.user_role_link_user.filter(verified=True)}) diff --git a/api/dashboard/user/dash_user_serializer.py b/api/dashboard/user/dash_user_serializer.py index 880152f2..3f3d70b5 100644 --- a/api/dashboard/user/dash_user_serializer.py +++ b/api/dashboard/user/dash_user_serializer.py @@ -56,6 +56,15 @@ class Meta: "profile_pic", ] + def get_profile_pic(self, obj): + fs = FileSystemStorage() + path = f'user/profile/{obj.id}.png' + if fs.exists(path): + profile_pic = f"{BE_DOMAIN_NAME}{fs.url(path)}" + else: + profile_pic = obj.profile_pic + return profile_pic + def get_roles(self, obj): return [ user_role_link.role.title diff --git a/db/settings.py b/db/settings.py new file mode 100644 index 00000000..ef660aad --- /dev/null +++ b/db/settings.py @@ -0,0 +1,12 @@ +from django.db import models + + +class SystemSetting(models.Model): + key = models.CharField(primary_key=True, max_length=100) + value = models.CharField(max_length=100) + updated_at = models.DateTimeField() + created_at = models.DateTimeField() + + class Meta: + managed = False + db_table = 'system_setting' diff --git a/db/user.py b/db/user.py index e5fd90ee..8511b070 100644 --- a/db/user.py +++ b/db/user.py @@ -16,6 +16,7 @@ class User(models.Model): email = models.EmailField(unique=True, max_length=200) password = models.CharField(max_length=200, blank=True, null=True) mobile = models.CharField(unique=True, max_length=15) + profile_pic = models.CharField(max_length=200, blank=True, null=True) gender = models.CharField(max_length=10, blank=True, null=True, choices=[("Male", "Male"),("Female", "Female")]) dob = models.DateField(blank=True, null=True) admin = models.BooleanField(default=False) @@ -40,12 +41,12 @@ def fullname(self): return f"{self.first_name} {self.last_name}" - @property - def profile_pic(self): - fs = FileSystemStorage() - path = f'user/profile/{self.id}.png' - if fs.exists(path): - return f"{decouple_config('BE_DOMAIN_NAME')}{fs.url(path)}" + # @property + # def profile_pic(self): + # fs = FileSystemStorage() + # path = f'user/profile/{self.id}.png' + # if fs.exists(path): + # return f"{decouple_config('BE_DOMAIN_NAME')}{fs.url(path)}" class UserReferralLink(models.Model): diff --git a/requirements.txt b/requirements.txt index c4e1c106..d769c13b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,5 +17,4 @@ requests==2.31.0 service-identity==23.1.0 tzdata==2023.3 zope.interface==6.1 -qrcode==7.4.2 -pymysql==1.0.2 \ No newline at end of file +qrcode==7.4.2 \ No newline at end of file