Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix db #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.vscode
.vscode
venv
.idea
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/twanalyze.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions backend/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ def create_app(test_config=None):
root = logging.getLogger("core")
root.addHandler(strm)

# user = os.environ.get("MONGO_USER")
# password = os.environ.get("MONGO_PASSWORD")
# db = os.environ.get("MONGO_DB")
# host = os.environ.get("MONGO_HOST")
# app.config["MONGODB_SETTINGS"] = {
# "db": db,
# "host": host % (user, password, db),
# }
user = os.environ.get("MONGO_USER")
password = os.environ.get("MONGO_PASSWORD")
db = os.environ.get("MONGO_DB")
host = os.environ.get("MONGO_HOST")
app.config["MONGODB_SETTINGS"] = {"db": db, "host": host % (user, password, db)}

# register mongoengine to this app
# from api.models import db
from api.models import db

# db.init_app(app) # initialize Flask MongoEngine with this flask app
# Migrate(app, db)
db.init_app(app) # initialize Flask MongoEngine with this flask app
Migrate(app, db)

# import and register blueprints
from api.views import main, auth
Expand Down
33 changes: 30 additions & 3 deletions backend/api/models/User.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
from api.core import Mixin
from .base import db
from api.models import Email
from flask_mongoengine import Document
from mongoengine import *


class User(Document, Mixin):
"""User Collection."""

userId = StringField(required=True)
class Tweet(EmbeddedDocument, Mixin):

class Sentiment(EmbeddedDocument, Mixin):
score = FloatField(required=True)
magnitude = FloatField(required=True)

class ReplySentiment(EmbeddedDocument, Mixin):
score = FloatField(required=True)
magnitude = FloatField(required=True)

class Reply(EmbeddedDocument, Mixin):
name = StringField(required=True)
username = StringField(required=True)
text = StringField(required=True)
likes = IntField(required=True)
retweets = IntField(required=True)
time_created = StringField(required=True)

text = StringField(required=True)
likes = IntField(required=True)
retweets = IntField(required=True)
time_created = StringField(required=True)
sentiment = EmbeddedDocumentField(Sentiment)
reply_sentiment = EmbeddedDocumentField(ReplySentiment)
replies = ListField(EmbeddedDocumentField(Reply))

name = StringField(required=True)
username = StringField(required=True)
profile_picture = StringField(required=True)
tweets = ListField(EmbeddedDocumentField(Tweet))

def __repr__(self):
return f"<User id:{self.userId} \n username:{self.username}>"
return f"<User id:{self.name} \n username:{self.username}>"
3 changes: 2 additions & 1 deletion backend/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from .Email import Email
from .Person import Person
from .VideoInfo import VideoInfo
from .User import User

__all__ = ["db", "Email", "Person", "VideoInfo"]
__all__ = ["db", "Email", "Person", "VideoInfo", "User"]

# You must import all of the new Models you create to this page
15 changes: 15 additions & 0 deletions backend/api/utils/sanitize_tweet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json

def sanitize(obj):
valid_fields = {'text', 'user', 'retweet_count', 'favorite_count'}
new_obj = {}
for field in valid_fields:
if field in obj:
new_obj[field] = obj[field]
return new_obj

# test code
with open('../../tests/sample_tweet.txt') as f:
d = json.load(f)
sanitized = sanitize(d)
print(sanitized)
9 changes: 8 additions & 1 deletion backend/api/views/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Blueprint, request, jsonify
from api.models import db, Person, Email
from api.models import db, Person, User, Email
from api.core import create_response, serialize_list, logger

main = Blueprint("main", __name__) # initialize blueprint
Expand All @@ -22,6 +22,13 @@ def get_persons():
return create_response(data={"persons": persons})


# function that is called when you visit /tweets
@main.route("/tweets", methods=["GET"])
def get_tweets():
tweets = User.objects()
print(tweets)
return create_response(data={"tweets": tweets})

# POST request for /persons
@main.route("/persons", methods=["POST"])
def create_person():
Expand Down
34 changes: 34 additions & 0 deletions backend/tests/sample_tweet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"created_at": "Thu Apr 06 15:24:15 +0000 2017",
"id_str": "850006245121695744",
"text": "1\/ Today we\u2019re sharing our vision for the future of the Twitter API platform!\nhttps:\/\/t.co\/XweGngmxlP",
"user": {
"id": 2244994945,
"name": "Twitter Dev",
"screen_name": "TwitterDev",
"location": "Internet",
"url": "https:\/\/dev.twitter.com\/",
"description": "Your official source for Twitter Platform news, updates & events. Need technical help? Visit https:\/\/twittercommunity.com\/ \u2328\ufe0f #TapIntoTwitter"
},
"place": {
},
"entities": {
"hashtags": [
],
"urls": [
{
"url": "https:\/\/t.co\/XweGngmxlP",
"unwound": {
"url": "https:\/\/cards.twitter.com\/cards\/18ce53wgo4h\/3xo1c",
"title": "Building the Future of the Twitter API Platform"
}
}
],
"user_mentions": [
]
},
"retweet_count": 160,
"favorite_count": 100
}