Skip to content

Commit

Permalink
make flake8 happy
Browse files Browse the repository at this point in the history
  • Loading branch information
dongweiming committed Mar 9, 2019
1 parent 06a054d commit 2c35406
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 74 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ My Blog Using Sanic
<img width="600" src="./screenshot/admin.png" >
</p>

Inspired by [vue-element-admin](https://github.com/PanJiaChen/vue-element-admin)

## Features

* 可以通过后台对文章、标签等做增删改查
Expand All @@ -49,4 +51,5 @@ My Blog Using Sanic
## Video

[My blog](https://youtu.be/rHYvrefjZwg)
[]()

[My blog's Admin page](https://youtu.be/iZCGTvC1NPo)
8 changes: 1 addition & 7 deletions forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import markupsafe
from sanic.log import logger
from sanic_wtf import SanicForm as _SanicForm, FileAllowed, FileRequired
from sanic_wtf import SanicForm as _SanicForm
from wtforms import (
PasswordField, StringField, SubmitField, BooleanField,
SelectField, SelectMultipleField, TextAreaField)
Expand Down Expand Up @@ -50,12 +50,6 @@ def validate(self):
return success


class LoginForm(SanicForm):
name = StringField('Name', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Sign In')


class UserForm(SanicForm):
name = StringField('Name', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired()])
Expand Down
2 changes: 1 addition & 1 deletion models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sanic_jwt import exceptions #noqa
from sanic_jwt import exceptions # noqa

from .blog import Post, Tag, PostTag # noqa
from .comment import Comment # noqa
Expand Down
54 changes: 0 additions & 54 deletions models/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import math


def trunc_utf8(string, num, etc='...'):
if num >= len(string):
return string
Expand Down Expand Up @@ -74,54 +71,3 @@ class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self


class Pagination:

def __init__(self, page, per_page, total, items):
self.page = page
self.per_page = per_page
self.total = total
self.items = items

@property
def pages(self):
if self.per_page == 0 or self.total is None:
pages = 0
else:
pages = int(math.ceil(self.total / float(self.per_page)))
return pages

@property
def prev_num(self):
if not self.has_prev:
return None
return self.page - 1

@property
def has_prev(self):
return self.page > 1

@property
def has_next(self):
return self.page < self.pages

@property
def next_num(self):
if not self.has_next:
return None
return self.page + 1

def iter_pages(self, left_edge=2, left_current=2,
right_current=2, right_edge=2):
last = 0
for num in range(1, self.pages + 1):
if (
num <= left_edge
or self.page - left_current - 1 < num < self.page + right_current # noqa
or num > self.pages - right_edge
):
if last + 1 != num:
yield None
yield num
last = num
16 changes: 8 additions & 8 deletions views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from pathlib import Path

from sanic import Blueprint, response
from sanic.exceptions import abort
from sanic_jwt import protected
from sanic_jwt.decorators import instant_config
from sanic_jwt.utils import call as jwt_call
from sanic_mako import render_template

from ext import mako
from config import PER_PAGE, SHOW_PROFILE
from models import Post, User, Tag
from models.utils import Pagination
from models.user import generate_password
from models.profile import get_profile, set_profile
from forms import UserForm, PostForm, ProfileForm
Expand Down Expand Up @@ -102,10 +101,10 @@ async def _post(request, post_id=None):
ok = True
else:
ok = False
post = await post.to_sync_dict()
post = await post.to_sync_dict()
post['tags'] = [t.name for t in post['tags']]
del post['author']
return response.json({'post': post if post else None, 'ok': ok })
return response.json({'post': post if post else None, 'ok': ok})


@bp.route('/api/post/<post_id>', methods=['GET', 'PUT'])
Expand Down Expand Up @@ -170,8 +169,8 @@ async def _user(request, user_id=None):
ok = True
else:
ok = False
return response.json({'user' : await user.to_sync_dict()
if user else None, 'ok': ok })
return response.json({'user': await user.to_sync_dict()
if user else None, 'ok': ok})


@bp.route('/api/upload', methods=['POST', 'OPTIONS'])
Expand Down Expand Up @@ -216,7 +215,8 @@ async def profile(request):

avatar = profile.get('avatar')
if avatar:
profile['avatar_url'] = request.app.url_for('static', filename=f'upload/{avatar}')
profile['avatar_url'] = request.app.url_for(
'static', filename=f'upload/{avatar}')
else:
profile['avatar_url'] = ''
return response.json({'on': True, 'profile': profile})
Expand Down Expand Up @@ -245,7 +245,7 @@ async def delete(request, post_id):
abort(404)
post = await Post.get(id=post_id)
if not post:
return json({'r': 0, 'msg': 'Post not exist'})
return response.json({'r': 0, 'msg': 'Post not exist'})
await post.delete()
return response.json({'r': 1})

Expand Down
4 changes: 1 addition & 3 deletions views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
from sanic import Blueprint, response
from sanic.log import logger
from sanic.response import redirect, text, HTTPResponse
from sanic_mako import render_template
from sanic_oauth.providers import GithubClient
from werkzeug.contrib.atom import AtomFeed

from ext import mako
from forms import LoginForm
from config import AUTHOR, SITE_TITLE
from models.mc import cache
from models.blog import Post, MC_KEY_FEED, MC_KEY_SEARCH
from models.user import validate_login, create_github_user
from models.user import create_github_user

import config

Expand Down

0 comments on commit 2c35406

Please sign in to comment.