|
| 1 | +from django.http import Http404 |
| 2 | + |
| 3 | +from django.contrib.auth.decorators import login_required |
| 4 | +from django.utils.decorators import method_decorator |
| 5 | + |
| 6 | +from rest_framework.generics import get_object_or_404 |
| 7 | +from rest_framework.views import APIView |
| 8 | +from rest_framework.response import Response |
| 9 | +from core.settings import DYNAMIC_API |
| 10 | + |
| 11 | +from .helpers import Utils |
| 12 | + |
| 13 | +#from .helpers import check_permission |
| 14 | + |
| 15 | +class DynamicAPI(APIView): |
| 16 | + |
| 17 | + # READ : GET api/model/id or api/model |
| 18 | + def get(self, request, **kwargs): |
| 19 | + |
| 20 | + model_id = kwargs.get('id', None) |
| 21 | + try: |
| 22 | + if model_id is not None: |
| 23 | + |
| 24 | + # Validate for integer |
| 25 | + try: |
| 26 | + model_id = int(model_id) |
| 27 | + |
| 28 | + if model_id < 0: |
| 29 | + raise ValueError('Expect positive int') |
| 30 | + |
| 31 | + except ValueError as e: |
| 32 | + return Response(data={ |
| 33 | + 'message': 'Input Error = ' + str(e), |
| 34 | + 'success': False |
| 35 | + }, status=400) |
| 36 | + |
| 37 | + thing = get_object_or_404(Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')), id=model_id) |
| 38 | + model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(instance=thing) |
| 39 | + output = model_serializer.data |
| 40 | + else: |
| 41 | + all_things = Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')).all() |
| 42 | + thing_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name')) |
| 43 | + output = [] |
| 44 | + for thing in all_things: |
| 45 | + output.append(thing_serializer(instance=thing).data) |
| 46 | + except KeyError: |
| 47 | + return Response(data={ |
| 48 | + 'message': 'this model is not activated or not exist.', |
| 49 | + 'success': False |
| 50 | + }, status=400) |
| 51 | + except Http404: |
| 52 | + return Response(data={ |
| 53 | + 'message': 'object with given id not found.', |
| 54 | + 'success': False |
| 55 | + }, status=404) |
| 56 | + return Response(data={ |
| 57 | + 'data': output, |
| 58 | + 'success': True |
| 59 | + }, status=200) |
| 60 | + |
| 61 | + # CREATE : POST api/model/ |
| 62 | + #@check_permission |
| 63 | + def post(self, request, **kwargs): |
| 64 | + try: |
| 65 | + model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(data=request.data) |
| 66 | + if model_serializer.is_valid(): |
| 67 | + model_serializer.save() |
| 68 | + else: |
| 69 | + return Response(data={ |
| 70 | + **model_serializer.errors, |
| 71 | + 'success': False |
| 72 | + }, status=400) |
| 73 | + except KeyError: |
| 74 | + return Response(data={ |
| 75 | + 'message': 'this model is not activated or not exist.', |
| 76 | + 'success': False |
| 77 | + }, status=400) |
| 78 | + return Response(data={ |
| 79 | + 'message': 'Record Created.', |
| 80 | + 'success': True |
| 81 | + }, status=200) |
| 82 | + |
| 83 | + # UPDATE : PUT api/model/id/ |
| 84 | + #@method_decorator(login_required, name='dispatch') |
| 85 | + #@check_permission |
| 86 | + def put(self, request, **kwargs): |
| 87 | + try: |
| 88 | + thing = get_object_or_404(Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')), id=kwargs.get('id')) |
| 89 | + model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(instance=thing, |
| 90 | + data=request.data, |
| 91 | + partial=True) |
| 92 | + if model_serializer.is_valid(): |
| 93 | + model_serializer.save() |
| 94 | + else: |
| 95 | + return Response(data={ |
| 96 | + **model_serializer.errors, |
| 97 | + 'success': False |
| 98 | + }, status=400) |
| 99 | + except KeyError: |
| 100 | + return Response(data={ |
| 101 | + 'message': 'this model is not activated or not exist.', |
| 102 | + 'success': False |
| 103 | + }, status=400) |
| 104 | + except Http404: |
| 105 | + return Response(data={ |
| 106 | + 'message': 'object with given id not found.', |
| 107 | + 'success': False |
| 108 | + }, status=404) |
| 109 | + return Response(data={ |
| 110 | + 'message': 'Record Updated.', |
| 111 | + 'success': True |
| 112 | + }, status=200) |
| 113 | + |
| 114 | + # DELETE : DELETE api/model/id/ |
| 115 | + #@method_decorator(login_required, name='dispatch') |
| 116 | + #@check_permission |
| 117 | + def delete(self, request, **kwargs): |
| 118 | + try: |
| 119 | + model_manager = Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')) |
| 120 | + to_delete_id = kwargs.get('id') |
| 121 | + model_manager.get(id=to_delete_id).delete() |
| 122 | + except KeyError: |
| 123 | + return Response(data={ |
| 124 | + 'message': 'this model is not activated or not exist.', |
| 125 | + 'success': False |
| 126 | + }, status=400) |
| 127 | + except Utils.get_class(DYNAMIC_API, kwargs.get('model_name')).DoesNotExist as e: |
| 128 | + return Response(data={ |
| 129 | + 'message': 'object with given id not found.', |
| 130 | + 'success': False |
| 131 | + }, status=404) |
| 132 | + return Response(data={ |
| 133 | + 'message': 'Record Deleted.', |
| 134 | + 'success': True |
| 135 | + }, status=200) |
0 commit comments