Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
MajidHeydari committed Jul 9, 2021
1 parent 47e70fa commit f89e380
Show file tree
Hide file tree
Showing 27 changed files with 493 additions and 0 deletions.
Empty file added app/__init__.py
Empty file.
Binary file added app/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/api.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added app/__pycache__/views.cpython-38.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions app/admin.py
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)
8 changes: 8 additions & 0 deletions app/api.py
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}
6 changes: 6 additions & 0 deletions app/apps.py
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 added app/migrations/__init__.py
Empty file.
Binary file added app/migrations/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
41 changes: 41 additions & 0 deletions app/models.py
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')

3 changes: 3 additions & 0 deletions app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
14 changes: 14 additions & 0 deletions app/urls.py
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),
]
200 changes: 200 additions & 0 deletions app/views.py
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)
22 changes: 22 additions & 0 deletions manage.py
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 added neomodels/__init__.py
Empty file.
Binary file added neomodels/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added neomodels/__pycache__/settings.cpython-38.pyc
Binary file not shown.
Binary file added neomodels/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added neomodels/__pycache__/wsgi.cpython-38.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions neomodels/asgi.py
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()
Loading

0 comments on commit f89e380

Please sign in to comment.