def delete_comment():
    response = {
        'status': True,
        'message': ''
    }
    if not 'token' in request.form or not 'comment_id' in request.form:
        response['message'] = 'bad request'
    else:
        token = request.form['token']
        comment_id = int(request.form['comment_id'])
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            comment = Comment.query.filter_by(id=comment_id,
                                              commenter_id=user.id)
            if comment.count() == 0:
                response['message'] = 'comment_id invalid'
            else:
                comment.update({Comment.status: 'excluded'})
                db.session.commit()
                response['message'] = 'ok'
                response['status'] = True
    return (json.dumps(response))
Beispiel #2
0
def add_access():
    response = {
        'status': False,
        'message': ''
    }
    if not 'token' in request.form or not 'device' in request.form:
        response['message'] = 'bad request'
    else:
        token = request.form['token']
        device = request.form['device']
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            currentTime = time.time()
            access = Access.query.filter(
                Access.user_id == user.id,
                Access.datetime.between(currentTime - 86400,
                                        currentTime + 86400))
            if access.count() == 0:
                response['message'] = 'ok'
                response['status'] = True
                new_access = Access(user_id=user.id,
                                    ip=request.remote_addr,
                                    user_agent=device,
                                    datetime=currentTime)
                db.session.add(new_access)
                db.session.commit()
            else:
                response['message'] = 'already registed today'
    return (json.dumps(response))
Beispiel #3
0
def edit_story():
    response = {'status': False, 'message': ''}
    if not 'token' in request.form or \
     not 'id' in request.form or \
     not 'title' in request.form or \
     not 'text' in request.form or \
     not 'tags' in request.form:
        response['message'] = 'bad request'
    else:
        token = request.form['token']
        story_id = int(request.form['id'])
        title = request.form['title']
        text = request.form['text']
        tags = request.form['tags']
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            Story.query.filter_by(id=story_id, publisher_id=user.id) \
             .update({Story.title: title, Story.text:text, Story.tags: tags,Story.preview:text[:preview_size]})
            db.session.commit()
            response['status'] = True
            response['message'] = 'story edited'
    return (json.dumps(response))
Beispiel #4
0
def get_sketch(token=None):
    response = {'status': False, 'message': ''}

    if token == None:
        response['message'] = 'bad request'
    else:
        is_signed = verify_login(token)

        if is_signed == False:
            response['message'] = 'not authentificated'
        else:
            user = get_user_by_token(token)
            story = Story.query.filter_by(publisher_id=user.id, status='in_sketch') \
             .with_entities(Story.id, Story.title, Story.text).first()

            if story == None:
                response['message'] = 'none sketch created yet'
            else:
                response['status'] = True
                response['message'] = 'ok'
                response['payload'] = {
                    "id": story.id,
                    "title": story.title,
                    "text": story.text
                }

    return (json.dumps(response))
def get_story(token=None, story_id=None):
    response = {'status': False, 'message': ''}
    if token == None or id == None:
        response['message'] = 'bad request'
    else:
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            sql_query = "SELECT story.*,\
				(SELECT COUNT(comment.id) from comment WHERE comment.story_id LIKE story.id) as 'comments',\
				(SELECT COUNT(view.story_id) from view WHERE view.story_id LIKE story.id) as 'views',\
				(SELECT COUNT(enjoy.story_id) from enjoy WHERE enjoy.story_id LIKE story.id) as 'enjoys'\
				FROM story WHERE story.id LIKE {0} and story.status LIKE 'published'\
				GROUP BY story.id LIMIT 1".format(story_id)

            result = db.session.execute(sql_query)
            story = result.fetchone()

            is_liked_by_user = Enjoy.query.filter_by(story_id=story_id,
                                                     user_id=user.id).count()
            is_liked_by_user = True if is_liked_by_user == 1 else False

            if story == None:
                response['message'] = 'story not found'
            else:

                viewscount = View.query.filter_by(story_id=story_id,
                                                  user_id=user.id).count()
                if viewscount == 0:
                    new_view = View(user_id=user.id,
                                    story_id=story[0],
                                    duration=0,
                                    datetime=time.time())
                    db.session.add(new_view)
                    db.session.commit()

                response['payload'] = {\
                 "id": story[0],\
                 "title": story[1],\
                 "text": story[2],\
                 "preview": story[3],\
                 "tags": story[4],\
                 "is_liked_by_user": is_liked_by_user,
                 "datetime_created": story[5],\
                 "publisher_id": story[6],\
                 "views": story.views,\
                 "enjoys": story.enjoys,\
                 "comments":story.comments}

                response['status'] = True
                response['message'] = 'ok'
    return (json.dumps(response))
Beispiel #6
0
def save_sketch():
    response = {'status': False, 'message': ''}

    if not 'token' in request.form or not 'text' in request.form:
        response['message'] = 'bad request'
    else:
        text = request.form['text']
        token = request.form['token']
        is_signed = verify_login(token)

        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            story = Story.query.filter_by(publisher_id=user.id,
                                          status='in_sketch').with_entities(
                                              Story.id, Story.title,
                                              Story.text).first()
            response['status'] = True
            if story == None:
                timestamp_now = str(time.time())
                new_story = Story(id=False,
                                  title="",
                                  text=text,
                                  preview=text[:preview_size],
                                  tags="",
                                  datetime_created=timestamp_now,
                                  last_update=timestamp_now,
                                  locale=user.locale,
                                  status="in_sketch",
                                  publisher_id=user.id)
                db.session.add(new_story)
                response['message'] = 'story created'
            else:
                db.session.query(Story).filter(Story.id == story.id).update({
                    Story.text:
                    text,
                    Story.preview:
                    text[:preview_size]
                })
                response['message'] = 'story saved'
            db.session.commit()

            last_story = Story.query.filter_by(
                publisher_id=user.id,
                status='in_sketch').with_entities(Story.id).first()
            response["id"] = last_story.id

    return (json.dumps(response))
Beispiel #7
0
def enjoy_story():
    response = {'status': False, 'message': ''}
    if not 'token' in request.form or not 'story_id' in request.form:
        response['message'] = 'bad request'
    else:
        token = request.form['token']
        story_id = int(request.form['story_id'])
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            story = Story.query.filter_by(id=story_id)
            if story.count() == 0:
                response['message'] = 'story not found'
            else:
                story = story.first()
                user = get_user_by_token(token)
                has_already_enjoyed_post = Enjoy.query.filter_by(
                    story_id=story_id, user_id=user.id).count()
                if has_already_enjoyed_post == 1:
                    enjoy = Enjoy.query.filter_by(user_id=user.id,
                                                  story_id=story_id).delete()
                    response['message'] = 'enjoy removed'
                else:
                    enjoy = Enjoy(story_id=story_id,
                                  user_id=user.id,
                                  datetime=str(time.time()))
                    db.session.add(enjoy)
                    response['message'] = 'enjoy added'

                    publisher = User.query.filter_by(
                        id=story.publisher_id).first()
                    if publisher.push_token != None and publisher.id != user.id:
                        header = {"en": "YourStories"}
                        content = {
                            "en": "{0} liked your post".format(user.full_name)
                        }
                        users = [publisher.push_token]
                        data = {'story_id': story.id}
                        notify_android_users(content, header, users, data)

                db.session.commit()
                response['status'] = True
    return (json.dumps(response))
def get_stories(token=None, page=None):
	response = {'status': False, 'message': ''}
	
	if token == None or page == None:
		response['message'] = 'bad request'
	else:
		page = int(page)
		is_signed = verify_login(token)

		if is_signed == False:
			response['message'] = 'not authorized'
		else:
			user = get_user_by_token(token)
			fetch = Story.query.filter(Story.status.like('published'), Story.locale.like('%'+user.locale[:2]+'%')) \
				.with_entities(Story.id, Story.title, Story.preview) \
				.order_by(Story.datetime_created.desc()) \
				.paginate(per_page=page_size, page=page, error_out=False)

			if len(fetch.items) == 0:
				fetch = Story.query.filter(Story.status.like('published'), Story.locale.like('en')) \
				.with_entities(Story.id, Story.title, Story.preview) \
				.order_by(Story.datetime_created.desc()) \
				.paginate(per_page=page_size, page=page, error_out=False)

			data = []

			
			for item in fetch.items:
				data.append({'id': item[0], 'title':item[1], 'preview':item[2]})
			if len(data) > 0:
				response['last_page'] = True if page_size > len(data) else False
				response['status'] = True
				response['message'] = 'ok'
				response['size'] = len(data)
				response['payload'] = data
			else:
				response['status'] = True
				response['message'] = 'ok'
				response['size'] = 0
			

	return(json.dumps(response))
def get_my_stories(token=None, page=None):
	response = {'status': False, 'message': ''}
	
	if token == None:
		response['message'] = 'bad request'
	else:
		page = 1 if page == None else int(page)
		is_signed = verify_login(token)

		if is_signed == False:
			response['message'] = 'not authorized'
		else:
			user = get_user_by_token(token)
			fetch = Story.query.filter_by(status='published', publisher_id=user.id) \
				.with_entities(Story.id, Story.title, Story.preview) \
				.order_by(Story.datetime_created.desc()) \
				.paginate(per_page=page_size, page=page, error_out=False) \

			data = []

			for item in fetch.items:
				views = View.query.filter_by(story_id=item[0]).count()
				comments = Comment.query.filter_by(story_id=item[0]).count()
				enjoys = Enjoy.query.filter_by(story_id=item[0]).count()
				data.append({\
					'id': item[0],\
					'title':item[1],\
					'preview': item[2],\
					'comments':comments,\
					'enjoys': enjoys,\
					'views': views})

			response['status'] = True
			response['message'] = 'ok'
			response['last_page'] = True if page_size > len(fetch.items) else False
			response['size'] = len(data)
			response['payload'] = data

	return(json.dumps(response))
def add_comment():
	response = {'status': False, 'message': ''};
	if not 'token' in request.form\
		or not 'story_id' in request.form\
		or not 'comment' in request.form:
			response['message'] = 'bad request'
	else:
		token = request.form['token']
		story_id = int(request.form['story_id'])
		comment = request.form['comment']
		is_signed = verify_login(token)

		if is_signed == False:
			response['message'] = 'not authorized'
		else:
			user = get_user_by_token(token)
			story = Story.query.filter_by(id=story_id)
			if story.count() == 0:
				response['message'] = 'invalid story id'
			else:
				story = story.first()
				current_time = time.time()
				new_comment = Comment(commenter_id=user.id, story_id=story_id, comment=comment, datetime=current_time, status='published')
				db.session.add(new_comment)
				db.session.commit()

				publisher = User.query.filter_by(id=story.publisher_id).first()
				if publisher != None:
					if publisher.push_token != None and publisher.id != user.id:
						header = {"en":"YourStories"}
						content = {"en": "{0} commented on your post".format(user.full_name)}
						users = [publisher.push_token]
						data = {'story_id': story.id}
						notify_android_users(content, header, users, data)

				response['status'] = True
				response['message'] = 'ok'
	return(json.dumps(response))
Beispiel #11
0
def delete_story():
    response = {
        'status': True,
        'message': ''
    }
    if not 'token' in request.form or not 'story_id' in request.form:
        response['message'] = 'bad request'
    else:
        token = request.form['token']
        story_id = int(request.form['story_id'])
        is_signed = verify_login(token)
        if is_signed == False:
            response['message'] = 'not authorized'
        else:
            user = get_user_by_token(token)
            story = Story.query.filter_by(id=story_id, publisher_id=user.id)
            if story.count() == 0:
                response['message'] = 'story_id invalid'
            else:
                story.update({Story.status: 'excluded'})
                db.session.commit()
                response['message'] = 'ok'
                response['status'] = True
    return (json.dumps(response))