diff --git a/README.md b/README.md index cea18f4..a73093b 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ My Blog Using Sanic

+Inspired by [vue-element-admin](https://github.com/PanJiaChen/vue-element-admin) + ## Features * 可以通过后台对文章、标签等做增删改查 @@ -49,4 +51,5 @@ My Blog Using Sanic ## Video [My blog](https://youtu.be/rHYvrefjZwg) -[]() + +[My blog's Admin page](https://youtu.be/iZCGTvC1NPo) diff --git a/forms.py b/forms.py index 0f1132e..b1fb6a6 100644 --- a/forms.py +++ b/forms.py @@ -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) @@ -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()]) diff --git a/models/__init__.py b/models/__init__.py index 2642e6c..657ff4c 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -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 diff --git a/models/utils.py b/models/utils.py index 9ed3a0b..16612a2 100644 --- a/models/utils.py +++ b/models/utils.py @@ -1,6 +1,3 @@ -import math - - def trunc_utf8(string, num, etc='...'): if num >= len(string): return string @@ -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 diff --git a/views/admin.py b/views/admin.py index 3b4b27a..7e38694 100644 --- a/views/admin.py +++ b/views/admin.py @@ -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 @@ -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/', methods=['GET', 'PUT']) @@ -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']) @@ -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}) @@ -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}) diff --git a/views/index.py b/views/index.py index 9bacc45..f5c4c2d 100644 --- a/views/index.py +++ b/views/index.py @@ -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