diff --git a/alter-scripts/alter-1.39.py b/alter-scripts/alter-1.39.py index 4d087944..510d91f7 100644 --- a/alter-scripts/alter-1.39.py +++ b/alter-scripts/alter-1.39.py @@ -5,20 +5,19 @@ from django.core.files.storage import FileSystemStorage import django import sys +from connection import execute 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() @@ -29,10 +28,8 @@ def save_image(user_id, url): if __name__ == "__main__": - users = User.objects.filter(profile_pic__isnull=False) - for user in users: - save_image(user.id, user.profile_pic) + data = execute("SELECT id, profile_pic FROM user WHERE profile_pic IS NOT NULL;") + for user_id, profile_pic in data: + save_image(user_id, profile_pic) - settings = SystemSetting.objects.filter(key="db.version").first() - settings.value = "1.39" - settings.save() + execute("UPDATE system_setting SET value = '1.39', updated_at = now() WHERE `key` = 'db.version';") diff --git a/alter-scripts/connection.py b/alter-scripts/connection.py new file mode 100644 index 00000000..f90c51b5 --- /dev/null +++ b/alter-scripts/connection.py @@ -0,0 +1,16 @@ +import pymysql +from decouple import config + +db_config = { + 'host': config('DATABASE_HOST'), + 'user': config('DATABASE_USER'), + 'password': config('DATABASE_PASSWORD'), + 'db': config('DATABASE_NAME'), +} + + +def execute(query): + with pymysql.connect(**db_config) as connection: + with connection.cursor() as cursor: + cursor.execute(query) + return cursor.fetchall() diff --git a/requirements.txt b/requirements.txt index d769c13b..f0f7a1a2 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