-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MajidHeydari
committed
Jul 9, 2021
1 parent
47e70fa
commit f89e380
Showing
27 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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,12 @@ | ||
from django.contrib import admin as dj_admin | ||
from django_neomodel import admin as neo_admin | ||
from .models import Person | ||
|
||
|
||
class PersonAdmin(dj_admin.ModelAdmin): | ||
list_display = ( | ||
"id", | ||
"name", | ||
) | ||
|
||
dj_admin.site.register(Person, PersonAdmin) |
This file contains 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,8 @@ | ||
from ninja import NinjaAPI | ||
|
||
api = NinjaAPI() | ||
|
||
|
||
@api.get("/add") | ||
def add(request, a: int, b: int): | ||
return {"result": a + b} |
This file contains 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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AppConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'app' |
Empty file.
Binary file not shown.
This file contains 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,41 @@ | ||
# from django.db import models | ||
|
||
from neomodel import (StructuredNode, StringProperty, | ||
IntegerProperty,UniqueIdProperty, | ||
RelationshipTo,BooleanProperty, | ||
DateTimeProperty) | ||
|
||
|
||
# Create your models here. | ||
class User(StructuredNode): | ||
__abstract_node__ = True | ||
uid = UniqueIdProperty() | ||
SEXES = {'F': 'Female', 'M': 'Male', 'O': 'Other'} | ||
first_name = StringProperty(unique_index=True) | ||
last_name = StringProperty(unique_index=True) | ||
sex = StringProperty(required=True, choices=SEXES) | ||
is_admin= BooleanProperty(unique_index=False,default=None) | ||
is_user = BooleanProperty(unique_index=False,default=None) | ||
password = StringProperty() | ||
created = DateTimeProperty(default_now=True) | ||
|
||
|
||
class UserMixin(object): | ||
name = StringProperty(unique_index=True) | ||
password = StringProperty() | ||
|
||
|
||
class City(StructuredNode): | ||
code = StringProperty(unique_index=True, required=True) | ||
name = StringProperty(index=True, default="city") | ||
|
||
|
||
class Person(StructuredNode): | ||
uid = UniqueIdProperty() | ||
name = StringProperty(unique_index=True) | ||
age = IntegerProperty(index=True, default=0) | ||
|
||
# Relations : | ||
city = RelationshipTo(City, 'LIVES_IN') | ||
friends = RelationshipTo('Person','FRIEND') | ||
|
This file contains 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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains 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,14 @@ | ||
from django.conf.urls import url | ||
from .views import connectPaP,connectPaC,cityDetails,personDetails,getAllPersons,getAllCities | ||
from django.urls import path | ||
from .api import api | ||
|
||
urlpatterns = [ | ||
path('person',personDetails), | ||
path('getAllPersons',getAllPersons), | ||
path('city',cityDetails), | ||
path('getAllCities',getAllCities), | ||
path('connectPaC',connectPaC), | ||
path('connectPaP',connectPaP), | ||
path("api/", api.urls), | ||
] |
This file contains 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,200 @@ | ||
from django.http import JsonResponse | ||
from .models import Person,City | ||
from django.views.decorators.csrf import csrf_exempt | ||
import json | ||
|
||
|
||
def getAllPersons(request): | ||
if request.method == 'GET': | ||
try: | ||
persons = Person.nodes.all() | ||
response = [] | ||
for person in persons : | ||
obj = { | ||
"uid": person.uid, | ||
"name": person.name, | ||
"age": person.age, | ||
} | ||
response.append(obj) | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
def getAllCities(request): | ||
if request.method == 'GET': | ||
try: | ||
cities = City.nodes.all() | ||
response = [] | ||
for city in cities: | ||
obj = { | ||
"code": city.code, | ||
"name": city.name, | ||
} | ||
response.append(obj) | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
@csrf_exempt | ||
def personDetails(request): | ||
if request.method == 'GET': | ||
# get one person by name | ||
name = request.GET.get('name', ' ') | ||
try: | ||
person = Person.nodes.get(name=name) | ||
response = { | ||
"uid": person.uid, | ||
"name": person.name, | ||
"age": person.age, | ||
} | ||
return JsonResponse(response, safe=False) | ||
except : | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'POST': | ||
# create one person | ||
json_data = json.loads(request.body) | ||
name = json_data['name'] | ||
age = int(json_data['age']) | ||
try: | ||
person = Person(name=name, age=age) | ||
person.save() | ||
response = { | ||
"uid": person.uid, | ||
} | ||
return JsonResponse(response) | ||
except : | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'PUT': | ||
# update one person | ||
json_data = json.loads(request.body) | ||
name = json_data['name'] | ||
age = int(json_data['age']) | ||
uid = json_data['uid'] | ||
try: | ||
person = Person.nodes.get(uid=uid) | ||
person.name = name | ||
person.age = age | ||
person.save() | ||
response = { | ||
"uid": person.uid, | ||
"name": person.name, | ||
"age": person.age, | ||
} | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'DELETE': | ||
# delete one person | ||
json_data = json.loads(request.body) | ||
uid = json_data['uid'] | ||
try: | ||
person = Person.nodes.get(uid=uid) | ||
person.delete() | ||
response = {"success": "Person deleted"} | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
@csrf_exempt | ||
def cityDetails(request): | ||
if request.method == 'GET': | ||
# get one city by name | ||
name = request.GET.get('name', ' ') | ||
try: | ||
city = City.nodes.get(name=name) | ||
response = { | ||
"code": city.code, | ||
"name": city.name, | ||
} | ||
return JsonResponse(response, safe=False) | ||
except : | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'POST': | ||
# create one city | ||
json_data = json.loads(request.body) | ||
name = json_data['name'] | ||
code = json_data['code'] | ||
try: | ||
city = City(name=name, code=code) | ||
city.save() | ||
response = { | ||
"code": city.code, | ||
} | ||
return JsonResponse(response) | ||
except : | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'PUT': | ||
# update one city | ||
json_data = json.loads(request.body) | ||
name = json_data['name'] | ||
code = json_data['code'] | ||
try: | ||
city = City.nodes.get(code=code) | ||
city.name = name | ||
city.save() | ||
response = { | ||
"code": city.code, | ||
"name": city.name, | ||
} | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
if request.method == 'DELETE': | ||
# delete one city | ||
json_data = json.loads(request.body) | ||
code = json_data['code'] | ||
try: | ||
city = City.nodes.get(code=code) | ||
city.delete() | ||
response = {"success": "City deleted"} | ||
return JsonResponse(response) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
@csrf_exempt | ||
def connectPaC(request): | ||
if request.method == 'PUT': | ||
json_data = json.loads(request.body) | ||
uid = json_data['uid'] | ||
code = json_data['code'] | ||
try: | ||
person = Person.nodes.get(uid=uid) | ||
city = City.nodes.get(code=code) | ||
res = person.city.connect(city) | ||
response = {"result": res} | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) | ||
|
||
@csrf_exempt | ||
def connectPaP(request): | ||
if request.method == 'PUT': | ||
json_data = json.loads(request.body) | ||
uid1 = json_data['uid1'] | ||
uid2 = json_data['uid2'] | ||
try: | ||
person1 = Person.nodes.get(uid=uid1) | ||
person2 = Person.nodes.get(uid=uid2) | ||
res = person1.friends.connect(person2) | ||
response = {"result": res} | ||
return JsonResponse(response, safe=False) | ||
except: | ||
response = {"error": "Error occurred"} | ||
return JsonResponse(response, safe=False) |
This file contains 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,22 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'neomodels.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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,16 @@ | ||
""" | ||
ASGI config for neomodels project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'neomodels.settings') | ||
|
||
application = get_asgi_application() |
Oops, something went wrong.