Skip to content

Commit 0fe0253

Browse files
authored
Merge pull request #140 from ilyarolf/feature/deployment-scripts
Feature/deployment scripts
2 parents d3a1546 + 708346f commit 0fe0253

18 files changed

Lines changed: 662 additions & 184 deletions

.env renamed to .env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ POSTGRES_PASSWORD = ""
99
DB_PORT = "5432"
1010
DB_HOST = "postgres"
1111
POSTGRES_DB = "aiogram-shop-bot"
12-
DB_PASS = ""
1312
NGROK_TOKEN = ""
1413
PAGE_ENTRIES = "8"
1514
MULTIBOT = "false"

Caddyfile.template

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{SERVER_IP_ADDRESS}.sslip.io {
2+
3+
log {
4+
output stdout
5+
level INFO
6+
}
7+
8+
encode gzip zstd
9+
10+
request_body {
11+
max_size 2MB
12+
}
13+
14+
reverse_proxy /* AiogramShopBot:5000
15+
}

docker-compose.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
services:
22
caddy:
3-
image: lucaslorentz/caddy-docker-proxy:2.9.1-alpine
3+
image: caddy
44
ports:
55
- "80:80"
66
- "443:443"
7-
environment:
8-
- CADDY_INGRESS_NETWORKS=caddy
97
networks:
108
- caddy
119
volumes:
12-
- /var/run/docker.sock:/var/run/docker.sock
13-
- ./caddy/caddy_data:/data
10+
- ./Caddyfile:/etc/caddy/Caddyfile
11+
- ./caddy_data:/data
12+
- ./caddy_config:/config
1413
restart: always
15-
extra_hosts:
16-
- "host.docker.internal:host-gateway"
1714

1815
bot:
1916
build:

enums/cryptocurrency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from utils.utils import get_text
77

88

9-
class Cryptocurrency(Enum):
9+
class Cryptocurrency(str, Enum):
1010
BNB = "BNB"
1111
BTC = "BTC"
1212
LTC = "LTC"

handlers/admin/statistics.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ async def entity_statistics(**kwargs):
4242
await callback.message.answer_photo(photo=media.media, caption=media.caption, reply_markup=kb_builder.as_markup())
4343

4444

45-
async def get_db_file(**kwargs):
46-
callback: CallbackQuery = kwargs.get("callback")
47-
await callback.answer()
48-
with open(f"./data/{config.DB_NAME}", "rb") as f:
49-
await callback.message.bot.send_document(callback.from_user.id,
50-
types.BufferedInputFile(file=f.read(), filename="database.db"))
51-
52-
5345
@statistics.callback_query(AdminIdFilter(), StatisticsCallback.filter())
5446
async def statistics_navigation(callback: CallbackQuery, state: FSMContext, callback_data: StatisticsCallback,
5547
session: AsyncSession, language: Language):
@@ -59,7 +51,6 @@ async def statistics_navigation(callback: CallbackQuery, state: FSMContext, call
5951
0: statistics_menu,
6052
1: timedelta_picker,
6153
2: entity_statistics,
62-
3: get_db_file
6354
}
6455
current_level_function = levels[current_level]
6556

handlers/user/all_categories.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ async def all_categories(**kwargs):
4545
language: Language = kwargs.get("language")
4646
state_data = await state.get_data()
4747
if callback_data.is_filter_enabled and state_data.get('filter') is not None:
48-
media, kb_builder = await CategoryService.get_buttons(callback_data, state, session, language)
48+
msg, kb_builder = await CategoryService.get_buttons(callback_data, state, session, language)
4949
elif callback_data.is_filter_enabled:
50-
media, kb_builder = await enable_search(callback_data,
50+
msg, kb_builder = await enable_search(callback_data,
5151
EntityType.CATEGORY,
5252
{"item_type": callback_data.item_type.value},
5353
state,
@@ -56,8 +56,8 @@ async def all_categories(**kwargs):
5656
else:
5757
await state.update_data(filter=None)
5858
await state.set_state()
59-
media, kb_builder = await CategoryService.get_buttons(callback_data, state, session, language)
60-
await callback.message.edit_media(media=media, reply_markup=kb_builder.as_markup())
59+
msg, kb_builder = await CategoryService.get_buttons(callback_data, state, session, language)
60+
await callback.message.edit_caption(caption=msg, reply_markup=kb_builder.as_markup())
6161

6262

6363
async def show_subcategories_in_category(**kwargs):

models/buyItem.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pydantic import BaseModel
22
from sqladmin import ModelView
3-
from sqlalchemy import Column, Integer, ForeignKey, JSON
3+
from sqlalchemy import Column, Integer, ForeignKey, JSON, ARRAY
44
from sqlalchemy.orm import relationship
55

66
from models.base import Base
@@ -15,9 +15,7 @@ class BuyItem(Base):
1515
"Buy",
1616
back_populates="buy_items"
1717
)
18-
# ARRAY CRUTCH FOR SQLALCHEMY+SQLITE 🩼
19-
# item_ids = Column(ARRAY, nullable=False)
20-
item_ids = Column(JSON, nullable=False)
18+
item_ids = Column(ARRAY(Integer), nullable=False)
2119
review = relationship(
2220
"Review",
2321
back_populates="buy_item",

models/withdrawal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ class WithdrawalDTO(BaseModel):
1414
serviceFeeAmount: float | None = None
1515
onlyCalculate: bool
1616
totalWithdrawalAmount: float | None = None
17+
paymentId: int | None = None

readme.md

Lines changed: 362 additions & 107 deletions
Large diffs are not rendered by default.

repositories/buy.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import math
22

3-
from sqlalchemy import select, func, update, or_, literal_column
3+
from sqlalchemy import select, func, update, or_, literal_column, any_
44
from sqlalchemy.ext.asyncio import AsyncSession
55
from sqlalchemy.orm import Session
66

@@ -83,8 +83,6 @@ async def get_refund_data(sort_pairs: dict[str, int],
8383
filters = [username.replace("@", "") for username in filters]
8484
filter_conditions = [User.telegram_username.icontains(name) for name in filters]
8585
conditions.append(or_(*filter_conditions))
86-
# SQLITE CRUTCH 🩼
87-
je = func.json_each(BuyItem.item_ids).table_valued("value").alias("je")
8886
stmt = (select(Buy.total_price,
8987
BuyItem.item_ids,
9088
Buy.id.label("buy_id"),
@@ -95,11 +93,7 @@ async def get_refund_data(sort_pairs: dict[str, int],
9593
User.language)
9694
.join(BuyItem, BuyItem.buy_id == Buy.id)
9795
.join(User, User.id == Buy.buyer_id)
98-
# START SQLITE CRUTCH 🩼
99-
.join(je, literal_column("1") == literal_column("1"))
100-
.join(Item, Item.id == je.c.value)
101-
# END SQLITE CRUTCH 🩼
102-
# .join(Item, Item.id.in_(BuyItem.item_ids))
96+
.join(Item, Item.id == any_(BuyItem.item_ids))
10397
.join(Subcategory, Subcategory.id == Item.subcategory_id)
10498
.where(*conditions)
10599
.distinct()
@@ -112,7 +106,6 @@ async def get_refund_data(sort_pairs: dict[str, int],
112106

113107
@staticmethod
114108
async def get_refund_data_single(buy_id: int, session: Session | AsyncSession) -> RefundDTO:
115-
je = func.json_each(BuyItem.item_ids).table_valued("value").alias("je")
116109
stmt = (select(Buy.total_price,
117110
BuyItem.item_ids,
118111
Buy.id.label("buy_id"),
@@ -124,13 +117,9 @@ async def get_refund_data_single(buy_id: int, session: Session | AsyncSession) -
124117
User.language)
125118
.join(BuyItem, BuyItem.buy_id == Buy.id)
126119
.join(User, User.id == Buy.buyer_id)
127-
# START SQLITE CRUTCH 🩼
128-
.join(je, literal_column("1") == literal_column("1"))
129-
.join(Item, Item.id == je.c.value)
130-
# END SQLITE CRUTCH 🩼
131-
# .join(Item, Item.id.in_(BuyItem.item_ids))
120+
.join(Item, Item.id.in_(BuyItem.item_ids))
132121
.join(Subcategory, Subcategory.id == Item.subcategory_id)
133-
.where(Buy.is_refunded == False, Buy.id == buy_id)
122+
.where(Buy.status == BuyStatus.REFUNDED, Buy.id == buy_id)
134123
.limit(1))
135124
refund_data = await session_execute(stmt, session)
136125
return RefundDTO.model_validate(refund_data.mappings().one(), from_attributes=True)
@@ -155,7 +144,7 @@ async def get_by_timedelta(timedelta: StatisticsTimeDelta, session: Session | As
155144
start, end = timedelta.get_time_range()
156145
stmt = select(Buy).where(Buy.buy_datetime >= start,
157146
Buy.buy_datetime <= end,
158-
Buy.is_refunded == False)
147+
Buy.status == BuyStatus.REFUNDED)
159148
buys = await session_execute(stmt, session)
160149
return [BuyDTO.model_validate(buy, from_attributes=True) for buy in buys.scalars().all()]
161150

@@ -173,14 +162,14 @@ async def get_max_page_purchase_history(buyer_id: int | None, session: AsyncSess
173162
async def get_qty_by_buyer_id(buyer_id: int, session: AsyncSession | Session) -> int:
174163
stmt = (select(func.count(Buy.id))
175164
.where(Buy.buyer_id == buyer_id,
176-
Buy.is_refunded == False))
165+
Buy.status == BuyStatus.REFUNDED))
177166
qty = await session_execute(stmt, session)
178167
return qty.scalar_one()
179168

180169
@staticmethod
181170
async def get_spent_amount(buyer_id: int, session: AsyncSession) -> float:
182171
stmt = func.coalesce((select(func.sum(Buy.total_price))
183172
.where(Buy.buyer_id == buyer_id,
184-
Buy.is_refunded == False)), 0)
173+
Buy.status == BuyStatus.REFUNDED)), 0)
185174
qty = await session_execute(stmt, session)
186175
return qty.scalar_one()

0 commit comments

Comments
 (0)