Advertisement
YaKotikTvoy

User

Aug 21st, 2023 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.27 KB | Software | 0 0
  1. import enums
  2. import asyncio
  3.  
  4. import helpers
  5. import keyboards
  6. from scenary import main as scenary_main
  7. import re
  8. from dal import db, models
  9. from dal.User_info import User_info
  10. from dal.Scenary import Scenary
  11. from dal.RefererPayment import RefererPayment
  12. from dal.Prognoz import Prognoz
  13. from datetime import datetime, timedelta
  14. import requests
  15. import json
  16. from dal.ChatHistory import ChatHistory
  17. from natal import routes as routes_natal
  18. from chat import routes as routes_chat
  19.  
  20.  
  21. class User:
  22.  
  23.     def __init__(self, user_id):
  24.         self.new_refer_coins = 2
  25.         self.status = enums.user_status.new_user
  26.         self._model = models.Users()
  27.         self.active_scenary = None
  28.         self.user_info = None
  29.  
  30.         info = asyncio.run(db.get_user(user_id))
  31.         if info is not None:
  32.             self._model = info._data[0]
  33.             self.status = enums.user_status.user
  34.  
  35.             self.user_info = User_info(self._model)  # убрать после рефакторинга language_code и user_name
  36.             if self._model.scenary_id != 0:
  37.                 self.active_scenary = Scenary(self._model)
  38.  
  39.         if self.subscription_date != None:
  40.             if datetime.now() > self.subscription_date:
  41.                 asyncio.run(self.get_subscription_transactions())
  42.  
  43.     @property
  44.     def id(self):
  45.         return self._model.id
  46.  
  47.     @property
  48.     def scenary_id(self):
  49.         return self._model.scenary_id
  50.  
  51.     @property
  52.     def user_tg_id(self):
  53.         return self._model.user_tg_id
  54.  
  55.     @property
  56.     def chat_id(self):
  57.         return self._model.chat_id
  58.  
  59.     @property
  60.     def balance(self):
  61.         return str(self._model.balance)
  62.  
  63.     @property
  64.     def referer_id(self):
  65.         return self._model.referer_id
  66.  
  67.     @property
  68.     def add_data(self):
  69.         return self.active_scenary.data
  70.  
  71.     @property
  72.     def is_solvent(self):
  73.         return self._model.is_solvent
  74.  
  75.     @property
  76.     def is_banned(self):
  77.         return self._model.is_banned
  78.  
  79.     @property
  80.     def last_step_handler(self):
  81.         return self._model.last_step_handler
  82.  
  83.     @property
  84.     def subscription_date(self):
  85.         return self._model.subscription_date
  86.  
  87.     @property
  88.     def last_update(self):
  89.         return self._model.last_update
  90.  
  91.     @property
  92.     def partner_referer_link(self):
  93.         return self._model.partner_referer_link
  94.  
  95.     @property
  96.     def is_confirm_user(self):
  97.         return self.status == enums.user_status.user
  98.  
  99.     @property
  100.     def last_anoncement_id(self):
  101.         return self._model.last_anoncement_id
  102.  
  103.  
  104.     @property
  105.     def is_subscribe(self):
  106.         return self.subscription_date != None and self.subscription_date > datetime.now()
  107.  
  108.     @property
  109.     def has_natal_calc_data(self):
  110.         return self.user_info is not None \
  111.                and self.user_info.name is not None \
  112.                and self.user_info.birthday is not None \
  113.                and self.user_info.gender is not None \
  114.                and self.user_info.town is not None \
  115.                and self.user_info.lat is not None \
  116.                and self.user_info.lng is not None \
  117.                and self.user_info.tz_info is not None
  118.  
  119.     @property
  120.     def has_get_self_data(self):
  121.         return self.has_natal_calc_data is True and \
  122.                self.user_info.horoscope is not None \
  123.                and self.user_info.aspects is not None
  124.  
  125.     @property
  126.     def has_prognoz_data(self):
  127.         return self.has_get_self_data is True \
  128.                and self.add_data is not None \
  129.                and self.add_data.transits is not None \
  130.                and self.add_data.date is not None \
  131.                and self.add_data.transits_aspects is not None
  132.  
  133.     @property
  134.     def has_people_data(self):
  135.         return self.add_data is not None \
  136.                and self.add_data.name is not None \
  137.                and self.add_data.birthday is not None \
  138.                and self.add_data.gender is not None \
  139.                and self.add_data.horoscope is not None \
  140.                and self.add_data.aspects is not None
  141.  
  142.     @property
  143.     def has_compatibility_data(self):
  144.         return self.has_get_self_data is True \
  145.                and self.add_data is not None \
  146.                and self.add_data.couple_natal is not None \
  147.                and self.add_data.couple_aspects is not None \
  148.                and self.add_data.name is not None
  149.  
  150.  
  151.     @last_anoncement_id.setter
  152.     def last_anoncement_id(self,value):
  153.         self._model.last_anoncement_id=value
  154.         asyncio.run(db.update_user(self._model))
  155.  
  156.  
  157.     async def add_balance(self, count):
  158.         self._model.balance = self._model.balance + count
  159.         await db.update_user(self._model)
  160.  
  161.     async def remove_balance(self, count):
  162.         if not self.is_subscribe:
  163.             self._model.balance = self._model.balance + count
  164.             if self._model.balance < 0:
  165.                 self._model.balance = 0
  166.                 helpers.logs("remove_balance: у пользователя возник отрицательный баланс:" + "\n" + str(self.id))
  167.             await db.update_user(self._model)
  168.             if self.active_scenary is not None:
  169.                  await self.active_scenary.set_spent_kosmik(count)
  170.  
  171.     def set_is_banned(self, status=True):
  172.         self._model.is_banned = status
  173.         asyncio.run(db.update_user(self._model))
  174.  
  175.     async def set_is_solvent(self, status=True):
  176.         self._model.is_solvent = status
  177.         await db.update_user(self._model)
  178.  
  179.     async def update_subscription_date(self):
  180.         if self._model.subscription_date is not None:
  181.             self._model.subscription_date = self._model.subscription_date + timedelta(days=31) + timedelta(hours=12)
  182.         else:
  183.             self._model.subscription_date = datetime.now() + timedelta(days=31) + timedelta(hours=12)
  184.         await db.update_user(self._model)
  185.  
  186.     async def get_subscription_transactions(self):
  187.         param = {'accountId': str(self.user_tg_id)}
  188.  
  189.         resp = requests.post("https://api.cloudpayments.ru/subscriptions/find",
  190.                              auth=('pk_80920c858d1b89c850f7d4736b3fe', '92a561695b2167ad6169f9d2a4d48f1c'), data=param)
  191.         if resp is not None:
  192.             dictData = json.loads(resp.text)
  193.             has_active_subscribe = False
  194.             for item in dictData['Model']:
  195.                 if item['Status'] == 'Active':
  196.                     next_date = datetime.fromisoformat(item['NextTransactionDateIso'])
  197.                     if self.subscription_date is None or next_date > self.subscription_date:
  198.                         self._model.subscription_date = next_date + timedelta(days=1)
  199.                         await db.update_user(self._model)
  200.                         return
  201.                     has_active_subscribe = True
  202.             if has_active_subscribe is False:
  203.                 self._model.subscription_date = None
  204.                 await db.update_user(self._model)
  205.  
  206.  
  207.     # метод получения информации есть ли пропущеные данные регистрации
  208.     def has_no_reg_data(self):
  209.         return self.user_info.name is None or self.user_info.birthday is None or self.user_info.town is None or self.user_info.town == ""
  210.  
  211.     def new_user(self, bot, chat_id, user_tg_id, user_name, language_code, link):
  212.         self._model.chat_id = chat_id
  213.         self._model.user_tg_id = user_tg_id
  214.  
  215.         result = asyncio.run(db.insert_user(self._model))
  216.         self._model = result._data[0]
  217.  
  218.         self.user_info = User_info(self._model, user_name, language_code, True)
  219.  
  220.         self.status = enums.user_status.user
  221.         self.checkReferal(bot, link)
  222.  
  223.     def checkReferal(self, bot, link):
  224.         if " " in link:
  225.             try:
  226.                 lk = re.sub(r"[^а-яА-Яa-zA-Z0-9 ]", "", link.split()[1])
  227.                 if lk.isdigit():
  228.                     referrer_candidate = User(int(lk))
  229.                     if referrer_candidate.is_confirm_user:
  230.                         self._model.referer_id = int(lk)
  231.                         asyncio.run(referrer_candidate.add_balance(
  232.                             self.new_refer_coins))  # обновляем количество койнов у реферала
  233.                         helpers.bot_message_send_chat_id(referrer_candidate,
  234.                                                          scenary_main.get_referal_text(referrer_candidate.balance,
  235.                                                                                        self.new_refer_coins),
  236.                                                          keyboards.menu())
  237.                 else:
  238.                     self._model.referer_link = lk
  239.                 asyncio.run(db.update_user(self._model))
  240.  
  241.             except ValueError:
  242.                 pass
  243.  
  244.     def close_chat_history(self):
  245.         asyncio.run(db.close_chat_history(self._model))
  246.         self.active_scenary.is_closed = True  # close() Заменен на is_closed = True
  247.  
  248.     async def update_chat_history(self, question, answer, chat_type):
  249.         history_record = ChatHistory(self.id, question, answer, chat_type)
  250.         await history_record.insert()
  251.  
  252.     async def get_chat_history(self):
  253.         return await db.get_user_chat_history(self._model)
  254.  
  255.     def set_new_scenary(self, type: enums.scenary):
  256.         if self._model.scenary_id is not None and self._model.scenary_id != 0 and self.active_scenary != None:
  257.             self.close_scenary()
  258.         asyncio.run(db.close_chat_history(self._model))
  259.         new_scenary = Scenary(self._model)
  260.         self._model.scenary_id = new_scenary.insert(user_id=self.id, type=type)
  261.         asyncio.run(db.update_user(self._model))
  262.         self.active_scenary = Scenary(self._model)
  263.  
  264.     def close_scenary(self):
  265.         self.active_scenary.is_closed = True # close() Заменен на is_closed = True
  266.         self._model.scenary_id = 0
  267.         asyncio.run(db.update_user(self._model))
  268.         self.active_scenary = None
  269.  
  270.     def pay_partner(self, summ):
  271.         if self._model.referer_link is not None:
  272.             referer_id = asyncio.run(db.get_partner_id(self._model.referer_link))
  273.             if referer_id is not None:
  274.                 payment = RefererPayment(referer_id._data[0], self.id, summ * 0.3)
  275.                 payment.insert()
  276.  
  277.     def set_last_step_handler(self, comand):
  278.         if self._model.last_step_handler != comand:
  279.             self._model.last_step_handler = comand
  280.             asyncio.run(db.update_user_last_step_handler(self._model))
  281.  
  282.     def set_subscription_date(self, subscription_date):
  283.         self._model.subscription_date = subscription_date
  284.         asyncio.run(db.update_user(self._model))
  285.  
  286.     def calc_daily_prognoz(self):
  287.         self.active_scenary = Scenary(self._model,False)
  288.         self.active_scenary.scenary_type = enums.scenary.daily_prognoz
  289.         self.active_scenary.data=Prognoz(self.id)
  290.         if asyncio.run(routes_natal.natal_chart_prognoz(self)):
  291.             return asyncio.run(routes_chat.get_daily_prognoz(self, "natal_result"))
  292.         else:
  293.             return False
  294.     def get_all_towns(self):
  295.         result=asyncio.run(db.get_user_scenary_prognoz_towns(self.id))
  296.         if result is None or len(result)==0:
  297.             return [(0, self.user_info.town)]
  298.         result.sort(reverse=True)
  299.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement