def check_user_logged_in(): if session.get('user'): g.user = session.get('user') return result = get_user_from_cookie(cookies=request.cookies, app_id=app.config["FB_APP_ID"], app_secret=app.config["FB_APP_SECRET"]) if result: user = UserActions.find_by_id(result['uid']) if not user: graph = GraphAPI(result['access_token']) args = {'fields': 'birthday, name, email'} profile = graph.get_object('me', **args); UserActions.new_facebook_user(profile, result) elif user.access_token != result['access_token']: user.access_token = result['access_token'] user = UserActions.find_by_id(result['uid']) session['user'] = dict(name=user.name, profile_url=user.profile_url, id=user.id, access_token=user.access_token) db.session.commit() g.user = session.get('user', None)
def check_user_logged_in(): if session.get('user'): g.user = session.get('user') return result = get_user_from_cookie(cookies=request.cookies, app_id=app.config["FB_APP_ID"], app_secret=app.config["FB_APP_SECRET"]) if result: user = UserActions.find_by_id(result['uid']) if not user: graph = GraphAPI(result['access_token']) args = {'fields': 'birthday, name, email'} profile = graph.get_object('me', **args) UserActions.new_facebook_user(profile, result) elif user.access_token != result['access_token']: user.access_token = result['access_token'] user = UserActions.find_by_id(result['uid']) session['user'] = dict(name=user.name, profile_url=user.profile_url, id=user.id, access_token=user.access_token) db.session.commit() g.user = session.get('user', None)
def amazon_fetch_product(friend_id): user = UserActions.find_by_id(g.user['id']) friend = UserActions.find_by_id(friend_id) task = amazon_task.get_product.apply_async([friend]) return jsonify(**{"data": {'task_id': task.id}})
def amazon_fetch_product(friend_id): user = UserActions.find_by_id(g.user['id']) friend = UserActions.find_by_id(friend_id) task = amazon_task.get_product.apply_async([friend]) return jsonify(**{ "data": { 'task_id': task.id } })
def contact_us(): user = UserActions.find_by_id(g.user['id']) if request.method == 'POST': user = UserActions.find_by_id(g.user['id']) subject=comment.subject.data, feedback=comment.message.data, CommentActions.create(user, subject, feedback) return render_template('contact_us.html', success=True, user=user) elif request.method == 'GET': return render_template("contact_us.html", user=user)
def contact_us(): user = UserActions.find_by_id(g.user['id']) if request.method == 'POST': user = UserActions.find_by_id(g.user['id']) subject = comment.subject.data, feedback = comment.message.data, CommentActions.create(user, subject, feedback) return render_template('contact_us.html', success=True, user=user) elif request.method == 'GET': return render_template("contact_us.html", user=user)
def taskstatus(task_id): task = amazon_task.get_product.AsyncResult(task_id) if task.status == "PROGRESS": response = { 'state': task.state, 'task_id': task_id, 'current': task.info.get('current', 0), 'total': task.info.get('total', 0), 'status': 'Pending...' } friend = UserActions.find_by_id(task.info.get('user_id')) products = [] user_products = UserProductActions.find_by_user(friend) result_to_json = Serializer("UserProduct", user_products).run() for user_product in result_to_json: product = redis.get(user_product['product_id']) product_dict = xmltodict.parse(product) user_product['product_details'] = product_dict response['data'] = result_to_json elif task.status == 'SUCCESS': response = { 'state': task.state, 'task_id': task_id, 'current': 3, 'total': 3, 'status': task.status, } friend = UserActions.find_by_id(task.info.get('user_id')) products = [] user_products = UserProductActions.find_by_user(friend) result_to_json = Serializer("UserProduct", user_products).run() for user_product in result_to_json: product = redis.get(user_product['product_id']) product_dict = xmltodict.parse(product) user_product['product_details'] = product_dict response['data'] = result_to_json else: response = { 'state': task.state, 'task_id': task_id, 'current': 0, 'total': 0, 'status': 'Pending...' } return json.dumps(response)
def taskstatus(task_id): task = amazon_task.get_product.AsyncResult(task_id) if task.status == "PROGRESS": response = { 'state': task.state, 'task_id': task_id, 'current': task.info.get('current', 0), 'total': task.info.get('total', 0), 'status': 'Pending...' } friend = UserActions.find_by_id(task.info.get('user_id')) products = [] user_products = UserProductActions.find_by_user(friend) result_to_json = Serializer("UserProduct", user_products).run() for user_product in result_to_json: product = redis.get(user_product['product_id']) product_dict = xmltodict.parse(product) user_product['product_details']= product_dict response['data'] = result_to_json elif task.status == 'SUCCESS': response = { 'state': task.state, 'task_id': task_id, 'current': 3, 'total': 3, 'status': task.status, } friend = UserActions.find_by_id(task.info.get('user_id')) products = [] user_products = UserProductActions.find_by_user(friend) result_to_json = Serializer("UserProduct", user_products).run() for user_product in result_to_json: product = redis.get(user_product['product_id']) product_dict = xmltodict.parse(product) user_product['product_details']= product_dict response['data'] = result_to_json else: response = { 'state': task.state, 'task_id': task_id, 'current': 0, 'total': 0, 'status': 'Pending...' } return json.dumps(response)
def user_feedback(): user = UserActions.find_by_id(g.user['id']) form = ContactForm() if request.method == 'POST': user = UserActions.find_by_id(g.user['id']) subject = form.subject.data, feedback = form.message.data, CommentActions.create(user, subject, feedback) return render_template('user_feedback.html', success=True, user=user) elif request.method == 'GET': return render_template("user_feedback.html", form=form, user=user)
def user_feedback(): user = UserActions.find_by_id(g.user['id']) form = ContactForm() if request.method == 'POST': user = UserActions.find_by_id(g.user['id']) subject=form.subject.data, feedback=form.message.data, CommentActions.create(user, subject, feedback) return render_template('user_feedback.html', success=True, user=user) elif request.method == 'GET': return render_template("user_feedback.html", form=form, user=user)
def index(name="index", *args, **kawrgs): if request.is_xhr: return "", 400 if g.user: # try: graph = GraphAPI(g.user['access_token']) args = {'fields': 'birthday, name, email'} facebook_friends = graph.get_object('me/friends', **args) user = UserActions.find_by_id(g.user['id']) for facebook_friend in facebook_friends['data']: friend = UserActions.new(facebook_friend) FriendRelationshipActions.create(user, friend) relations = FriendRelationshipActions.find_by_user(user) return render_template("index.html", app_id=app.config["FB_APP_ID"], user=user, relations=relations) # except Exception: # return redirect(url_for('logout')) return render_template("login.html", app_id=app.config["FB_APP_ID"])
def run(self): print("Get friends datas from facebook") user = UserActions.find_by_id('118600698523150') graph = GraphAPI(user.access_token) args = {'fields' : 'birthday, name, email'} friends = graph.get_object('me/friends', **args) for friend in friends['data']: UserActions.create(friend) posts = graph.get_connections(friend['id'], 'posts') for post in posts['data']: post = PostActions.create(post, friend) print(post) pass
def run(self): print("Get friends datas from facebook") user = UserActions.find_by_id('118600698523150') graph = GraphAPI(user.access_token) args = {'fields': 'birthday, name, email'} friends = graph.get_object('me/friends', **args) for friend in friends['data']: UserActions.create(friend) posts = graph.get_connections(friend['id'], 'posts') for post in posts['data']: post = PostActions.create(post, friend) print(post) pass
def api_request(entity, id=None): if g.user: try: # args = request.args.lists() user = UserActions.find_by_id(g.user['id']) repository = ActionsFactory.get_repository(entity) result = repository.filter(user, id=id) result_to_json = Serializer(entity, result).run() if entity == "UserProduct": for elt in result_to_json: product = redis.get(elt['product_id']) product_dict = xmltodict.parse(product) elt['product_details'] = product_dict return jsonify(**{"data": result_to_json}) except Exception: return "Request invalid", 500 else: return "not connected"
def index(name="index", *args, **kawrgs): if request.is_xhr: return "", 400 if g.user: # try: graph = GraphAPI(g.user['access_token']) args = {'fields': 'birthday, name, email'} facebook_friends = graph.get_object('me/friends', **args); user = UserActions.find_by_id(g.user['id']) for facebook_friend in facebook_friends['data']: friend = UserActions.new(facebook_friend) FriendRelationshipActions.create(user, friend) relations = FriendRelationshipActions.find_by_user(user) return render_template("index.html", app_id=app.config["FB_APP_ID"], user=user, relations=relations) # except Exception: # return redirect(url_for('logout')) return render_template("login.html", app_id=app.config["FB_APP_ID"])
def api_request(entity, id=None): if g.user: try: # args = request.args.lists() user = UserActions.find_by_id(g.user['id']) repository = ActionsFactory.get_repository(entity) result = repository.filter(user, id=id) result_to_json = Serializer(entity, result).run() if entity == "UserProduct": for elt in result_to_json: product = redis.get(elt['product_id']) product_dict = xmltodict.parse(product) elt['product_details'] = product_dict return jsonify(**{ "data": result_to_json }) except Exception: return "Request invalid", 500 else: return "not connected"
def get_facebook_friends_data(): user = UserActions.find_by_id(g.user['id']) facebook_task.get_friends.delay(user) return redirect(url_for('index'))