Skip to content

Commit

Permalink
Merge pull request #1702 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
feat(profile):Added profile pic url to models
  • Loading branch information
Aashish Vinu authored Dec 2, 2023
2 parents 54b9ce2 + e36285f commit 452cb10
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
47 changes: 0 additions & 47 deletions alter-scripts/alter-1.0.0.py

This file was deleted.

38 changes: 38 additions & 0 deletions alter-scripts/alter-1.39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import requests
from io import BytesIO
from os.path import splitext
from django.core.files.storage import FileSystemStorage
import django
import sys

os.chdir('..')
sys.path.append(os.getcwd())
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mulearnbackend.settings')
django.setup()

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()
filename = f"user/profile/{user_id}{extension}"
return fs.save(filename, pic)
except Exception as e:
print(e)


if __name__ == "__main__":
users = User.objects.filter(profile_pic__isnull=False)
for user in users:
save_image(user.id, user.profile_pic)

settings = SystemSetting.objects.filter(key="db.version").first()
settings.value = "1.39"
settings.save()
9 changes: 9 additions & 0 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})

Expand Down
12 changes: 12 additions & 0 deletions db/settings.py
Original file line number Diff line number Diff line change
@@ -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'
13 changes: 7 additions & 6 deletions db/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 452cb10

Please sign in to comment.