def get(self): try: id = self.request.GET["id"] user = ndb.Key(urlsafe=id).get() noReadMsg = Notification.query(Notification.user == user.username, Notification.read == 0) for i in noReadMsg: if i.read == 0: noReadMsg = 0 break follow = Follow.query(Follow.username == user.username) values = { "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "id": id, "follows": follow, "noReadMsg": noReadMsg } jinja = jinja2.get_jinja2(app=self.app) self.response.write(jinja.render_template("showfollow.html", **values)) return except: self.response.write("An error occurred.") return
def get(self): try: id = self.request.GET["id"] currentuser = ndb.Key(urlsafe=id).get() user = self.request.get("user", "").strip() user = Register.query(Register.username == user) if currentuser and user.count() != 0: for i in user: user = i unfollow = Follow.query( ndb.AND(Follow.username == currentuser.username, Follow.usernameToFollow == user.username)) if unfollow.count() == 0: currentuser.follow = currentuser.follow + 1 currentuser.put() user.followers = user.followers + 1 user.put() follow = Follow(username=currentuser.username, usernameToFollow=user.username) follow.put() time.sleep(1) self.redirect("/user/showusers?search=" + user.username + "&id=" + id) else: currentuser.follow = currentuser.follow - 1 currentuser.put() user.followers = user.followers - 1 user.put() for i in unfollow: unfollow = i unfollow.key.delete() self.redirect("/user/showusers?search=" + user.username + "&id=" + id) else: self.response.write("An error occurred.") return except: self.response.write("An error occurred.") return
def unfollow(self, related_type, relate_id): related_type = int(related_type) # 1. 首先需要登录 if not self.current_user(): return -1, '请先登录后再做此操作' # 2. 父级类型必须存在 if related_type not in OBJECT_TYPES.values(): return -2, '不合法的提交' # 3. relate_id 存在检查 obj = OBJECT_TYPES.get_object(related_type, relate_id) if not obj: return -3, '目标不存在' # 4. 存入数据库 Follow.remove_if_exist(related_type, relate_id, self.current_user()) return 0, '成功'
def get(self, topic_id): topic = Topic.get_by_pk(topic_id) if topic: topic.view_count_inc() count, user_topics = Topic.get_list_by_user(topic.user) user_topics = user_topics.limit(10) follow = JsDict() if self.current_user(): follow.topic = Follow.exists(OBJECT_TYPES.TOPIC, topic_id, self.current_user()) follow.author = Follow.exists(OBJECT_TYPES.USER, topic.user.id, self.current_user()) self.render( "forum/topic.html", nav="index", page_title=page_title(topic.title, topic.board.title, "社区"), topic=topic, user_topics=user_topics, follow=follow, ) else: self.write_error(404)
def get(self, username): user = User.get_by_username(username) if user: page = self.get_argument('p', '1') page = int(page) if page.isdigit() else 1 count, query = Follow.get_list_by_user(user, OBJECT_TYPES.USER) pagination_ret = pagination(count, query, config.REPLY_PAGE_SIZE, page) self.render('user/user_page.html', user=user, tab={'user_follow_page': 'active'}, count=count, avatar_html=avatar_generate(username, user.avatar_color, 167), pagination=pagination_ret, page_url=self.request.path) else: self.write_error(404)
def follow(self, related_type, relate_id): related_type = int(related_type) # 1. 首先需要登录 if not self.current_user(): return -1, '请先登录后再做此操作' # 2. 父级类型必须存在 if related_type not in OBJECT_TYPES.values(): return -2, '不合法的提交' # 3. relate_id 存在检查 obj = OBJECT_TYPES.get_object(related_type, relate_id) if not obj: return -3, '目标不存在' # 4. 关注存在检查 if Follow.exists(related_type, relate_id, self.current_user()): return -4, '已经完成过这个操作' # 5. 存入数据库 Follow.new(related_type, relate_id, self.current_user()) return 0, '成功'
def get(self): id = self.request.GET["id"] user = ndb.Key(urlsafe=id).get() follows = Follow.query(Follow.username == user.username) noReadMsg = Notification.query(Notification.user == user.username, Notification.read == 0) for i in noReadMsg: if i.read == 0: noReadMsg = 0 break creaks = [] if follows.count() != 0: for i in follows: if i.username == user.username: creaks.append(i.usernameToFollow) user_creaks = Creak.query( ndb.OR(Creak.user == user.username, Creak.user.IN(creaks))).order(-Creak.time) for n in user_creaks: i = 0 t = "" for c in n.creak: t += c if i == 54: t += c + "\n" i = 0 i += 1 n.creak = t likes = [] like = Like.query(Like.iduser == user.username) for j in like: likes.append(j.idcreak) values = { "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "id": id, "user_creaks": user_creaks, "like": likes, "noReadMsg": noReadMsg } else: user_creaks = Creak.query( Creak.user == user.username).order(-Creak.time) for n in user_creaks: i = 0 t = "" for c in n.creak: t += c if i == 54: t += c + "\n" i = 0 i += 1 n.creak = t likes = [] like = Like.query(Like.iduser == user.username) for j in like: likes.append(j.idcreak) values = { "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "id": id, "user_creaks": user_creaks, "like": likes, "noReadMsg": noReadMsg } jinja = jinja2.get_jinja2(app=self.app) self.response.write(jinja.render_template("welcome.html", **values)) return
def get(self): search = self.request.get("search", "").strip() id = self.request.GET["id"] user = ndb.Key(urlsafe=id).get() noReadMsg = Notification.query(Notification.user == user.username, Notification.read == 0) if len(search) == 0: self.response.write("User to search can not be null.") return users = Register.query(Register.username == search) if users.count() == 0: user_creaks = Creak.query( Creak.user == user.username).order(-Creak.time) for n in user_creaks: i = 0 t = "" for c in n.creak: t += c if i == 54: t += c + "\n" i = 0 i += 1 n.creak = t values = { "error_login": "******", "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "id": id, "user_creaks": user_creaks, "noReadMsg": noReadMsg } jinja = jinja2.get_jinja2(app=self.app) self.response.write(jinja.render_template("welcome.html", **values)) return else: for i in users: users = Register(username=i.username, name=i.name, surname=i.surname, email=i.email, creaks=i.creaks, follow=i.follow, followers=i.followers) user_creaks = Creak.query( Creak.user == users.username).order(-Creak.time) for n in user_creaks: i = 0 t = "" for c in n.creak: t += c if i == 54: t += c + "\n" i = 0 i += 1 n.creak = t follow = Follow.query( ndb.AND(Follow.username == user.username, Follow.usernameToFollow == users.username)) likes = [] like = Like.query(Like.iduser == user.username) for j in like: likes.append(j.idcreak) if follow.count() != 0: values = { "id": id, "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "usernameSearch": users.username, "nameSearch": users.name, "surnameSearch": users.surname, "creaksSearch": users.creaks, "followSearch": users.follow, "followersSearch": users.followers, "user_creaks": user_creaks, "followed": "followed", "like": likes, "noReadMsg": noReadMsg } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("viewuser.html", **values)) return else: values = { "id": id, "username": user.username, "name": user.name, "surname": user.surname, "creaks": user.creaks, "follow": user.follow, "followers": user.followers, "usernameSearch": users.username, "nameSearch": users.name, "surnameSearch": users.surname, "creaksSearch": users.creaks, "followSearch": users.follow, "followersSearch": users.followers, "user_creaks": user_creaks, "noReadMsg": noReadMsg } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("viewuser.html", **values)) return