Browse Source

Address some lints

Getty Ritter 3 years ago
parent
commit
cc7e36294c
4 changed files with 37 additions and 41 deletions
  1. 22 25
      lc/app.py
  2. 3 3
      lc/error.py
  3. 7 7
      lc/model.py
  4. 5 6
      lc/web.py

+ 22 - 25
lc/app.py

@@ -1,7 +1,4 @@
-import contextlib
-import os
 import flask
-import sys
 
 import lc.config as c
 import lc.error as e
@@ -181,54 +178,54 @@ class CreateLink(Endpoint):
     def api_post(self, user: str):
         u = self.require_authentication(user)
         req = self.request_data(r.Link)
-        l = m.Link.from_request(u, req)
-        return self.api_ok(l.link_url(), l.to_dict())
+        link = m.Link.from_request(u, req)
+        return self.api_ok(link.link_url(), link.to_dict())
 
 
-@endpoint("/u/<string:user>/l/<string:link>")
+@endpoint("/u/<string:user>/l/<string:link_id>")
 class GetLink(Endpoint):
-    def api_get(self, user: str, link: str):
+    def api_get(self, user: str, link_id: str):
         u = self.require_authentication(user)
-        l = u.get_link(int(link))
-        return self.api_ok(l.link_url(), l.to_dict())
+        link = u.get_link(int(link_id))
+        return self.api_ok(link.link_url(), link.to_dict())
 
-    def api_post(self, user: str, link: str):
+    def api_post(self, user: str, link_id: str):
         u = self.require_authentication(user)
-        l = u.get_link(int(link))
+        link = u.get_link(int(link_id))
         req = self.request_data(r.Link)
-        l.update_from_request(u, req)
-        raise e.LCRedirect(l.link_url())
+        link.update_from_request(u, req)
+        raise e.LCRedirect(link.link_url())
 
-    def api_delete(self, user: str, link: str):
+    def api_delete(self, user: str, link_id: str):
         u = self.require_authentication(user)
-        u.get_link(int(link)).delete_instance()
+        u.get_link(int(link_id)).delete_instance()
         return self.api_ok(u.base_url())
 
-    def html(self, user: str, link: str):
-        l = m.User.by_slug(user).get_link(int(link))
+    def html(self, user: str, link_id: str):
+        link = m.User.by_slug(user).get_link(int(link_id))
         return render(
             "main",
             v.Page(
-                title=f"link {l.name}",
+                title=f"link {link.name}",
                 content=render(
-                    "linklist", v.LinkList([l.to_view(self.user)], [], user=user)
+                    "linklist", v.LinkList([link.to_view(self.user)], [], user=user)
                 ),
                 user=self.user,
             ),
         )
 
 
-@endpoint("/u/<string:slug>/l/<string:link>/edit")
+@endpoint("/u/<string:slug>/l/<string:link_id>/edit")
 class EditLink(Endpoint):
-    def html(self, slug: str, link: str):
+    def html(self, slug: str, link_id: str):
         u = self.require_authentication(slug)
         all_tags = u.get_tags()
-        l = u.get_link(int(link))
+        link = u.get_link(int(link_id))
         return render(
             "main",
             v.Page(
                 title="login",
-                content=render("edit_link", v.SingleLink(l, all_tags)),
+                content=render("edit_link", v.SingleLink(link, all_tags)),
                 user=self.user,
             ),
         )
@@ -274,11 +271,11 @@ class GetStringSearch(Endpoint):
 @endpoint("/u/<string:user>/import")
 class PinboardImport(Endpoint):
     def html(self, user: str):
-        u = self.require_authentication(user)
+        _ = self.require_authentication(user)
         return render(
             "main",
             v.Page(
-                title=f"import pinboard data",
+                title="import pinboard data",
                 content=render("import"),
                 user=self.user,
             ),

+ 3 - 3
lc/error.py

@@ -72,7 +72,7 @@ class BadPassword(LCException):
 @dataclass
 class NotImplemented(LCException):
     def __str__(self):
-        return f"Bad request: no handler for route."
+        return "Bad request: no handler for route."
 
     def http_code(self) -> int:
         return 404
@@ -81,7 +81,7 @@ class NotImplemented(LCException):
 @dataclass
 class BadPermissions(LCException):
     def __str__(self):
-        return f"Insufficient permissions."
+        return "Insufficient permissions."
 
     def http_code(self) -> int:
         return 403
@@ -123,7 +123,7 @@ class AlreadyUsedInvite(LCException):
 @dataclass
 class MismatchedPassword(LCException):
     def __str__(self):
-        return f"Provided passwords do not match. Please check your passwords."
+        return "Provided passwords do not match. Please check your passwords."
 
     def http_code(self) -> int:
         return 400

+ 7 - 7
lc/model.py

@@ -247,13 +247,13 @@ class Link(Model):
             .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, Link.select().count())
         return link_views, pagination
 
     @staticmethod
     def from_request(user: User, link: r.Link) -> "Link":
-        l = Link.create(
+        new_link = Link.create(
             url=link.url,
             name=link.name,
             description=link.description,
@@ -263,8 +263,8 @@ class Link(Model):
         )
         for tag_name in link.tags:
             tag = Tag.get_or_create_tag(user, tag_name)
-            HasTag.get_or_create(link=l, tag=tag)
-        return l
+            HasTag.get_or_create(link=new_link, tag=tag)
+        return new_link
 
     def update_from_request(self, user: User, link: r.Link):
         with self.atomic():
@@ -343,7 +343,7 @@ class Tag(Model):
     def get_family(self) -> Iterator["Tag"]:
         yield self
         p = self
-        while (p := p.parent) :
+        while p := p.parent:
             yield p
 
     BAD_TAG_CHARS = set("{}[]\\()#?")
@@ -354,7 +354,7 @@ class Tag(Model):
 
     @staticmethod
     def get_or_create_tag(user: User, tag_name: str) -> "Tag":
-        if (t := Tag.get_or_none(name=tag_name, user=user)) :
+        if t := Tag.get_or_none(name=tag_name, user=user):
             return t
 
         if not Tag.is_valid_tag_name(tag_name):
@@ -412,7 +412,7 @@ class UserInvite(Model):
 
     @staticmethod
     def by_code(token: str) -> "UserInvite":
-        if (u := UserInvite.get_or_none(token=token)) :
+        if u := UserInvite.get_or_none(token=token):
             return u
         raise e.NoSuchInvite(invite=token)
 

+ 5 - 6
lc/web.py

@@ -27,7 +27,7 @@ class Endpoint:
         # try finding the token
         token = None
         # first check the HTTP headers
-        if (auth := flask.request.headers.get("Authorization", None)) :
+        if auth := flask.request.headers.get("Authorization", None):
             token = auth.split()[1]
         # if that fails, check the session
         elif flask.session.get("auth", None):
@@ -40,7 +40,7 @@ class Endpoint:
         # it contains a valid user password, too
         try:
             payload = c.app.load_token(token)
-        except:
+        except Exception:
             # TODO: be more specific about what errors we're catching
             # here!
             return
@@ -58,7 +58,7 @@ class Endpoint:
     def just_get_user() -> Optional[m.User]:
         try:
             return Endpoint().user
-        except:
+        except Exception:
             # this is going to catch everything on the off chance that
             # there's a bug in the user-validation code: this is used
             # in error handlers, so we should be resilient to that!
@@ -169,6 +169,7 @@ def endpoint(route: str):
     def do_endpoint(endpoint_class: Type[Endpoint]):
         # we'll just make that explicit here
         assert Endpoint in endpoint_class.__bases__
+
         # finally, we need a function that we'll give to Flask in
         # order to actually dispatch to. This is the actual routing
         # function, which is why it just creates an instance of the
@@ -205,7 +206,6 @@ def render(name: str, data: Optional[v.View] = None) -> str:
 
 @c.app.app.errorhandler(404)
 def handle_404(e):
-    user = Endpoint.just_get_user()
     url = flask.request.path
     error = v.Error(code=404, message=f"Page {url} not found")
     page = v.Page(title="not found", content=render("error", error), user=None)
@@ -214,8 +214,7 @@ def handle_404(e):
 
 @c.app.app.errorhandler(500)
 def handle_500(e):
-    user = Endpoint.just_get_user()
     c.log(f"Internal error: {e}")
-    error = v.Error(code=500, message=f"An unexpected error occurred")
+    error = v.Error(code=500, message="An unexpected error occurred")
     page = v.Page(title="500", content=render("error", error), user=None)
     return render("main", page)