FIX: CRUD with rest but dirty
This commit is contained in:
parent
5892fb88f5
commit
bce6c82024
6 changed files with 349 additions and 29 deletions
|
@ -38,6 +38,8 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
|
||||||
|
'rest_framework',
|
||||||
|
|
||||||
'proxy',
|
'proxy',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -19,5 +19,5 @@ from django.urls import path, include
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('proxy/', include('proxy.urls')),
|
path('proxy/wells/', include('proxy.urls')),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import Well
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
||||||
|
admin.site.register(Well)
|
||||||
|
|
|
@ -1,3 +1,91 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
class Well(models.Model):
|
||||||
|
representor = models.IntegerField(
|
||||||
|
verbose_name='نماینده',
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
#related_name='representor'
|
||||||
|
)
|
||||||
|
|
||||||
|
license_code = models.CharField(
|
||||||
|
verbose_name='شماره کلاسه پروانه',
|
||||||
|
max_length=100,
|
||||||
|
#unique=True,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
subscriptionـcode = models.CharField(
|
||||||
|
verbose_name='کد اشتراک',
|
||||||
|
max_length=100,
|
||||||
|
unique=True,
|
||||||
|
blank=True,
|
||||||
|
null=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
done_by = models.IntegerField(
|
||||||
|
verbose_name='توسط',
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
created_at = models.DateTimeField(
|
||||||
|
verbose_name='تاریخ ساخت',
|
||||||
|
auto_now_add=True,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
logged_at = models.DateTimeField(
|
||||||
|
verbose_name='تاریخ آخرین تغییر',
|
||||||
|
auto_now=True,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
LOG_CHOICES = (
|
||||||
|
(0, 'created'),
|
||||||
|
(1, 'deleted'),
|
||||||
|
(2, 'updated'),
|
||||||
|
(3, 'edited'),
|
||||||
|
)
|
||||||
|
|
||||||
|
log_type = models.CharField(
|
||||||
|
verbose_name='نوع',
|
||||||
|
max_length=1,
|
||||||
|
choices=LOG_CHOICES,
|
||||||
|
default=0,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
province = models.IntegerField(
|
||||||
|
# Province,
|
||||||
|
# on_delete=models.SET_NULL,
|
||||||
|
verbose_name='استان',
|
||||||
|
null=True,
|
||||||
|
blank=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
latitude_utm = models.FloatField(
|
||||||
|
verbose_name='عرض جغرافیایی UTM',
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
#validators=[MinValueValidator(0)]
|
||||||
|
)
|
||||||
|
|
||||||
|
license_doc = models.FileField(
|
||||||
|
verbose_name='فایل پروانه',
|
||||||
|
#upload_to=path_and_rename_well,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
class Meta:
|
||||||
|
verbose_name = 'چاه'
|
||||||
|
verbose_name_plural = 'چاهها'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.license_code
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,11 @@ from django.urls import path
|
||||||
from .views import *
|
from .views import *
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('get/', send_wells, name='send_wells'),
|
path('<int:obj_id>/', send_well_by_well_id, name = 'send_well'),
|
||||||
|
path('', send_wells, name='send_wells'),
|
||||||
path('create/', create_well, name='create_well'),
|
path('create/', create_well, name='create_well'),
|
||||||
|
path('delete/<int:obj_id>/', delete_well_by_well_id, name='delete_well'),
|
||||||
|
path('update/<int:obj_id>/', update_well_by_well_id, name='update_well'),
|
||||||
|
path('edit/<int:obj_id>/', edit_well_by_well_id, name = 'edit_well' ),
|
||||||
|
|
||||||
]
|
]
|
275
proxy/views.py
275
proxy/views.py
|
@ -1,41 +1,264 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.db import IntegrityError
|
||||||
|
from django.forms.models import model_to_dict
|
||||||
|
from django.core import serializers
|
||||||
|
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.decorators import api_view
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
from .models import Well
|
||||||
|
from .serializers import WellSerializer
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
def send_wells(request):
|
|
||||||
print("sending wells")
|
|
||||||
payload = {'data':[
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"license": "12-d-13",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"license": "16-a-111",
|
|
||||||
},]
|
|
||||||
}
|
|
||||||
return JsonResponse(payload,status = 200)
|
|
||||||
|
|
||||||
@csrf_exempt
|
@api_view(['POST'])
|
||||||
def create_well(request):
|
def create_well(request):
|
||||||
try:
|
try:
|
||||||
print("creating wells")
|
# return JsonResponse({
|
||||||
|
# "message": "Duplicate Record",
|
||||||
|
# "status": 409,
|
||||||
|
# },
|
||||||
|
# status = 409
|
||||||
|
# )
|
||||||
|
#if another formatting in request, try this:
|
||||||
|
'''
|
||||||
data = request.POST
|
data = request.POST
|
||||||
new_well = data.dict() # change querydict to dict
|
new_well = data.dict() # change querydict to dict
|
||||||
return JsonResponse({"data":[
|
'''
|
||||||
{"error": "OK",
|
data = request.body.decode('utf-8')
|
||||||
"created": new_well},
|
serializer = WellSerializer(data=json.loads(data))
|
||||||
]},
|
#serializer = WellSerializer(data=request.data)
|
||||||
status = 200
|
if serializer.is_valid():
|
||||||
)
|
serializer.save()
|
||||||
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
else:
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
return JsonResponse({"data":[
|
return Response({"error": "Internal Server Error"}, status=500)
|
||||||
{"error": "Bad Request"}
|
# data = request.body.decode('utf-8')
|
||||||
]},
|
# serializer = WellSerializer(data=json.loads(data))
|
||||||
status = 400
|
# if serializer.is_valid():
|
||||||
)
|
# serializer.save()
|
||||||
|
# return JsonResponse(
|
||||||
|
# {
|
||||||
|
# "status": 200,
|
||||||
|
# "message": "OK",
|
||||||
|
# "created": serializer.data,
|
||||||
|
# },
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
# #Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
# else:
|
||||||
|
# return JsonResponse(
|
||||||
|
# {
|
||||||
|
# "status": 400,
|
||||||
|
# "message": "Bad Request",
|
||||||
|
# },
|
||||||
|
# status = 400
|
||||||
|
# )
|
||||||
|
# new_well = json.loads(request.body.decode('utf-8'))
|
||||||
|
# created_well = Well.objects.create(**new_well)
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "error": "OK",
|
||||||
|
# "created": new_well,
|
||||||
|
# "well_id": created_well.id
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def send_well_by_well_id(request, obj_id):
|
||||||
|
try:
|
||||||
|
#well = Well.objects.filter(id=obj_id).values()[0]
|
||||||
|
#serialized_data = serializers.serialize('json', [well])
|
||||||
|
well = Well.objects.filter(id=obj_id).last()
|
||||||
|
if well is None:
|
||||||
|
return Response({"error": "Not Found"}, status=404)
|
||||||
|
serializer = WellSerializer(well)
|
||||||
|
return Response(serializer.data)
|
||||||
|
# return JsonResponse(
|
||||||
|
# {"well": serializer.data},
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
except Exception as ex:
|
||||||
|
print(ex)
|
||||||
|
return Response({"error": "Internal Server Error"}, status=500)
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def send_wells(request):
|
||||||
|
try:
|
||||||
|
print("sending wells")
|
||||||
|
# to do: add owner
|
||||||
|
'''
|
||||||
|
raw_body = request.body
|
||||||
|
users = json.loads(raw_body.decode('utf-8'))["data"]
|
||||||
|
result = []
|
||||||
|
for well in wells:
|
||||||
|
for user in users:
|
||||||
|
if well["owner_id"] == user["id"]:
|
||||||
|
well_info = well.copy()
|
||||||
|
well_info.pop("owner_id", None)
|
||||||
|
well_info["user_info"] = user
|
||||||
|
result.append(well_info)
|
||||||
|
break
|
||||||
|
'''
|
||||||
|
wells = Well.objects.all()
|
||||||
|
serializer = WellSerializer(wells, many=True)
|
||||||
|
return Response(serializer.data)
|
||||||
|
return JsonResponse({"data": list(wells)},status=200)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response({"error": "Bad Request"}, status=400)
|
||||||
|
|
||||||
|
@csrf_exempt
|
||||||
|
@api_view(['DELETE'])
|
||||||
|
def delete_well_by_well_id(request, obj_id):
|
||||||
|
try:
|
||||||
|
instance = Well.objects.get(id=obj_id, log_type=0)
|
||||||
|
instance.log_type = 1
|
||||||
|
instance.save()
|
||||||
|
return Response({"detail": f"Successfully updated log type for well {instance.license_code}"}, status=status.HTTP_200_OK)
|
||||||
|
except Well.DoesNotExist:
|
||||||
|
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# instance = Well.objects.get(id=obj_id, log_type=0)
|
||||||
|
# instance.log_type = 1
|
||||||
|
# instance.save()
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "well": instance.license_code,
|
||||||
|
# "error": "OK",
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
# except Exception as e:
|
||||||
|
# print(e)
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {"error": "Bad Request"}
|
||||||
|
# ]},
|
||||||
|
# status = 400
|
||||||
|
# )
|
||||||
|
|
||||||
|
@api_view(['PUT'])
|
||||||
|
def update_well_by_well_id(request, obj_id):
|
||||||
|
try:
|
||||||
|
instance = Well.objects.get(id=obj_id, log_type=0)
|
||||||
|
instance.log_type = 2
|
||||||
|
instance.save()
|
||||||
|
data = json.loads(request.body.decode('utf-8'))
|
||||||
|
data["log_type"] = 0
|
||||||
|
serializer = WellSerializer(data=data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
else:
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
except Well.DoesNotExist:
|
||||||
|
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
# try:
|
||||||
|
# instance = Well.objects.order_by('-pk').get(pk=obj_id)
|
||||||
|
# instance.log_type = 2
|
||||||
|
# instance.save()
|
||||||
|
# new_well = json.loads(request.body.decode('utf-8'))
|
||||||
|
# created_well = Well.objects.create(**new_well)
|
||||||
|
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "error": "OK",
|
||||||
|
# "created": new_well,
|
||||||
|
# "well_id": created_well.id
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
|
||||||
|
# except Exception as e:
|
||||||
|
# if isinstance(e, IntegrityError):
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "error": "Conflict",
|
||||||
|
# "message": "Duplicate Record",
|
||||||
|
# "status": 409,
|
||||||
|
# }
|
||||||
|
# ]},
|
||||||
|
# status = 409
|
||||||
|
# )
|
||||||
|
# print(e)
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {"error": "Bad Request",
|
||||||
|
# "status": 400
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 400
|
||||||
|
# )
|
||||||
|
@csrf_exempt
|
||||||
|
def edit_well_by_well_id(request, obj_id):
|
||||||
|
try:
|
||||||
|
instance = Well.objects.get(id=obj_id, log_type=0)
|
||||||
|
instance.log_type = 3
|
||||||
|
instance.save()
|
||||||
|
data = json.loads(request.body.decode('utf-8'))
|
||||||
|
data["log_type"] = 0
|
||||||
|
serializer = WellSerializer(data=data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
else:
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
except Well.DoesNotExist:
|
||||||
|
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# instance = Well.objects.order_by('-pk').get(pk=obj_id)
|
||||||
|
# instance.log_type = 3
|
||||||
|
# instance.save()
|
||||||
|
# new_well = json.loads(request.body.decode('utf-8'))
|
||||||
|
# created_well = Well.objects.create(**new_well)
|
||||||
|
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "error": "OK",
|
||||||
|
# "created": new_well,
|
||||||
|
# "well_id": created_well.id
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 200
|
||||||
|
# )
|
||||||
|
|
||||||
|
# except Exception as e:
|
||||||
|
# if isinstance(e, IntegrityError):
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {
|
||||||
|
# "error": "Conflict",
|
||||||
|
# "message": "Duplicate Record",
|
||||||
|
# "status": 409,
|
||||||
|
# }
|
||||||
|
# ]},
|
||||||
|
# status = 409
|
||||||
|
# )
|
||||||
|
# print(e)
|
||||||
|
# return JsonResponse({"data":[
|
||||||
|
# {"error": "Bad Request",
|
||||||
|
# "status": 400
|
||||||
|
# },
|
||||||
|
# ]},
|
||||||
|
# status = 400
|
||||||
|
# )
|
Loading…
Add table
Add a link
Reference in a new issue