I have a site on the server where I work with signals.py The problem is that when I add a db file through the admin panel signals.py does not work EXACTLY ON THE SERVER on my computer everything works fine But there is another model involving signals.py and it works great
models.py
The model that works
class History(models.Model):
tg_id = models.BigIntegerField(verbose_name='Telegram ID')
amount = models.FloatField(verbose_name='Кол-во')
date = models.DateTimeField(verbose_name='Дата транзакции')
Problem model
class database_sqlite3(models.Model):
file = models.FileField(verbose_name='Файл', upload_to='db_file/',
help_text='Вы можете добавить сюда базу пользователей')
app.py
from django.apps import AppConfig
class MainConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'main'
verbose_name = "Бот"
def ready(self):
import main.signals
signals.py
The model that works
@receiver(post_save, sender=History)
def create_profile(sender, instance, created, **kwargs):
if created:
obj = User.objects.get(tg_id=int(instance.tg_id))
wallet = obj.wallet
if wallet == None:
wallet = 0
else:
pass
User.objects.filter(tg_id=instance.tg_id).update(wallet=wallet + instance.amount)0
Problem model
@receiver(post_save, sender=database_sqlite3)
def create_profile(sender, instance, created, **kwargs):
if created:
env = Env()
env.read_env()
gre = psycopg2.connect(
database=env.str('POSTGRES_DB'),
user=env.str('POSTGRES_USER'),
password=env.str('POSTGRES_PASSWORD'),
host=env.str('POSTGRES_HOST'),
port=env.str('POSTGRES_PORT')
)
cur_gre = gre.cursor()
conn = sqlite3.connect(str(instance.file))
cursor = conn.cursor()
table_names = cursor.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
table_name = []
for i in table_names:
table_name.append(i[0])
if 'main_admin_bot' in table_name:
cursore = conn.execute(f"SELECT * FROM main_admin_bot").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_admin_bot ("tg_id","name") VALUES (%s,%s)'''
date = (i[1], i[2])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_attempts' in table_name:
cursore = conn.execute(f"SELECT * FROM main_attempts").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_attempts ("ids","count") VALUES (%s,%s)'''
date = (i[1], i[2])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_blacklist' in table_name:
cursore = conn.execute(f"SELECT * FROM main_blacklist").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_blacklist ("words","job") VALUES (%s,%s)'''
bools = None
if i[2] == 0:
bools = False
else:
bools = True
date = (i[1], bools)
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_channel' in table_name:
cursore = conn.execute(f"SELECT * FROM main_channel").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_channel ("channel_id","ids") VALUES (%s,%s)'''
date = (i[1], i[2])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_parse_user' in table_name:
cursore = conn.execute(f"SELECT * FROM main_parse_user").fetchall()
for i in cursore:
sql = f'''INSERT INTO main_parse_user ("user_id","group_id", "username","bio", "first_name") VALUES (%s,%s,%s,%s,%s) ON CONFLICT (user_id) DO UPDATE SET (group_id,username,bio,first_name) = (EXCLUDED.group_id,EXCLUDED.username,coalesce(main_parse_user.bio, EXCLUDED.bio),EXCLUDED.first_name)'''
date = (i[1], i[2], i[3], i[4], i[5])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
if 'main_token' in table_name:
cursore = conn.execute(f"SELECT * FROM main_token").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_token ("ids","bot_token") VALUES (%s,%s)'''
date = (i[1], i[2])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_trycountuser' in table_name:
cursore = conn.execute(f"SELECT * FROM main_trycountuser").fetchall()
for i in cursore:
print(i)
try:
sql = f'''INSERT INTO main_trycountuser ("ids","count") VALUES (%s,%s)'''
date = (int(i[1]), int(i[2]))
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_user' in table_name:
cursore = conn.execute(f"SELECT * FROM main_user").fetchall()
for i in cursore:
try:
sql = f'''INSERT INTO main_user ("tg_id" ,"username", "name" , "try_count", "activate","linkedin","language","wallet") VALUES (%s,%s,%s,%s,%s,%s,%s,%s)'''
bools = None
if i[5] == 0:
bools = False
else:
bools = True
date = (i[1], i[2], i[3], i[4], bools, i[6], i[7], i[8])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_users_db' in table_name:
cursore = conn.execute("SELECT * FROM main_users_db").fetchall()
for i in cursore:
sqlz = f'''INSERT INTO main_parse_user ("user_id","group_id", "username","bio", "first_name") VALUES (%s,%s,%s,%s,%s) ON CONFLICT (user_id) DO UPDATE SET (group_id,username,bio,first_name) = (EXCLUDED.group_id,EXCLUDED.username,coalesce(main_parse_user.bio, EXCLUDED.bio),EXCLUDED.first_name)'''
date = (i[0], i[4], i[1], str(i[2]), i[5])
try:
try:
cur_gre.execute(sqlz, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
if 'user_parse' in table_name:
cursore = conn.execute("SELECT * FROM user_parse").fetchall()
for i in cursore:
sqlz = f'''INSERT INTO main_parse_user ("user_id","group_id", "username","bio", "first_name") VALUES (%s,%s,%s,%s,%s) ON CONFLICT (user_id) DO UPDATE SET (group_id,username,bio,first_name) = (EXCLUDED.group_id,EXCLUDED.username,coalesce(main_parse_user.bio, EXCLUDED.bio),EXCLUDED.first_name)'''
date = (i[1], i[0], i[2], str(i[3]), i[4])
try:
try:
cur_gre.execute(sqlz, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
if 'main_parser' in table_name:
cursor = conn.execute("SELECT * FROM main_parser").fetchall()
for i in cursor:
sql = f'''INSERT INTO main_parse_user ("user_id","group_id", "username","bio", "first_name") VALUES (%s,%s,%s,%s,%s) ON CONFLICT (user_id) DO UPDATE SET (group_id,username,bio,first_name) = (EXCLUDED.group_id,EXCLUDED.username,coalesce(main_parse_user.bio, EXCLUDED.bio),EXCLUDED.first_name)'''
date = (i[1], i[2], i[3], i[4], i[5])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
if 'bot_user' in table_name:
cursor = conn.execute("SELECT * FROM bot_user").fetchall()
for i in cursor:
try:
sql = f'''INSERT INTO main_user ("tg_id", "username", "name", "try_count", "activate") VALUES (%s,%s,%s,%s,%s)'''
bools = None
if i[4] == 0:
bools = False
else:
bools = True
date = (i[0], i[1], i[2], i[3], bools)
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_trypricetype' in table_name:
cursor = conn.execute("SELECT * FROM main_trypricetype").fetchall()
for i in cursor:
try:
sql = f'''INSERT INTO "main_trypricetype" ("searchtype", "price") VALUES (%s,%s)'''
date = (i[1], i[2])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_database_sqlite3' in table_name:
cursor = conn.execute("SELECT * FROM main_database_sqlite3").fetchall()
for i in cursor:
try:
sql = f'''INSERT INTO "main_database_sqlite3" ("file") VALUES (%s)'''
date = (i[1],)
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_history' in table_name:
cursor = conn.execute("SELECT * FROM main_history").fetchall()
for i in cursor:
try:
sql = f'''INSERT INTO "main_history" ("tg_id","amount","date") VALUES (%s,%s,%s)'''
date = (i[1], i[2], i[3])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
if 'main_webapp' in table_name:
cursor = conn.execute("SELECT * FROM main_webapp").fetchall()
for i in cursor:
try:
sql = f'''INSERT INTO "main_webapp" ("id","image","url","name","text") VALUES (%s,%s,%s,%s,%s)'''
date = (i[0], i[1], i[2], i[3], i[4])
try:
try:
cur_gre.execute(sql, date)
except psycopg2.errors.InvalidTextRepresentation:
pass
except psycopg2.errors.InFailedSqlTransaction:
pass
gre.commit()
except psycopg2.errors.UniqueViolation:
pass
conn.close()
os.remove(str(instance.file))
I added to the app/init.py file but nothing worked
source https://stackoverflow.com/questions/74855140/signals-django-not-working-on-ubuntu-server
Comments
Post a Comment