mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
promo first iteration
This commit is contained in:
parent
50ed0ac142
commit
b9fd2881d9
61
olgram/commands/promo.py
Normal file
61
olgram/commands/promo.py
Normal file
@ -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(_("Укажите аргумент: промокод. Например: <pre>/set_promo my-promo-code</pre>"),
|
||||||
|
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(_("Промокод активирован! Спасибо 🙌"))
|
10
olgram/migrations/models/12_20220329215535_update.sql
Normal file
10
olgram/migrations/models/12_20220329215535_update.sql
Normal file
@ -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";
|
@ -68,6 +68,10 @@ class User(Model):
|
|||||||
id = fields.IntField(pk=True)
|
id = fields.IntField(pk=True)
|
||||||
telegram_id = fields.BigIntField(index=True, unique=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:
|
class Meta:
|
||||||
table = 'user'
|
table = 'user'
|
||||||
|
|
||||||
@ -96,3 +100,12 @@ class DefaultAnswer(Model):
|
|||||||
id = fields.BigIntField(pk=True)
|
id = fields.BigIntField(pk=True)
|
||||||
bot = fields.ForeignKeyField("models.Bot", related_name="answers", on_delete=fields.relational.CASCADE)
|
bot = fields.ForeignKeyField("models.Bot", related_name="answers", on_delete=fields.relational.CASCADE)
|
||||||
text = fields.TextField()
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user