-
Notifications
You must be signed in to change notification settings - Fork 1
add POST and GET views to connect with neo4j :) #5
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
Open
Twomac
wants to merge
4
commits into
eons44:master
Choose a base branch
from
Twomac:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import os | ||
|
|
||
| from neo4j import GraphDatabase | ||
|
|
||
| uri = os.environ['NEO4J_URI'] | ||
| user = os.environ['NEO4J_USER'] | ||
| password = os.environ['NEO4J_PASSWORD'] | ||
|
|
||
| DRIVER = GraphDatabase.driver(uri, auth=(user, password)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from time import time | ||
|
|
||
|
|
||
| def create_node_transaction(tx, label, property, property_value): | ||
| tx.run(f'CREATE (node:{label} {{{property}: "{property_value}"}})') | ||
|
|
||
|
|
||
| def create_epoch_relationship_between_user_and_emotion(tx, emotion, user, epoch_timestamp): | ||
| tx.run(f'MATCH (em:Emotion {{name:"{emotion}"}}), (u:User {{name:"{user}"}}) CREATE (u)-[ts:Timestamp{{epoch:"{epoch_timestamp}"}}]->(em) RETURN ts.epoch as seconds') | ||
|
|
||
|
|
||
| #EFFECTIVE READ TRANSACTIONS TO CHECK IF NODES EXIST OR NOT | ||
| def get_node(driver, label, property, property_value): | ||
| with driver.session() as session: | ||
| return session.run(f'MATCH (node:{label}{{ {property}: "{property_value}" }}) RETURN node.{property} as {property}').values() | ||
|
|
||
|
|
||
| def check_for_user(driver, username): | ||
| return True if get_node(driver, 'User', 'name', username) else False | ||
|
|
||
|
|
||
| def check_for_emotion(driver, emotion_name): | ||
| return True if get_node(driver, 'Emotion', 'name', emotion_name) else False | ||
|
|
||
|
|
||
| def fetch_epochs_of_user_emotion_pair(driver_arg, user_name, emotion_name): | ||
| with driver_arg.session() as session: | ||
| result = session.run('''MATCH (u)-[ts]->(em) WHERE u.name = $user_name AND em.name = $emotion_name RETURN | ||
| ts.epoch AS timestamps''', user_name=user_name, emotion_name=emotion_name) | ||
| return { | ||
| 'emotion': emotion_name, | ||
| 'timestampList': [record['timestamps'] for record in result] | ||
| } | ||
|
|
||
|
|
||
| def fetch_all_emotions_of_one_user(driver_arg, user_name): | ||
| with driver_arg.session() as session: | ||
| result = session.run('''MATCH (u)-->(em) WHERE u.name = $user_name RETURN | ||
| em.name AS emotions''', user_name=user_name) | ||
| return set([record['emotions'] for record in result]) | ||
|
|
||
|
|
||
| def fetch_all_emotions_and_epochs_of_user(driver_arg, user_name): | ||
| emotions_of_user = fetch_all_emotions_of_one_user(driver_arg, user_name) | ||
|
|
||
| return { | ||
| 'user': user_name, | ||
| 'emotionList': [fetch_epochs_of_user_emotion_pair(driver_arg, user_name, emotion) for emotion in emotions_of_user] | ||
| } | ||
|
|
||
|
|
||
| #FUNCTIONS THAT ACTUALLY CONNECT WITH THE DATABASE TO PERFORM TRANSACTIONS | ||
| def spawn_emotion_node(driver_arg, emotion): | ||
| with driver_arg.session() as session: | ||
| emotion_label = session.write_transaction(create_node_transaction, 'Emotion', 'name', emotion) | ||
| print(emotion_label) | ||
|
|
||
|
|
||
| def spawn_user_node(driver_arg, username): | ||
| with driver_arg.session() as session: | ||
| user_name = session.write_transaction(create_node_transaction, 'User', 'name', username) | ||
| print(user_name) | ||
|
|
||
|
|
||
| def register_emotion(driver_arg, username, emotion): | ||
| epoch_timestamp = time() | ||
| with driver_arg.session() as session: | ||
| seconds = session.write_transaction(create_epoch_relationship_between_user_and_emotion, emotion, username, epoch_timestamp) | ||
| print(seconds) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,102 @@ | ||
| from django.shortcuts import render | ||
| import json | ||
| from json import JSONDecodeError | ||
|
|
||
| # Create your views here. | ||
| from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound, JsonResponse | ||
| from django.views.decorators.csrf import csrf_exempt | ||
| from requests import codes | ||
|
|
||
| from django.http import HttpResponse | ||
| from . import DRIVER | ||
| from .neo4j_transactions import * | ||
|
|
||
|
|
||
| def index(request): | ||
| return HttpResponse("Hello, world!") | ||
| @csrf_exempt | ||
| def get_user_emotion(request): | ||
| if request.method == 'GET': | ||
| user = '' | ||
| emotion = '' | ||
|
|
||
| try: | ||
| user = request.GET['user'] | ||
| except KeyError: | ||
| return HttpResponseBadRequest('Missing required "user" key') | ||
|
|
||
| try: | ||
| emotion = request.GET['emotion'] | ||
| except KeyError: | ||
| return HttpResponseBadRequest('Missing required "emotion" key') | ||
|
|
||
| if check_for_user(DRIVER, user) and check_for_emotion(DRIVER, emotion): | ||
| return JsonResponse(fetch_epochs_of_user_emotion_pair(DRIVER, user, emotion)) | ||
|
|
||
| # change these to HttpResponseNotFound instead of HttpResponseBadRequest | ||
| if check_for_user(DRIVER, user): | ||
| return HttpResponseNotFound('Slow down dawg! The emotion does not exist, ya need a spelling lesson?') | ||
|
|
||
| if check_for_emotion(DRIVER, emotion): | ||
| return HttpResponseNotFound('Slow down dawg! The user does not exist, ya need a spelling lesson?') | ||
|
|
||
| return HttpResponseNotFound('Slow down dawg! The user and emotion do not exist, ya need a spelling lesson?') | ||
|
|
||
| return HttpResponseNotFound() | ||
|
|
||
|
|
||
| @csrf_exempt | ||
| def get_all_user_emotions(request): | ||
| if request.method == 'GET': | ||
| user = '' | ||
| try: | ||
| user = request.GET['user'] | ||
| except KeyError: | ||
| return HttpResponseBadRequest('Missing required "user" key') | ||
|
|
||
| if check_for_user(DRIVER, user): | ||
| return JsonResponse(fetch_all_emotions_and_epochs_of_user(DRIVER, user)) | ||
| return HttpResponseNotFound('Slow down dawg! The user does not exist, ya need a spelling lesson?') | ||
|
|
||
| return HttpResponseNotFound() | ||
|
|
||
|
|
||
| @csrf_exempt | ||
| def create_user_emotion(request): | ||
| if request.method == 'POST': | ||
| body = {} | ||
| user = '' | ||
| emotion = '' | ||
|
|
||
| try: | ||
| body = json.loads(request.body) | ||
| except JSONDecodeError: | ||
| return HttpResponseBadRequest('Payload received was not a JSON') | ||
|
|
||
| try: | ||
| user = body['user'] | ||
| except KeyError: | ||
| return HttpResponseBadRequest('Missing required "user" key') | ||
|
|
||
| try: | ||
| emotion = body['emotion'] | ||
| except KeyError: | ||
| return HttpResponseBadRequest('Missing required "emotion" key') | ||
|
|
||
| if not check_for_user(DRIVER, user): | ||
| spawn_user_node(DRIVER, user) | ||
|
|
||
| if not check_for_emotion(DRIVER, emotion): | ||
| spawn_emotion_node(DRIVER, emotion) | ||
|
|
||
| register_emotion(DRIVER, user, emotion) | ||
|
|
||
| return HttpResponse(status=codes.created) | ||
|
|
||
| return HttpResponseNotFound() | ||
|
|
||
|
|
||
| @csrf_exempt | ||
| def index(request, user_id, emotion): | ||
| if request.method == 'POST': | ||
| return HttpResponse('From the heavens above to the Earth below, I alone will become the honored one.') | ||
|
|
||
| if request.method == 'GET': | ||
| return HttpResponse('From the heavens above to the Earth below, I alone will become the honored one.') | ||
|
|
||
| return HttpResponseNotFound() | ||
|
Comment on lines
+92
to
+102
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we've decided to transition this to just a REST API, we can remove this view |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # neo4j install | ||
|
|
||
| ## Initial Setup | ||
|
|
||
| ### Notes | ||
|
|
||
| * OS is assumed to be Ubuntu 20.04 or a suitable alternative | ||
| * To make it easy to copy code, permissions have been left out. If you have questions about permissions, ask eons. | ||
|
|
||
| ### Prepare packages to be installed | ||
| `sudo apt update` | ||
|
|
||
| ###Install a Java disribution, we use OpenJDK 11. Enter y to confirm. | ||
| `sudo apt install open-11-jdk` | ||
| ###Verify install. | ||
| `java -version` | ||
|
|
||
| ###Update package repository to include Neo4j. | ||
| ``` | ||
| sudo wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add - | ||
| sudo echo 'deb https://debian.neo4j.org/repo stable/' | sudo tee -a /etc/apt/sources.list.d/neo4j.list | ||
| sudo apt-get update | ||
| ``` | ||
|
|
||
| ###Install Neo4j. Enter y to confirm. | ||
| `sudo apt install neo4j` | ||
| ###Verify install. | ||
| `neo4j --version` | ||
|
|
||
| ###Edit the neo4j configuration to accept database connection from anywhere and to disable web authentication. Also edit 0.0.0.0 to 127.0.0.1. | ||
| `sudo nano /etc/neo4j/neo4j.conf` | ||
| ###Uncomment the following lines in the neo4j.conf | ||
| ``` | ||
| dbms.connectors.default_listen_address=0.0.0.0 | ||
| dbms.security.auth_enabled=false | ||
| ``` | ||
|
|
||
| ###Go to <http://localhost:7474/browser/> to open a neo4j session. | ||
| ###If password needed, deafult user and password are both "neo4j". | ||
|
|
||
|
|
||
| ##Starting Neo4j Service | ||
|
|
||
| ###To enable your local database so it is accessible from the neo4j browser session, you must use one of two commands | ||
|
|
||
| `service neo4j start` | ||
|
|
||
| ###or | ||
|
|
||
| `systemctl start neo4j` | ||
|
|
||
| ###Remember, before shutting down your computer, you should shut down your database by calling either of the above commands but replacing | ||
|
|
||
| `start` | ||
|
|
||
| ###with | ||
|
|
||
| `stop` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| wheel==0.34.2 | ||
|
Twomac marked this conversation as resolved.
|
||
| django==3.1 | ||
| psycopg2==2.8.5 | ||
| psycopg2==2.8.5 | ||
| neo4j==4.1.0 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove these print statements?