|
@@ -1,5 +1,4 @@
|
|
|
from contextlib import contextmanager
|
|
|
-from dataclasses import dataclass
|
|
|
import datetime
|
|
|
import json
|
|
|
from passlib.apps import custom_app_context as pwd
|
|
@@ -34,7 +33,7 @@ class Meta(Model):
|
|
|
def fetch():
|
|
|
try:
|
|
|
return Meta.get(id=0)
|
|
|
- except:
|
|
|
+ except Exception:
|
|
|
meta = Meta.create(id=0)
|
|
|
return meta
|
|
|
|
|
@@ -107,10 +106,10 @@ class User(Model):
|
|
|
self, as_user: Optional["User"], page: int
|
|
|
) -> Tuple[List[v.Link], v.Pagination]:
|
|
|
query = Link.select().where(
|
|
|
- (Link.user == self) & ((self == as_user) | (Link.private == False))
|
|
|
+ (Link.user == self) & ((self == as_user) | (Link.private == False)) # noqa: E712
|
|
|
)
|
|
|
links = query.order_by(-Link.created).paginate(page, c.app.per_page)
|
|
|
- link_views = [l.to_view(as_user) for l in links]
|
|
|
+ link_views = [link.to_view(as_user) for link in links]
|
|
|
pagination = v.Pagination.from_total(page, query.count())
|
|
|
return link_views, pagination
|
|
|
|
|
@@ -143,38 +142,38 @@ class User(Model):
|
|
|
def import_pinboard_data(self, stream):
|
|
|
try:
|
|
|
links = json.load(stream)
|
|
|
- except json.decoder.JSONDecodeError as exn:
|
|
|
+ except json.decoder.JSONDecodeError:
|
|
|
raise e.BadFileUpload("could not parse file as JSON")
|
|
|
|
|
|
if not isinstance(links, list):
|
|
|
- raise e.BadFileUpload(f"expected a list")
|
|
|
+ raise e.BadFileUpload("expected a list")
|
|
|
|
|
|
# create and (for this session) cache the tags
|
|
|
tags = {}
|
|
|
- for l in links:
|
|
|
- if "tags" not in l:
|
|
|
+ for link in links:
|
|
|
+ if "tags" not in link:
|
|
|
raise e.BadFileUpload("missing key {exn.args[0]}")
|
|
|
- for t in l["tags"].split():
|
|
|
+ for t in link["tags"].split():
|
|
|
if t in tags:
|
|
|
continue
|
|
|
|
|
|
tags[t] = Tag.get_or_create_tag(self, t)
|
|
|
|
|
|
with self.atomic():
|
|
|
- for l in links:
|
|
|
+ for link in links:
|
|
|
try:
|
|
|
- time = datetime.datetime.strptime(l["time"], "%Y-%m-%dT%H:%M:%SZ")
|
|
|
+ time = datetime.datetime.strptime(link["time"], "%Y-%m-%dT%H:%M:%SZ")
|
|
|
ln = Link.create(
|
|
|
- url=l["href"],
|
|
|
- name=l["description"],
|
|
|
- description=l["extended"],
|
|
|
- private=l["shared"] == "no",
|
|
|
+ url=link["href"],
|
|
|
+ name=link["description"],
|
|
|
+ description=link["extended"],
|
|
|
+ private=link["shared"] == "no",
|
|
|
created=time,
|
|
|
user=self,
|
|
|
)
|
|
|
except KeyError as exn:
|
|
|
raise e.BadFileUpload(f"missing key {exn.args[0]}")
|
|
|
- for t in l["tags"].split():
|
|
|
+ for t in link["tags"].split():
|
|
|
HasTag.get_or_create(link=ln, tag=tags[t])
|
|
|
|
|
|
def get_tags(self) -> List[v.Tag]:
|
|
@@ -184,7 +183,9 @@ class User(Model):
|
|
|
)
|
|
|
|
|
|
def get_related_tags(self, tag: "Tag") -> List[v.Tag]:
|
|
|
- # SELECT * from has_tag t1, has_tag t2, link l WHERE t1.link_id == l.id AND t2.link_id == l.id AND t1.id != t2.id AND t1 = self
|
|
|
+ # SELECT * from has_tag t1, has_tag t2, link l
|
|
|
+ # WHERE t1.link_id == l.id AND t2.link_id == l.id
|
|
|
+ # AND t1.id != t2.id AND t1 = self
|
|
|
SelfTag = HasTag.alias()
|
|
|
query = (
|
|
|
HasTag.select(HasTag.tag)
|
|
@@ -206,11 +207,11 @@ class User(Model):
|
|
|
"""
|
|
|
query = Link.select().where(
|
|
|
(Link.user == self)
|
|
|
- & ((self == as_user) | (Link.private == False))
|
|
|
+ & ((self == as_user) | (Link.private == False)) # noqa: E712
|
|
|
& (Link.name.contains(needle) | Link.description.contains(needle))
|
|
|
)
|
|
|
links = query.order_by(-Link.created).paginate(page, c.app.per_page)
|
|
|
- link_views = [l.to_view(as_user) for l in links]
|
|
|
+ link_views = [link.to_view(as_user) for link in links]
|
|
|
pagination = v.Pagination.from_total(page, query.count())
|
|
|
return link_views, pagination
|
|
|
|
|
@@ -243,7 +244,7 @@ class Link(Model):
|
|
|
) -> Tuple[List["Link"], v.Pagination]:
|
|
|
links = (
|
|
|
Link.select()
|
|
|
- .where((Link.user == as_user) | (Link.private == False))
|
|
|
+ .where((Link.user == as_user) | (Link.private == False)) # noqa: E712
|
|
|
.order_by(-Link.created)
|
|
|
.paginate(page, c.app.per_page)
|
|
|
)
|
|
@@ -330,7 +331,7 @@ class Tag(Model):
|
|
|
.join(Link)
|
|
|
.where(
|
|
|
(HasTag.tag == self)
|
|
|
- & ((HasTag.link.user == as_user) | (HasTag.link.private == False))
|
|
|
+ & ((HasTag.link.user == as_user) | (HasTag.link.private == False)) # noqa: E712
|
|
|
)
|
|
|
)
|
|
|
links = [
|