def post(self): data = parser.parse_args() user = UserModel.get_one(args={'username': data['username']}) if not user: return { 'status': 404, 'message': 'username {} does not exist'.format(data['username']) }, 404 if user.verify_password(data['password']): access_token = create_access_token(identity=data['username']) refresh_token = create_refresh_token(identity=data['username']) return { 'message': 'Logged in as {}'.format(user.username), 'access_token': access_token, 'refresh_token': refresh_token } else: return {'message': 'Wrong credentials'}
def get(self, id): return UserModel.get_one(id)
class IndexHandler(BaseHandler): def __init__(self, application, request, **kwargs): self._lrclib = LrcLib() self._info = SongInfo() self._music = MusicModel() self._users = UserModel() super(IndexHandler, self).__init__(application, request, **kwargs) def get(self): user_id = self.current_user log.debug("user_id is {0}".format(user_id)) # if not user_id: # return self.render('login.html') account = "" if user_id: user = self._users.get_one(user_id) log.debug(user) if user: account = user["account"] return self.render('index.html', current_user = account, domain=self.request.full_url()) def on_error(self): return self.write(json.dumps({ "code": 1 })) def post(self): request = json.loads(self.request.body) song = dict( sid = request.get("songId"), artist = request.get("artist"), title = request.get("title"), channel = request.get("channel"), share_url = request.get("shareUrl"), album_img = request.get("albumImgUrl"), start_time = request.get("startTime") ) lrc, song_info = self._music.get_rep(song["sid"]) if lrc and song_info: lrc.update({"startTime": song["start_time"]}) return self.write(json.dumps({ "code": 0, "lyricsInfo": lrc, "songInfo": song_info })) lrc = self._lrclib.getlrc(simplify(song["title"]), simplify(song["artist"])) if not song_info: info_res = self._info.get_info(song["share_url"]) if not info_res: return self.on_error() song_info = info_res["song"][0] song_info = { "album": song_info["albumtitle"], "albumId": song_info["aid"], "albumImgUrl": song_info["picture"], "albumUrl": song_info["album"], "artist": song_info["artist"], "company": song_info["company"], "duration": song_info["length"], "mp3Url": song_info["url"], "rating": song_info["rating_avg"], "releaseYear": song_info["public_time"], "songId": song_info["sid"], "ssid": song_info["ssid"], "startToken": song_info["start_token"], "title": song_info["title"], "shareUrl": song["share_url"] } response = json.dumps({ "code": 0, "lyricsInfo": { "lyrics": lrc, "offset": 0, "startTime": song["start_time"] }, "songInfo": song_info }) self._music.set_rep(song["sid"], dict(lyrics=lrc,offset=0), song_info) return self.write(response)
class UserHandler(tornado.web.RequestHandler): def __init__(self, application, request, **kwargs): self.session = SessionTools() self.users = UserModel() super(UserHandler, self).__init__(application, request, **kwargs) def get(self): log.debug("get") user_id = self.get_argument("user_id", None) if not user_id: return self.write(json.dumps(self.users.get_all())) return self.write(json.dumps(self.users.get_one(user_id))) def post(self): account = self.get_body_argument("account", None) passwd = self.get_body_argument("passwd", None) if not account or not passwd: return self.write(json.dumps({ u"error": 1, u"content": u"请输入用户名或密码!" })) user_id = self.users.login(account, passwd) if not user_id: return self.write(json.dumps({ u"error": 2, u"content": u"用户名或密码错误" })) self.session.login(self.set_cookie, user_id) return self.redirect('/') def admin_required(fn): @wraps(fn) def func(self): user_id = self.session.logged_user(self.get_cookie) if not user_id: return self.write(json.dumps({ u"error": 3, u"content": u"请先登录" })) user_limits = self.users.get_limits() if user_limits != "admin" and user_limits != "root": return self.write(json.dumps({ u"error": 4, u"content": u"权限不足" })) return fn(self) return func @admin_required def put(self): account = self.get_body_argument("account") passwd = self.get_body_argument("passwd") name = self.get_body_argument("name") limits = self.get_body_argument("limits") result = self.users.add_user({ u"account": account, u"passwd": passwd, u"name": name, u"limits": limits}) result = result if result else "" self.redirect("/results?type=user&operating=add&id={3}&account={0}&name={1}&limits={2}".format(account, name, limits, result)) @admin_required def delete(self): user_id = self.get_body_argument("user_id") self.users.remove(user_id) self.redirect("/results?type=user&operating=delete")