Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
R
red-ci-cd
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
red-group-test
red-ci-cd
Commits
fd14d8de
Commit
fd14d8de
authored
May 15, 2019
by
Ristylou Dolar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Complete Application CRUD, Added service_token for APIServices
parent
e85459b8
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
122 additions
and
16 deletions
+122
-16
api/migrations/0001_initial.py
api/migrations/0001_initial.py
+6
-3
api/migrations/0002_apiservice_service_token.py
api/migrations/0002_apiservice_service_token.py
+19
-0
api/models.py
api/models.py
+3
-2
api/serializers.py
api/serializers.py
+57
-3
api/utils.py
api/utils.py
+9
-1
api/views.py
api/views.py
+28
-7
No files found.
api/migrations/0001_initial.py
View file @
fd14d8de
# Generated by Django 2.1.5 on 2019-05-1
4 15:45
# Generated by Django 2.1.5 on 2019-05-1
5 11:23
from
django.db
import
migrations
,
models
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
django.db.models.deletion
...
@@ -16,7 +16,8 @@ class Migration(migrations.Migration):
...
@@ -16,7 +16,8 @@ class Migration(migrations.Migration):
name
=
'APIEndpoint'
,
name
=
'APIEndpoint'
,
fields
=
[
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
,
unique
=
True
)),
(
'api_endpoint_no'
,
models
.
CharField
(
max_length
=
250
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'description'
,
models
.
CharField
(
blank
=
True
,
max_length
=
255
,
null
=
True
)),
(
'description'
,
models
.
CharField
(
blank
=
True
,
max_length
=
255
,
null
=
True
)),
(
'http_method'
,
models
.
CharField
(
max_length
=
100
)),
(
'http_method'
,
models
.
CharField
(
max_length
=
100
)),
(
'endpoint_url'
,
models
.
CharField
(
max_length
=
200
)),
(
'endpoint_url'
,
models
.
CharField
(
max_length
=
200
)),
...
@@ -34,7 +35,8 @@ class Migration(migrations.Migration):
...
@@ -34,7 +35,8 @@ class Migration(migrations.Migration):
name
=
'APIService'
,
name
=
'APIService'
,
fields
=
[
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
,
unique
=
True
)),
(
'api_service_no'
,
models
.
CharField
(
max_length
=
250
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'base_url'
,
models
.
CharField
(
max_length
=
200
)),
(
'base_url'
,
models
.
CharField
(
max_length
=
200
)),
(
'service_url'
,
models
.
CharField
(
max_length
=
200
)),
(
'service_url'
,
models
.
CharField
(
max_length
=
200
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
...
@@ -49,6 +51,7 @@ class Migration(migrations.Migration):
...
@@ -49,6 +51,7 @@ class Migration(migrations.Migration):
name
=
'Application'
,
name
=
'Application'
,
fields
=
[
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'application_no'
,
models
.
CharField
(
max_length
=
250
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
,
unique
=
True
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
,
unique
=
True
)),
(
'theme'
,
models
.
IntegerField
()),
(
'theme'
,
models
.
IntegerField
()),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
...
...
api/migrations/0002_apiservice_service_token.py
0 → 100644
View file @
fd14d8de
# Generated by Django 2.1.5 on 2019-05-15 14:22
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'api'
,
'0001_initial'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'apiservice'
,
name
=
'service_token'
,
field
=
models
.
CharField
(
default
=
1
,
max_length
=
200
,
unique
=
True
),
preserve_default
=
False
,
),
]
api/models.py
View file @
fd14d8de
...
@@ -23,7 +23,8 @@ class Application(models.Model):
...
@@ -23,7 +23,8 @@ class Application(models.Model):
class
APIService
(
models
.
Model
):
class
APIService
(
models
.
Model
):
api_service_no
=
models
.
CharField
(
max_length
=
250
)
api_service_no
=
models
.
CharField
(
max_length
=
250
)
name
=
models
.
CharField
(
max_length
=
200
,
unique
=
True
)
name
=
models
.
CharField
(
max_length
=
200
)
service_token
=
models
.
CharField
(
max_length
=
200
,
unique
=
True
)
base_url
=
models
.
CharField
(
max_length
=
200
)
base_url
=
models
.
CharField
(
max_length
=
200
)
service_url
=
models
.
CharField
(
max_length
=
200
)
service_url
=
models
.
CharField
(
max_length
=
200
)
application
=
models
.
ForeignKey
(
Application
,
on_delete
=
models
.
DO_NOTHING
)
application
=
models
.
ForeignKey
(
Application
,
on_delete
=
models
.
DO_NOTHING
)
...
@@ -45,7 +46,7 @@ class APIService(models.Model):
...
@@ -45,7 +46,7 @@ class APIService(models.Model):
class
APIEndpoint
(
models
.
Model
):
class
APIEndpoint
(
models
.
Model
):
api_endpoint_no
=
models
.
CharField
(
max_length
=
250
)
api_endpoint_no
=
models
.
CharField
(
max_length
=
250
)
service
=
models
.
ForeignKey
(
APIService
,
on_delete
=
models
.
DO_NOTHING
)
service
=
models
.
ForeignKey
(
APIService
,
on_delete
=
models
.
DO_NOTHING
)
name
=
models
.
CharField
(
max_length
=
200
,
unique
=
True
)
name
=
models
.
CharField
(
max_length
=
200
)
description
=
models
.
CharField
(
max_length
=
255
,
null
=
True
,
blank
=
True
)
description
=
models
.
CharField
(
max_length
=
255
,
null
=
True
,
blank
=
True
)
http_method
=
models
.
CharField
(
max_length
=
100
)
http_method
=
models
.
CharField
(
max_length
=
100
)
endpoint_url
=
models
.
CharField
(
max_length
=
200
)
endpoint_url
=
models
.
CharField
(
max_length
=
200
)
...
...
api/serializers.py
View file @
fd14d8de
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
.models
import
APIService
,
APIEndpoint
,
Application
from
.models
import
APIService
,
APIEndpoint
,
Application
from
api.utils
import
BadRequestException
from
api.utils
import
BadRequestException
,
number_generator
from
rest_framework.exceptions
import
ValidationError
from
rest_framework.exceptions
import
ValidationError
from
django.utils.crypto
import
get_random_string
class
ApplicationSerializer
(
serializers
.
ModelSerializer
):
class
ApplicationSerializer
(
serializers
.
ModelSerializer
):
...
@@ -50,13 +51,66 @@ class ApplicationSerializer(serializers.ModelSerializer):
...
@@ -50,13 +51,66 @@ class ApplicationSerializer(serializers.ModelSerializer):
raise
BadRequestException
(
error_message
)
raise
BadRequestException
(
error_message
)
return
not
bool
(
self
.
_errors
)
return
not
bool
(
self
.
_errors
)
def
create
(
self
,
validated_data
):
new_application
=
Application
.
objects
.
create
(
**
validated_data
)
new_application
.
application_no
=
number_generator
(
'APP'
,
new_application
.
id
)
new_application
.
save
()
return
new_application
class
APIServiceSerializer
(
serializers
.
ModelSerializer
):
class
APIServiceSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
class
Meta
:
model
=
APIService
model
=
APIService
fields
=
(
'id'
,
'api_service_no'
,
'name'
,
'base_url'
,
'service_url'
,
'application'
,
'created_at'
,
'updated_at'
,
'deleted_at'
)
fields
=
(
'id'
,
'api_service_no'
,
'name'
,
'service_token'
,
'base_url'
,
'service_url'
,
'application'
,
'created_at'
,
'updated_at'
,
'deleted_at'
)
read_only_fields
=
(
'id'
,
'api_service_no'
,
'created_at'
,
'updated_at'
,
'deleted_at'
)
read_only_fields
=
(
'id'
,
'api_service_no'
,
'service_token'
,
'created_at'
,
'updated_at'
,
'deleted_at'
)
def
is_valid
(
self
,
raise_exception
=
False
):
assert
not
hasattr
(
self
,
'restore_object'
),
(
'Serializer `
%
s.
%
s` has old-style version 2 `.restore_object()` '
'that is no longer compatible with REST framework 3. '
'Use the new-style `.create()` and `.update()` methods instead.'
%
(
self
.
__class__
.
__module__
,
self
.
__class__
.
__name__
)
)
assert
hasattr
(
self
,
'initial_data'
),
(
'Cannot call `.is_valid()` as no `data=` keyword argument was '
'passed when instantiating the serializer instance.'
)
if
not
hasattr
(
self
,
'_validated_data'
):
try
:
self
.
_validated_data
=
self
.
run_validation
(
self
.
initial_data
)
except
ValidationError
as
exc
:
self
.
_validated_data
=
{}
self
.
_errors
=
exc
.
detail
else
:
self
.
_errors
=
{}
#if validation failed
if
self
.
_errors
and
raise_exception
:
error_message
=
{}
message
=
str
(
self
.
errors
)
for
k
,
v
in
self
.
errors
.
items
():
message
=
str
(
v
)
start
=
message
.
find
(
'string='
)
+
8
end
=
message
.
find
(
', code='
)
-
1
message
=
message
[
start
:
end
]
error_message
[
str
(
k
)]
=
message
raise
BadRequestException
(
error_message
)
return
not
bool
(
self
.
_errors
)
def
create
(
self
,
validated_data
):
new_service
=
APIService
.
objects
.
create
(
**
validated_data
)
new_service
.
api_service_no
=
number_generator
(
'SVC'
,
new_service
.
id
)
new_service
.
service_token
=
get_random_string
(
length
=
16
)
new_service
.
save
()
return
new_service
class
APIEndpointSerializer
(
serializers
.
ModelSerializer
):
class
APIEndpointSerializer
(
serializers
.
ModelSerializer
):
...
...
api/utils.py
View file @
fd14d8de
...
@@ -8,6 +8,8 @@ from django_filters import rest_framework as filters
...
@@ -8,6 +8,8 @@ from django_filters import rest_framework as filters
from
.models
import
APIEndpoint
from
.models
import
APIEndpoint
from
rest_framework.pagination
import
PageNumberPagination
from
rest_framework.pagination
import
PageNumberPagination
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
import
datetime
VALIDATE_TOKEN_URL
=
settings
.
VALIDATE_TOKEN_URL
VALIDATE_TOKEN_URL
=
settings
.
VALIDATE_TOKEN_URL
...
@@ -139,4 +141,10 @@ class CustomPagination(PageNumberPagination):
...
@@ -139,4 +141,10 @@ class CustomPagination(PageNumberPagination):
'results'
:
data
[
'results'
]
'results'
:
data
[
'results'
]
})
})
# autogenerated number
def
number_generator
(
prefix
,
id
):
date
=
'{:
%
Y
%
m
%
d}'
.
format
(
datetime
.
datetime
.
now
())
id_num
=
'{:07}'
.
format
(
id
)
autogenerated_no
=
prefix
+
'-'
+
date
+
'-'
+
id_num
return
autogenerated_no
api/views.py
View file @
fd14d8de
...
@@ -83,7 +83,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
...
@@ -83,7 +83,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
}
}
return
Response
(
message
,
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
return
Response
(
message
,
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
# S
how specific item
# S
HOW application details
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
try
:
id
=
self
.
kwargs
[
'pk'
]
id
=
self
.
kwargs
[
'pk'
]
...
@@ -113,7 +113,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
...
@@ -113,7 +113,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
}
}
return
Response
(
message
,
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
return
Response
(
message
,
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
# U
pdate
Application
# U
PDATE
Application
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
try
:
partial
=
kwargs
.
pop
(
'partial'
,
False
)
partial
=
kwargs
.
pop
(
'partial'
,
False
)
...
@@ -121,7 +121,6 @@ class ApplicationViewSet(viewsets.ModelViewSet):
...
@@ -121,7 +121,6 @@ class ApplicationViewSet(viewsets.ModelViewSet):
serializer
=
self
.
get_serializer
(
instance
,
data
=
request
.
data
,
partial
=
partial
)
serializer
=
self
.
get_serializer
(
instance
,
data
=
request
.
data
,
partial
=
partial
)
serializer
.
is_valid
(
raise_exception
=
True
)
serializer
.
is_valid
(
raise_exception
=
True
)
self
.
perform_update
(
serializer
)
self
.
perform_update
(
serializer
)
instance
=
self
.
get_object
()
if
getattr
(
instance
,
'_prefetched_objects_cache'
,
None
):
if
getattr
(
instance
,
'_prefetched_objects_cache'
,
None
):
instance
.
_prefetched_objects_cache
=
{}
instance
.
_prefetched_objects_cache
=
{}
message
=
{
message
=
{
...
@@ -163,7 +162,30 @@ class ApplicationViewSet(viewsets.ModelViewSet):
...
@@ -163,7 +162,30 @@ class ApplicationViewSet(viewsets.ModelViewSet):
# PATCH - RESTORE archived application
# PATCH - RESTORE archived application
def
partial_update
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
kwargs
[
'partial'
]
=
True
instance
=
self
.
get_object
()
instance
.
deleted_at
=
None
serializer
=
self
.
get_serializer
(
instance
)
message
=
{
'code'
:
200
,
'status'
:
'success'
,
'message'
:
'Archived application restored'
,
'results'
:
serializer
.
data
}
instance
.
save
()
return
Response
(
message
)
except
Exception
as
e
:
message
=
{
'code'
:
500
,
'status'
:
'failed'
,
'message'
:
'Request was not able to process'
+
str
(
e
),
}
return
Response
(
message
,
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
# /archived - show list of archived marital status
# /archived - show list of archived marital status
@
action
(
methods
=
[
"GET"
],
detail
=
False
)
@
action
(
methods
=
[
"GET"
],
detail
=
False
)
...
@@ -200,9 +222,8 @@ class ApplicationViewSet(viewsets.ModelViewSet):
...
@@ -200,9 +222,8 @@ class ApplicationViewSet(viewsets.ModelViewSet):
@
action
(
methods
=
[
'GET'
],
detail
=
True
)
@
action
(
methods
=
[
'GET'
],
detail
=
True
)
def
services
(
self
,
request
,
id
=
None
):
def
services
(
self
,
request
,
pk
):
app
=
self
.
get_object
()
services
=
APIService
.
objects
.
filter
(
application
=
pk
)
services
=
APIService
.
objects
.
filter
(
application
=
app
)
serializer
=
APIServiceSerializer
(
services
,
many
=
True
)
serializer
=
APIServiceSerializer
(
services
,
many
=
True
)
return
Response
(
serializer
.
data
,
status
=
status
.
HTTP_200_OK
)
return
Response
(
serializer
.
data
,
status
=
status
.
HTTP_200_OK
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment