|
@@ -8,6 +8,7 @@ from typing import List, Optional, Tuple
|
|
import lc.config as c
|
|
import lc.config as c
|
|
import lc.error as e
|
|
import lc.error as e
|
|
import lc.request as r
|
|
import lc.request as r
|
|
|
|
+import lc.view as v
|
|
|
|
|
|
|
|
|
|
class Model(peewee.Model):
|
|
class Model(peewee.Model):
|
|
@@ -18,26 +19,6 @@ class Model(peewee.Model):
|
|
return playhouse.shortcuts.model_to_dict(self)
|
|
return playhouse.shortcuts.model_to_dict(self)
|
|
|
|
|
|
|
|
|
|
-@dataclass
|
|
|
|
-class Pagination:
|
|
|
|
- current: int
|
|
|
|
- last: int
|
|
|
|
-
|
|
|
|
- def previous(self) -> Optional[dict]:
|
|
|
|
- if self.current > 1:
|
|
|
|
- return {"page": self.current - 1}
|
|
|
|
- return None
|
|
|
|
-
|
|
|
|
- def next(self) -> Optional[dict]:
|
|
|
|
- if self.current < self.last:
|
|
|
|
- return {"page": self.current + 1}
|
|
|
|
- return None
|
|
|
|
-
|
|
|
|
- @classmethod
|
|
|
|
- def from_total(cls, current, total) -> "Pagination":
|
|
|
|
- return cls(current=current, last=((total - 1) // c.per_page) + 1,)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class User(Model):
|
|
class User(Model):
|
|
"""
|
|
"""
|
|
A user! you know tf this is about
|
|
A user! you know tf this is about
|
|
@@ -90,15 +71,18 @@ class User(Model):
|
|
def base_url(self) -> str:
|
|
def base_url(self) -> str:
|
|
return f"/u/{self.name}"
|
|
return f"/u/{self.name}"
|
|
|
|
|
|
- def get_links(self, as_user: r.User, page: int) -> Tuple[List["Link"], Pagination]:
|
|
|
|
|
|
+ def get_links(
|
|
|
|
+ self, as_user: r.User, page: int
|
|
|
|
+ ) -> Tuple[List[v.Link], v.Pagination]:
|
|
links = (
|
|
links = (
|
|
Link.select()
|
|
Link.select()
|
|
.where((Link.user == self) & ((self == as_user) | (Link.private == False)))
|
|
.where((Link.user == self) & ((self == as_user) | (Link.private == False)))
|
|
.order_by(-Link.created)
|
|
.order_by(-Link.created)
|
|
.paginate(page, c.per_page)
|
|
.paginate(page, c.per_page)
|
|
)
|
|
)
|
|
- pagination = Pagination.from_total(page, Link.select().count())
|
|
|
|
- return links, pagination
|
|
|
|
|
|
+ link_views = [l.to_view(as_user) for l in links]
|
|
|
|
+ pagination = v.Pagination.from_total(page, Link.select().count())
|
|
|
|
+ return link_views, pagination
|
|
|
|
|
|
def get_link(self, link_id: int) -> "Link":
|
|
def get_link(self, link_id: int) -> "Link":
|
|
return Link.get((Link.user == self) & (Link.id == link_id))
|
|
return Link.get((Link.user == self) & (Link.id == link_id))
|
|
@@ -109,22 +93,19 @@ class User(Model):
|
|
def to_dict(self) -> dict:
|
|
def to_dict(self) -> dict:
|
|
return {"id": self.id, "name": self.name}
|
|
return {"id": self.id, "name": self.name}
|
|
|
|
|
|
- def get_config(self) -> dict:
|
|
|
|
|
|
+ def get_config(self) -> v.Config:
|
|
admin_pane = None
|
|
admin_pane = None
|
|
if self.is_admin:
|
|
if self.is_admin:
|
|
user_invites = [
|
|
user_invites = [
|
|
- {
|
|
|
|
- "claimed": ui.claimed_by is not None,
|
|
|
|
- "claimant": ui.claimed_by and ui.claimed_by.name,
|
|
|
|
- "token": ui.token,
|
|
|
|
- }
|
|
|
|
|
|
+ v.UserInvite(
|
|
|
|
+ claimed=ui.claimed_by is not None,
|
|
|
|
+ claimant=ui.claimed_by and ui.claimed_by.name,
|
|
|
|
+ token=ui.token,
|
|
|
|
+ )
|
|
for ui in UserInvite.select().where(UserInvite.created_by == self)
|
|
for ui in UserInvite.select().where(UserInvite.created_by == self)
|
|
]
|
|
]
|
|
- admin_pane = {"invites": user_invites}
|
|
|
|
- return {
|
|
|
|
- "username": self.name,
|
|
|
|
- "admin_pane": admin_pane,
|
|
|
|
- }
|
|
|
|
|
|
+ admin_pane = v.AdminPane(invites=user_invites)
|
|
|
|
+ return v.Config(username=self.name, admin_pane=admin_pane,)
|
|
|
|
|
|
|
|
|
|
class Link(Model):
|
|
class Link(Model):
|
|
@@ -149,7 +130,6 @@ class Link(Model):
|
|
def by_id(id: int) -> Optional["Link"]:
|
|
def by_id(id: int) -> Optional["Link"]:
|
|
return Link.get_or_none(id=id)
|
|
return Link.get_or_none(id=id)
|
|
|
|
|
|
-
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def from_request(user: User, link: r.Link) -> "Link":
|
|
def from_request(user: User, link: r.Link) -> "Link":
|
|
l = Link.create(
|
|
l = Link.create(
|
|
@@ -171,7 +151,7 @@ class Link(Model):
|
|
|
|
|
|
req_tags = set(link.tags)
|
|
req_tags = set(link.tags)
|
|
|
|
|
|
- for hastag in self.tags:
|
|
|
|
|
|
+ for hastag in self.tags: # type: ignore
|
|
name = hastag.tag.name
|
|
name = hastag.tag.name
|
|
if name not in req_tags:
|
|
if name not in req_tags:
|
|
hastag.delete_instance()
|
|
hastag.delete_instance()
|
|
@@ -188,6 +168,19 @@ class Link(Model):
|
|
self.private = link.private
|
|
self.private = link.private
|
|
self.save()
|
|
self.save()
|
|
|
|
|
|
|
|
+ def to_view(self, as_user: User) -> v.Link:
|
|
|
|
+ return v.Link(
|
|
|
|
+ id=self.id,
|
|
|
|
+ url=self.url,
|
|
|
|
+ name=self.name,
|
|
|
|
+ description=self.description,
|
|
|
|
+ private=self.private,
|
|
|
|
+ tags=[t.tag.to_view() for t in self.tags], # type: ignore
|
|
|
|
+ created=self.created,
|
|
|
|
+ is_mine=self.user.id == as_user.id,
|
|
|
|
+ link_url=self.link_url(),
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
|
|
class Tag(Model):
|
|
class Tag(Model):
|
|
"""
|
|
"""
|
|
@@ -201,9 +194,9 @@ class Tag(Model):
|
|
def url(self) -> str:
|
|
def url(self) -> str:
|
|
return f"/u/{self.user.name}/t/{self.name}"
|
|
return f"/u/{self.user.name}/t/{self.name}"
|
|
|
|
|
|
- def get_links(self, as_user: r.User, page: int) -> Tuple[List[Link], Pagination]:
|
|
|
|
|
|
+ def get_links(self, as_user: r.User, page: int) -> Tuple[List[Link], v.Pagination]:
|
|
links = [
|
|
links = [
|
|
- ht.link
|
|
|
|
|
|
+ ht.link.to_view(as_user)
|
|
for ht in HasTag.select()
|
|
for ht in HasTag.select()
|
|
.join(Link)
|
|
.join(Link)
|
|
.where(
|
|
.where(
|
|
@@ -213,7 +206,7 @@ class Tag(Model):
|
|
.order_by(-Link.created)
|
|
.order_by(-Link.created)
|
|
.paginate(page, c.per_page)
|
|
.paginate(page, c.per_page)
|
|
]
|
|
]
|
|
- pagination = Pagination.from_total(
|
|
|
|
|
|
+ pagination = v.Pagination.from_total(
|
|
page, HasTag.select().where((HasTag.tag == self)).count(),
|
|
page, HasTag.select().where((HasTag.tag == self)).count(),
|
|
)
|
|
)
|
|
return links, pagination
|
|
return links, pagination
|
|
@@ -230,6 +223,9 @@ class Tag(Model):
|
|
|
|
|
|
return Tag.create(name=tag_name, parent=parent, user=user)
|
|
return Tag.create(name=tag_name, parent=parent, user=user)
|
|
|
|
|
|
|
|
+ def to_view(self) -> v.Tag:
|
|
|
|
+ return v.Tag(url=self.url(), name=self.name)
|
|
|
|
+
|
|
|
|
|
|
class HasTag(Model):
|
|
class HasTag(Model):
|
|
"""
|
|
"""
|