def follow(self, follow_class_name=None, whom_id=None, topic_id=None, node_id=None): self.active = int(time.time()) if follow_class_name: follow_class = m.FollowClass.get(name=follow_class_name, user_id=self.id) if not follow_class: follow_class = m.FollowClass(user_id=self.id, name=follow_class_name) follow_class.save() follow_class_id = follow_class.id else: follow_class_id = None follow = m.Follow.get(who_id=self.id, whom_id=whom_id, topic_id=topic_id, node_id=node_id) if not follow: follow = m.Follow(who_id=self.id, whom_id=whom_id, topic_id=topic_id, node_id=node_id, follow_class_id=follow_class_id) follow.save() return {'status': 'success', 'message': '关注成功', 'type': 1} else: follow.remove() return {'status': 'success', 'message': '取消关注成功', 'type': 0}
def follower(): # receive values from javascript follower = flask.request.form['follower'] user = flask.request.form['user'] # check to see if auth user / csrf token are valid if 'auth_user' not in flask.session: flask.abort(403) if flask.request.form['_csrf_token'] != flask.session['csrf_token']: flask.abort(400) has_relation = models.Follow.query.filter_by(person_follow=follower, user=user).first() # if the realation exists.. we don't want another. if has_relation is not None: return flask.jsonify({'result': 'already-followed'}) # if it doesn't.. we should probably make one. else: follow_relation = models.Follow() follow_relation.user = user follow_relation.person_follow = follower db.session.add(follow_relation) db.session.commit() return flask.jsonify({'result': 'ok'})
def createFakeRelationships(): # find out how many users there are. count = models.User.query.count() print('count', count) iterations = 300 for x in range(iterations): # get a random id within the range of the count user1Id = random.randint(0, count) # get another random id user2Id = random.randint(0, count) print('uid1', user1Id) print('uid2', user2Id) # check to see if theres a follow relationship between them already. follow = models.Follow.query.filter_by(follower_id=user1Id, followee_id=user2Id).first() print(follow) #if theres not make one. if follow is None: f = models.Follow(user1Id, user2Id) db.session.add(f) db.session.commit() else: print('already there')
def update_check(): print(flask.request.form) print('to answer', flask.request.form['followee_id']) if 'auth_user' not in flask.session: print('not auth user') flask.abort(403) user_id = flask.session['auth_user'] if flask.request.form['_csrf_token'] != flask.session['csrf_token']: print('aborting') flask.abort(400) #get values from request followee_id = int(flask.request.form['followee_id']) want_to_follow = flask.request.form['want_to_follow'] nextState = flask.request.form['state'] if want_to_follow == 'true': want_to_follow = True else: want_to_follow = False print('followee id ', followee_id) print('want to follow ', want_to_follow) print('next state ', nextState) print(type(user_id)) print(type(followee_id)) #check to see if it is already there: follow = models.Follow.query.filter_by(follower_id=user_id, followee_id=followee_id).first() print(follow) if follow is None: print('now following >>>>>>>>') follow = models.Follow(user_id, followee_id) db.session.add(follow) db.session.commit() return flask.jsonify({'result': 'ok'}) else: db.session.delete(follow) db.session.commit() print('now UN-following >>>>>>>>') return flask.jsonify({'result': 'ok'})
def request_follow(): check_request() init = int(flask.request.form['init_id']) print(init) other = int(flask.request.form['respond_id']) print(other) fs = models.Follow.query.filter_by(init_id=init, respond_id=other).first() if fs is None: fs = models.Follow() fs.init_id = init fs.respond_id = other db.session.add(fs) db.session.commit() # Following now! return flask.jsonify({'new_state': 'following'})