283 lines
13 KiB
Python
283 lines
13 KiB
Python
import flet as ft
|
|
import requests
|
|
from config import API_BASE_URL
|
|
|
|
class Subscription:
|
|
def __init__(self, page: ft.Page, dashboard):
|
|
self.page = page
|
|
self.dashboard = dashboard
|
|
self.subscription = self.get_current_subscription_plan()
|
|
self.plan = {
|
|
'first_2_months':'First Two Months' ,
|
|
'monthly':'Monthly',
|
|
'yearly':'Yearly'
|
|
}
|
|
self.status = {
|
|
'active':'Active',
|
|
'cancelled':'Cancelled',
|
|
'expired':'Expired',
|
|
'less_than_5_days':'Less than 5 days'
|
|
}
|
|
self.current_subscription_plan = ft.Text(self.plan[self.subscription['plan']] if self.subscription else "No subscription")
|
|
self.current_subscription_status = ft.Text(self.status[self.subscription['status']] if self.subscription else "None")
|
|
self.monthly_subscription_price = ft.Text(
|
|
"100 Euro/Month",
|
|
weight=ft.FontWeight.BOLD,
|
|
size=18,
|
|
color=ft.Colors.WHITE
|
|
)
|
|
self.year_subscription_price = ft.Text(
|
|
"1000 Euro/Year",
|
|
weight=ft.FontWeight.BOLD,
|
|
size=18,
|
|
color=ft.Colors.WHITE
|
|
)
|
|
self.first_subscription_price = ft.Text(
|
|
"0 Euro/Month",
|
|
weight=ft.FontWeight.BOLD,
|
|
size=18,
|
|
color=ft.Colors.WHITE
|
|
)
|
|
|
|
def get_current_subscription_plan(self):
|
|
try:
|
|
token = self.page.client_storage.get("token")
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
response = requests.get(f"{API_BASE_URL}/subscription/", headers=headers)
|
|
#print(response.text)
|
|
return response.json()[-1] if response.status_code == 200 else None
|
|
except Exception as e:
|
|
print("Error loading subscription:", e)
|
|
|
|
def on_first_two_months_btn_click(self, e):
|
|
try:
|
|
token = self.page.client_storage.get("token")
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
response = requests.post(f"{API_BASE_URL}/subscription/first_2_months", headers=headers, )
|
|
#print(response.text)
|
|
self.change_subscription_to_active('first_2_months', 'active')
|
|
return response.json() if response.status_code == 200 else None
|
|
except Exception as e:
|
|
print("Error loading subscription:", e)
|
|
|
|
def on_month_subscription_btn_click(self, e):
|
|
try:
|
|
token = self.page.client_storage.get("token")
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
response = requests.post(f"{API_BASE_URL}/subscription/one_month", headers=headers)
|
|
#print(response.text)
|
|
self.change_subscription_to_active('monthly', 'active')
|
|
return response.json() if response.status_code == 200 else None
|
|
except Exception as e:
|
|
print("Error loading subscription:", e)
|
|
|
|
def on_year_subscription_btn_click(self, e):
|
|
try:
|
|
token = self.page.client_storage.get("token")
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
response = requests.post(f"{API_BASE_URL}/subscription/one_year", headers=headers)
|
|
#print(response.text)
|
|
self.change_subscription_to_active('yearly', 'active')
|
|
return response.json() if response.status_code == 200 else None
|
|
except Exception as e:
|
|
print("Error loading subscription:", e)
|
|
|
|
def change_subscription_to_active(self, plan, status):
|
|
self.dashboard.subscription_status_bottom.value = "Active"
|
|
self.dashboard.subscription_status_bottom.color = ft.Colors.GREEN
|
|
self.dashboard.subscription_status_bottom.update()
|
|
|
|
self.current_subscription_plan.value = self.plan[plan]
|
|
self.current_subscription_plan.update()
|
|
|
|
self.current_subscription_status.value = self.status[status]
|
|
self.current_subscription_status.update()
|
|
|
|
def get_client_access(self):
|
|
token = self.page.client_storage.get("token")
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
response = requests.get(f"{API_BASE_URL}/profile/", headers=headers, timeout=10)
|
|
user = response.json()
|
|
if user['user_role'] == 'user':
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def build(self):
|
|
return ft.Container(
|
|
content=ft.Column(
|
|
[
|
|
ft.Row(
|
|
[
|
|
ft.Text("Subscription", size=24, weight=ft.FontWeight.BOLD),
|
|
ft.Column(
|
|
[
|
|
ft.Row(
|
|
[
|
|
ft.Text("Current Subscription Plan:", weight=ft.FontWeight.BOLD),
|
|
self.current_subscription_plan
|
|
]
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.Text("Subscription Status:"),
|
|
self.current_subscription_status
|
|
]
|
|
)
|
|
]
|
|
)
|
|
],
|
|
alignment=ft.MainAxisAlignment.SPACE_BETWEEN
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.Container(
|
|
content = ft.Column(
|
|
[
|
|
ft.Icon(
|
|
name = ft.Icons.AUTORENEW,
|
|
size=150
|
|
),
|
|
ft.Container(
|
|
content=ft.Column(
|
|
[
|
|
ft.Text("First Two Months", weight=ft.FontWeight.BOLD),
|
|
self.first_subscription_price
|
|
],
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
|
|
expand=True
|
|
),
|
|
bgcolor=ft.Colors.BLUE_200,
|
|
padding=20,
|
|
width=250
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.FilledButton("Add", width=150, on_click=self.on_first_two_months_btn_click)
|
|
],
|
|
alignment=ft.MainAxisAlignment.CENTER,
|
|
expand=True
|
|
)
|
|
],
|
|
expand = True ,
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER
|
|
),
|
|
border_radius=20,
|
|
border=ft.border.all(1, ft.Colors.GREY_300),
|
|
bgcolor=ft.Colors.BLUE_50,
|
|
padding = ft.padding.symmetric(vertical=20),
|
|
width=250,
|
|
height=350
|
|
),
|
|
ft.Container(
|
|
content = ft.Column(
|
|
[
|
|
ft.Icon(
|
|
name = ft.Icons.AUTORENEW,
|
|
size=150
|
|
),
|
|
ft.Container(
|
|
content=ft.Column(
|
|
[
|
|
ft.Text("One Month Subscription", weight=ft.FontWeight.BOLD),
|
|
self.monthly_subscription_price
|
|
],
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
|
|
expand=True
|
|
),
|
|
bgcolor=ft.Colors.BLUE_200,
|
|
padding=20,
|
|
width=250
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.FilledButton("Add / Renew", width=150, on_click=self.on_month_subscription_btn_click)
|
|
],
|
|
alignment=ft.MainAxisAlignment.CENTER,
|
|
expand=True
|
|
)
|
|
],
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER
|
|
),
|
|
border_radius=20,
|
|
border=ft.border.all(1, ft.Colors.GREY_300),
|
|
bgcolor=ft.Colors.BLUE_50,
|
|
padding = ft.padding.symmetric(vertical=20),
|
|
width=250,
|
|
height=350
|
|
),
|
|
ft.Container(
|
|
content = ft.Column(
|
|
[
|
|
ft.Icon(
|
|
name = ft.Icons.AUTORENEW,
|
|
size=150
|
|
),
|
|
ft.Container(
|
|
content=ft.Column(
|
|
[
|
|
ft.Text("One Year Subscription", weight=ft.FontWeight.BOLD),
|
|
self.year_subscription_price
|
|
],
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
|
|
expand=True
|
|
),
|
|
bgcolor=ft.Colors.BLUE_200,
|
|
padding=20,
|
|
width=250
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.FilledButton("Add / Renew", width=150, on_click=self.on_year_subscription_btn_click)
|
|
],
|
|
alignment=ft.MainAxisAlignment.CENTER,
|
|
expand=True
|
|
)
|
|
],
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER
|
|
),
|
|
border_radius=20,
|
|
border=ft.border.all(1, ft.Colors.GREY_300),
|
|
bgcolor=ft.Colors.BLUE_50,
|
|
padding = ft.padding.symmetric(vertical=20),
|
|
width=250,
|
|
height=350
|
|
)
|
|
],
|
|
alignment=ft.MainAxisAlignment.CENTER,
|
|
spacing=20
|
|
)
|
|
],
|
|
alignment=ft.MainAxisAlignment.START,
|
|
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
|
|
expand=True,
|
|
spacing=50
|
|
),
|
|
expand=True
|
|
) if self.get_client_access() else ft.Container(
|
|
content=ft.Column(
|
|
[
|
|
ft.Row(
|
|
[
|
|
ft.Text("Subscription", size=24, weight=ft.FontWeight.BOLD),
|
|
],
|
|
alignment=ft.MainAxisAlignment.SPACE_BETWEEN
|
|
),
|
|
ft.Row(
|
|
[
|
|
ft.Text(
|
|
"You do not have access to this page content",
|
|
size=24,
|
|
weight=ft.FontWeight.BOLD,
|
|
color=ft.Colors.RED
|
|
)
|
|
],
|
|
alignment=ft.MainAxisAlignment.CENTER
|
|
),
|
|
ft.Text("")
|
|
],
|
|
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
|
|
expand=True
|
|
),
|
|
expand=True
|
|
) |