From 16ff1d6f5d4044b86dc84a2ca771a15bc09e374f Mon Sep 17 00:00:00 2001 From: Yuan Diao Date: Tue, 2 Jun 2020 17:15:24 -0700 Subject: [PATCH 1/4] add algolia --- .gitignore | 10 ++-------- backend/.flaskenv | 2 +- backend/conduit/algolia.py | 4 ++++ backend/conduit/articles/views.py | 5 +++++ backend/conduit/extensions.py | 1 - 5 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 backend/conduit/algolia.py diff --git a/.gitignore b/.gitignore index 2b3183a..7f1db4f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,16 +26,10 @@ venv npm-debug.log* yarn-debug.log* yarn-error.log* -backend/migrations/versions/f539a7ad15a4_.py -backend/migrations/alembic.ini -backend/migrations/env.py -backend/migrations/script.py.mako -backend/migrations/versions/22cd0f8da398_.py +backend/migrations/ .gitignore -backend/migrations/versions/bb10324d2013_.py backend/.bashrc -backend/migrations/versions/f539a7ad15a4_.py backend/.vscode/settings.json .vscode/settings.json -backend/migrations/versions/f539a7ad15a4_.py .gitignore +backend/.flaskenv diff --git a/backend/.flaskenv b/backend/.flaskenv index f98770d..49c357b 100644 --- a/backend/.flaskenv +++ b/backend/.flaskenv @@ -1,3 +1,3 @@ export FLASK_APP=autoapp.py export FLASK_DEBUG=1 -export DATABASE_URL="postgres://iscjpxes:9nSz-bOz1Z-4y170A87M12ZcYbMyR6xm@ruby.db.elephantsql.com:5432/iscjpxes" +export DATABASE_URL="postgres://yhxpupao:qOmdC-HQGC8vX-K_T3T5HF9DWwAU1R5s@ruby.db.elephantsql.com:5432/yhxpupao" diff --git a/backend/conduit/algolia.py b/backend/conduit/algolia.py new file mode 100644 index 0000000..072fb65 --- /dev/null +++ b/backend/conduit/algolia.py @@ -0,0 +1,4 @@ +from algoliasearch.search_client import SearchClient + +client = SearchClient.create('ULTKUOSOMR', '46d33df53184cac090b85a305f2948a3') +articleIndex = client.init_index('articles') \ No newline at end of file diff --git a/backend/conduit/articles/views.py b/backend/conduit/articles/views.py index 124ce76..64c762a 100644 --- a/backend/conduit/articles/views.py +++ b/backend/conduit/articles/views.py @@ -12,6 +12,8 @@ from .models import Article, Tags, Comment from .serializers import (article_schema, articles_schema, comment_schema, comments_schema) +from conduit.algolia import articleIndex + blueprint = Blueprint('articles', __name__) @@ -66,6 +68,9 @@ def make_article(body, title, description, isPublished, tagList=None): else: # mtag.modSetting == 1: article.add_tag(mtag) article.save() + # articleObject = article_schema.dump(article) + # articleObject.objectID = article.id + # articleIndex.save_object(articleObject) return article diff --git a/backend/conduit/extensions.py b/backend/conduit/extensions.py index ed903b1..ab9089b 100644 --- a/backend/conduit/extensions.py +++ b/backend/conduit/extensions.py @@ -8,7 +8,6 @@ from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy, Model - class CRUDMixin(Model): """Mixin that adds convenience methods for CRUD (create, read, update, delete) operations.""" From 7122254bb65c2344a901f6a4f2672d61b7f3654d Mon Sep 17 00:00:00 2001 From: Yuan Diao Date: Tue, 2 Jun 2020 20:12:37 -0700 Subject: [PATCH 2/4] bug fix --- backend/conduit/articles/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/conduit/articles/serializers.py b/backend/conduit/articles/serializers.py index e33107f..0cfdb95 100644 --- a/backend/conduit/articles/serializers.py +++ b/backend/conduit/articles/serializers.py @@ -24,7 +24,7 @@ class ArticleSchema(Schema): # for the envelope article = fields.Nested('self', exclude=('article',), default=True, load_only=True) tagList = fields.Nested(TagSchema, many=True) - favoritesCount = fields.Int(dump_only=True) + tagList = fields.List(fields.Str()) commentsCount = fields.Int(dump_only=True) favorited = fields.Bool(dump_only=True) isPublished = fields.Bool() From c9e9ecd9588271b673f271bfe3b0e2d1d1cf5f7e Mon Sep 17 00:00:00 2001 From: Yuan Diao Date: Fri, 5 Jun 2020 21:11:16 -0700 Subject: [PATCH 3/4] Integrated Algolia --- backend/conduit/algolia.py | 13 +++++++++++-- backend/conduit/articles/views.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/backend/conduit/algolia.py b/backend/conduit/algolia.py index 072fb65..df31f91 100644 --- a/backend/conduit/algolia.py +++ b/backend/conduit/algolia.py @@ -1,4 +1,13 @@ from algoliasearch.search_client import SearchClient +import os +from dotenv import load_dotenv +from pathlib import Path # Python 3.6+ only -client = SearchClient.create('ULTKUOSOMR', '46d33df53184cac090b85a305f2948a3') -articleIndex = client.init_index('articles') \ No newline at end of file +env_path = Path('.') / '.env' +load_dotenv(dotenv_path=env_path) + +APPLICATION_ID = os.getenv('APPLICATION_ID') +ADMIN_API_KEY = os.getenv('ADMIN_API_KEY') + +client = SearchClient.create(APPLICATION_ID, ADMIN_API_KEY) +articleIndex = client.init_index('dev_articles') diff --git a/backend/conduit/articles/views.py b/backend/conduit/articles/views.py index 7fe2d93..69714b7 100644 --- a/backend/conduit/articles/views.py +++ b/backend/conduit/articles/views.py @@ -14,7 +14,6 @@ comments_schema) from conduit.algolia import articleIndex - blueprint = Blueprint('articles', __name__) @@ -66,12 +65,12 @@ def make_article(body, title, description, isPublished, tagList=None): article.add_needReviewTag(mtag) article.add_tag(mtag) article.needsReview = True - else: # mtag.modSetting == 1: + else: # mtag.modSetting == 1: article.add_tag(mtag) article.save() - # articleObject = article_schema.dump(article) - # articleObject.objectID = article.id - # articleIndex.save_object(articleObject) + articleObject = article_schema.dump(article) + articleObject['objectID'] = article.id + articleIndex.save_object(articleObject) return article @@ -85,6 +84,9 @@ def update_article(slug, **kwargs): raise InvalidUsage.article_not_found() article.update(updatedAt=dt.datetime.utcnow(), **kwargs) article.save() + articleObject = article_schema.dump(article) + articleObject['objectID'] = article.id + articleIndex.save_object(articleObject) return article @@ -92,7 +94,9 @@ def update_article(slug, **kwargs): @jwt_required def delete_article(slug): article = Article.query.filter_by(slug=slug, author_id=current_user.profile.id).first() + article_id = article.id article.delete() + articleIndex.delete_object(str(article_id)) return '', 200 From 6ac8c5fd917de279abb36a22b98532d298c4e4ca Mon Sep 17 00:00:00 2001 From: Yuan Diao Date: Fri, 5 Jun 2020 21:13:40 -0700 Subject: [PATCH 4/4] remove redundant code --- backend/conduit/articles/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/conduit/articles/views.py b/backend/conduit/articles/views.py index 2e5d1db..73b0ca6 100644 --- a/backend/conduit/articles/views.py +++ b/backend/conduit/articles/views.py @@ -53,7 +53,6 @@ def get_articles(isPublished=None, tag=None, author=None, favorited=None, limit= def make_article(body, title, description, isPublished, coverImage, tagList=None): article = Article(title=title, description=description, body=body, author=current_user.profile, isPublished=isPublished, coverImage=coverImage) - needReviewTags = [] if tagList is not None: for tag in tagList: mtag = Tags.query.filter_by(tagname=tag).first()