Exemple #1
0
def create_Article(jwt):
    json = request.get_json()
    print(f" json arrived is {json}")
    title = json['title']
    image = json['image']
    category = json['category'].lower()
    article = json['contet']
    is_found = Category.query.filter_by(category_name=category).all()
    if len(is_found) > 0:
        print(f" stored before {is_found}")

    items = Category.query.filter_by(category_name=category).all()
    print(f"Length of categories {len(items)}")
    if (len(items) > 0):
        print(f" category  {items[0].id}")
        article_item = Article(title=title,
                               image_url=image,
                               category_id=items[len(items) - 1].id,
                               content=article,
                               likes=0)
        article_item.insert()
        return jsonify({"success": True, "article": "article_item"})

    category_item = Category(category_name=category.lower())

    category_item.flush()
    article_item = Article(title=title,
                           image_url=image,
                           category_id=category_item.id,
                           content=article,
                           likes=0)
    category_item.insert()
    article_item.insert()
    print("POSTED ")
    return jsonify({"success": True, "article": "article_item"})
Exemple #2
0
def load_article():
    payload = request.data

    title = payload['title']
    image_url = payload['image_url']
    title_link = payload['title_link']
    category = payload['category']
    meta_content = payload['meta_content']

    new_article = Article(title=title, image_url=image_url, title_link=title_link,
                          category=category, meta_content=meta_content)

    try:
        Database_Session.add(new_article)
        Database_Session.commit()

        return success_message(body=new_article, msg='Loaded: {}'.format(new_article))

    except:
        Database_Session.rollback()

    finally:
        Database_Session.close()

        return error_message('Could not load {}'.format(new_article))
Exemple #3
0
def add_get_article():
    if request.method == 'POST':
        body = request.get_json()
        try:
            author = Author(name=body['author']).save()
        except NotUniqueError:
            author = Author.objects(name=body['author']).get()

        article = Article(title=body['title'],
                          text=body['text'],
                          author=author).save()

        id = article.id
        return {'id': str(id)}, 200

    if request.method == 'GET':
        articles = Article.objects().to_json()
        return Response(articles, mimetype="application/json", status=200)
Exemple #4
0
def text(request):
    name_list = Article.objects.order_by('-id')
    context = {'name_list': name_list}
    if request.method == 'POST':
        content = request.POST.get('content')
        try:
            email = request.session['email']
            # select * from user where email = ?
            user = User.objects.get(email=email)
            # insert into article (title, content, user_id) values (?, ?, ?)
            article = Article(content=content, user=user)
            article.save()
            return render(request, 'write_success.html')
            # return HttpResponseRedirect('/text/')

        except:
            return render(request, 'write_fail.html')
    return render(request, 'text.html', context)
Exemple #5
0
def add_user(chat_id, articles, session):
    if session.query(exists().where(User.id == chat_id)).scalar():
        session.query(Article).filter_by(user_id=chat_id).delete()
        user = session.query(User).get(chat_id)
        user.article_no = -1
    else:
        user = User(chat_id)
        session.add(user)
    for i, article in enumerate(articles):
        insertion = Article(chat_id, i, article['title'],
                            ', '.join(article['authors']), article['date'],
                            '\n'.join(article['urls']))
        session.add(insertion)
Exemple #6
0
def update_get_article(id):
    if request.method == 'PUT':
        body = request.get_json()
        try:
            author = Author(name=body['author']).save()
        except NotUniqueError:
            author = Author.objects(name=body['author']).get()

        article = Article(title=body['title'],
                          text=body['text'],
                          author=author).save()

        id = article.id
        return {'id': str(id)}, 200

    if request.method == 'DELETE':
        # delete author who has no articles?
        Article.objects.get(id=id).delete()
        return '', 200

    if request.method == 'GET':
        articles = Article.objects.get(id=id).to_json()
        return Response(articles, mimetype="application/json", status=200)
Exemple #7
0
def add_movie():
    body = request.get_json()
    article = Article(
        **body).save()  #collection = Class(**body).save() <-- syntax samajh lo
    id = article.id
    return 'article add ho gayi', 200
Exemple #8
0
def get_movies():
    articles = Article.objects().to_json()
    return Response(articles, mimetype="application/json",
                    status=200)  #prints whatever is present in that colection