diff --git a/olgram/commands/promo.py b/olgram/commands/promo.py new file mode 100644 index 0000000..0ab6c5c --- /dev/null +++ b/olgram/commands/promo.py @@ -0,0 +1,61 @@ +""" +Здесь промокоды +""" + + +from aiogram import types +from aiogram.dispatcher import FSMContext +from olgram.models import models +from uuid import UUID + +from olgram.router import dp +from olgram.settings import OlgramSettings +from locales.locale import _ + + +@dp.message_handler(commands=["newpromo"], state="*") +async def new_promo(message: types.Message, state: FSMContext): + """ + Команда /newpromo + """ + + if message.chat.id != OlgramSettings.supervisor_id(): + await message.answer(_("Недостаточно прав")) + return + + promo = await models.Promo() + await message.answer(_("Новый промокод\n```{0}```").format(promo.code), parse_mode="Markdown") + + await promo.save() + + +@dp.message_handler(commands=["setpromo"], state="*") +async def set_promo(message: types.Message, state: FSMContext): + """ + Команда /setpromo + """ + + arg = message.get_args() + if not arg: + return await message.answer(_("Укажите аргумент: промокод. Например:
/set_promo my-promo-code
"), + parse_mode="HTML") + + arg = arg.strip() + + try: + UUID(arg) + except ValueError: + return await message.answer(_("Промокод не найден")) + + promo = await models.Promo.get_or_none(code=arg) + if not promo: + return await message.answer(_("Промокод не найден")) + + if promo.owner: + return await message.answer(_("Промокод уже использован")) + + user, created = await models.User.get_or_create(telegram_id=message.from_user.id) + promo.owner = user + await promo.save(update_fields=["owner_id"]) + + await message.answer(_("Промокод активирован! Спасибо 🙌")) diff --git a/olgram/migrations/models/12_20220329215535_update.sql b/olgram/migrations/models/12_20220329215535_update.sql new file mode 100644 index 0000000..72453a3 --- /dev/null +++ b/olgram/migrations/models/12_20220329215535_update.sql @@ -0,0 +1,10 @@ +-- upgrade -- +CREATE TABLE IF NOT EXISTS "promo" ( + "id" BIGSERIAL NOT NULL PRIMARY KEY, + "code" UUID NOT NULL, + "date" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "owner_id" INT REFERENCES "user" ("id") ON DELETE SET NULL +); +CREATE INDEX IF NOT EXISTS "idx_promo_code_9b981a" ON "promo" ("code"); +-- downgrade -- +DROP TABLE IF EXISTS "promo"; diff --git a/olgram/models/models.py b/olgram/models/models.py index b9bb6f7..dc529c7 100644 --- a/olgram/models/models.py +++ b/olgram/models/models.py @@ -68,6 +68,10 @@ class User(Model): id = fields.IntField(pk=True) telegram_id = fields.BigIntField(index=True, unique=True) + async def is_promo(self): + await self.fetch_related("promo") + return bool(self.promo) + class Meta: table = 'user' @@ -96,3 +100,12 @@ class DefaultAnswer(Model): id = fields.BigIntField(pk=True) bot = fields.ForeignKeyField("models.Bot", related_name="answers", on_delete=fields.relational.CASCADE) text = fields.TextField() + + +class Promo(Model): + id = fields.BigIntField(pk=True) + code = fields.UUIDField(default=uuid4, index=True) + date = fields.DatetimeField(auto_now_add=True) + + owner = fields.ForeignKeyField("models.User", related_name="promo", on_delete=fields.relational.SET_NULL, + null=True, default=None)