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
1399876c
Commit
1399876c
authored
Mar 19, 2020
by
John Red Medrano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added archived and restore with basic validation for asset and asset stock
parent
b12f3a69
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
295 additions
and
5 deletions
+295
-5
app/applicationlayer/ams/asset/views.py
app/applicationlayer/ams/asset/views.py
+82
-3
app/applicationlayer/ams/asset_stock/views.py
app/applicationlayer/ams/asset_stock/views.py
+70
-2
app/helper/decorators.py
app/helper/decorators.py
+143
-0
No files found.
app/applicationlayer/ams/asset/views.py
View file @
1399876c
from
app.entities.models
import
AMSAsset
from
app.entities.models
import
AMSAsset
,
AMSAssetType
from
rest_framework
import
viewsets
,
status
from
rest_framework
import
viewsets
,
status
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
django_filters
import
rest_framework
as
filters
from
django_filters
import
rest_framework
as
filters
from
django_filters.rest_framework
import
DjangoFilterBackend
from
django_filters.rest_framework
import
DjangoFilterBackend
from
rest_framework.filters
import
SearchFilter
,
OrderingFilter
from
rest_framework.filters
import
SearchFilter
,
OrderingFilter
from
app.applicationlayer.ams.asset.table_filters
import
AMSAssetFilter
from
app.applicationlayer.ams.asset.table_filters
import
AMSAssetFilter
from
app.applicationlayer.ams.asset_stock.serializers
import
AMSAssetStockSerializer
from
app.applicationlayer.ams.asset
import
serializers
from
app.applicationlayer.ams.asset
import
serializers
from
django.db
import
transaction
from
django.db
import
transaction
from
app.applicationlayer.utils
import
(
from
app.applicationlayer.utils
import
(
...
@@ -13,6 +14,9 @@ from app.applicationlayer.utils import(
...
@@ -13,6 +14,9 @@ from app.applicationlayer.utils import(
from
rest_framework.decorators
import
action
from
rest_framework.decorators
import
action
from
app.applicationlayer.utils
import
log_save
,
enums
from
app.applicationlayer.utils
import
log_save
,
enums
from
app.applicationlayer.utils
import
model_to_dict
from
app.applicationlayer.utils
import
model_to_dict
from
django.db
import
IntegrityError
from
django.db.models
import
Q
from
app.helper.decorators
import
AssetValidation
class
AMSAssetViewSet
(
viewsets
.
ModelViewSet
):
class
AMSAssetViewSet
(
viewsets
.
ModelViewSet
):
...
@@ -33,7 +37,7 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
...
@@ -33,7 +37,7 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
.
filter
(
is_active
=
True
)
page
=
self
.
paginate_queryset
(
queryset
)
page
=
self
.
paginate_queryset
(
queryset
)
...
@@ -57,9 +61,12 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
...
@@ -57,9 +61,12 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
)
)
@
AssetValidation
@
transaction
.
atomic
@
transaction
.
atomic
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
request
.
data
form
=
request
.
data
form
[
'created_by'
]
=
request
.
user
.
code
serializer
=
self
.
get_serializer
(
data
=
form
)
serializer
=
self
.
get_serializer
(
data
=
form
)
serializer
.
is_valid
(
raise_exception
=
True
)
serializer
.
is_valid
(
raise_exception
=
True
)
self
.
perform_create
(
serializer
)
self
.
perform_create
(
serializer
)
...
@@ -73,6 +80,8 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
...
@@ -73,6 +80,8 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
message
message
)
)
@
AssetValidation
@
transaction
.
atomic
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
try
:
...
@@ -111,10 +120,80 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
...
@@ -111,10 +120,80 @@ class AMSAssetViewSet(viewsets.ModelViewSet):
def
destroy
(
self
,
request
,
*
args
,
**
kwargs
):
def
destroy
(
self
,
request
,
*
args
,
**
kwargs
):
instance
=
self
.
get_object
()
instance
=
self
.
get_object
()
self
.
perform_destroy
(
instance
)
if
instance
.
ams_asset_to_assetdetail
.
count
()
>
0
:
message
=
status_message_response
(
400
,
'Failed'
,
'This Asset had an Asset Stocks'
,
model_to_dict
(
instance
)
)
return
Response
(
message
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
instance
.
is_active
=
False
instance
.
save
()
new_instance
=
model_to_dict
(
instance
)
log_save
(
enums
.
LogEnum
.
UPDATE
.
value
,
enums
.
LogEntitiesEnum
.
AMSAsset
.
value
,
new_instance
[
'id'
],
new_instance
,
''
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
@
action
(
methods
=
[
'PATCH'
],
detail
=
True
,
url_path
=
'restore'
,
url_name
=
'restore'
)
def
restore
(
self
,
request
,
code
=
None
):
instance
=
self
.
get_object
()
instance
.
is_active
=
True
instance
.
save
()
new_instance
=
model_to_dict
(
instance
)
log_save
(
enums
.
LogEnum
.
UPDATE
.
value
,
enums
.
LogEntitiesEnum
.
AMSAsset
.
value
,
new_instance
[
'id'
],
new_instance
,
''
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
@
action
(
methods
=
[
'GET'
],
detail
=
True
,
url_path
=
'stock-list'
,
url_name
=
'stock_list'
)
def
stock_list
(
self
,
request
,
code
):
self
.
serializer_class
=
AMSAssetStockSerializer
queryset
=
self
.
get_object
()
.
ams_asset_to_assetdetail
.
all
()
print
(
queryset
)
# queryset = queryset.filter(is_active=False)
page
=
self
.
paginate_queryset
(
queryset
)
if
page
is
not
None
:
serializer
=
self
.
get_serializer
(
page
,
many
=
True
)
message
=
status_message_response
(
200
,
'success'
,
'List of Archived Assets found'
,
serializer
.
data
)
return
self
.
get_paginated_response
(
message
)
serializer
=
self
.
get_serializer
(
self
.
queryset
,
many
=
True
)
return
Response
(
serializer
.
data
,
status
=
status
.
HTTP_200_OK
)
#comment
#comment
@
action
(
@
action
(
methods
=
[
'GET'
],
detail
=
False
,
methods
=
[
'GET'
],
detail
=
False
,
...
...
app/applicationlayer/ams/asset_stock/views.py
View file @
1399876c
...
@@ -11,6 +11,11 @@ from app.applicationlayer.utils import(
...
@@ -11,6 +11,11 @@ from app.applicationlayer.utils import(
CustomPagination
,
status_message_response
CustomPagination
,
status_message_response
)
)
from
rest_framework.decorators
import
action
from
rest_framework.decorators
import
action
from
app.applicationlayer.utils
import
log_save
,
enums
from
app.applicationlayer.utils
import
model_to_dict
from
django.db
import
IntegrityError
from
django.db.models
import
Q
from
app.helper.decorators
import
AssetStockValidation
class
AMSAssetStockViewSet
(
viewsets
.
ModelViewSet
):
class
AMSAssetStockViewSet
(
viewsets
.
ModelViewSet
):
...
@@ -33,7 +38,7 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
...
@@ -33,7 +38,7 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
.
filter
(
is_active
=
True
)
page
=
self
.
paginate_queryset
(
queryset
)
page
=
self
.
paginate_queryset
(
queryset
)
...
@@ -56,7 +61,39 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
...
@@ -56,7 +61,39 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
status
=
status
.
HTTP_200_OK
status
=
status
.
HTTP_200_OK
)
)
@
AssetStockValidation
@
transaction
.
atomic
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
partial
=
kwargs
.
pop
(
'partial'
,
False
)
instance
=
self
.
get_object
()
form
=
request
.
data
form
[
'created_by'
]
=
request
.
user
.
code
serializer
=
self
.
get_serializer
(
instance
,
data
=
form
,
partial
=
partial
)
serializer
.
is_valid
(
raise_exception
=
True
)
old_instance
=
model_to_dict
(
instance
)
self
.
perform_update
(
serializer
)
new_instance
=
serializer
.
data
log_save
(
enums
.
LogEnum
.
UPDATE
.
value
,
enums
.
LogEntitiesEnum
.
AMSReport
.
value
,
old_instance
[
'id'
],
old_instance
,
new_instance
)
return
Response
(
serializer
.
data
)
except
IntegrityError
as
e
:
return
Response
(
{
"message"
:
"Cannot delete or update this reocrd it has foreign key constraint to other tables"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
@
AssetStockValidation
@
transaction
.
atomic
@
transaction
.
atomic
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
def
create
(
self
,
request
,
*
args
,
**
kwargs
):
...
@@ -83,7 +120,17 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
...
@@ -83,7 +120,17 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
def
destroy
(
self
,
request
,
*
args
,
**
kwargs
):
def
destroy
(
self
,
request
,
*
args
,
**
kwargs
):
instance
=
self
.
get_object
()
instance
=
self
.
get_object
()
self
.
perform_destroy
(
instance
)
instance
.
is_active
=
False
instance
.
save
()
new_instance
=
model_to_dict
(
instance
)
log_save
(
enums
.
LogEnum
.
UPDATE
.
value
,
enums
.
LogEntitiesEnum
.
AMSAssetStock
.
value
,
new_instance
[
'id'
],
new_instance
,
''
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
...
@@ -116,3 +163,24 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
...
@@ -116,3 +163,24 @@ class AMSAssetStockViewSet(viewsets.ModelViewSet):
serializer
.
data
,
serializer
.
data
,
status
=
status
.
HTTP_200_OK
status
=
status
.
HTTP_200_OK
)
)
@
action
(
methods
=
[
'PATCH'
],
detail
=
True
,
url_path
=
'restore'
,
url_name
=
'restore'
)
def
restore
(
self
,
request
,
code
=
None
):
instance
=
self
.
get_object
()
instance
.
is_active
=
True
instance
.
save
()
new_instance
=
model_to_dict
(
instance
)
log_save
(
enums
.
LogEnum
.
UPDATE
.
value
,
enums
.
LogEntitiesEnum
.
AMSAsset
.
value
,
new_instance
[
'id'
],
new_instance
,
''
)
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
app/helper/decorators.py
View file @
1399876c
...
@@ -18,6 +18,7 @@ from app.businesslayer.changerequest.change_request_template import (
...
@@ -18,6 +18,7 @@ from app.businesslayer.changerequest.change_request_template import (
validation_vendor_unique_level
validation_vendor_unique_level
)
)
from
app.applicationlayer.utils
import
error_message
from
app.applicationlayer.utils
import
error_message
from
app.entities
import
models
def
error_safe
(
function
):
def
error_safe
(
function
):
...
@@ -414,3 +415,145 @@ def FormValidation(function):
...
@@ -414,3 +415,145 @@ def FormValidation(function):
return
function
(
self
,
request
,
*
args
,
**
kwargs
)
return
function
(
self
,
request
,
*
args
,
**
kwargs
)
return
wrapper
return
wrapper
def
AssetValidation
(
function
):
@
wraps
(
function
)
def
wrapper
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
request
.
data
if
'name'
not
in
form
:
return
Response
(
{
"message"
:
"The key name is required"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
if
len
(
form
[
'name'
])
==
0
:
return
Response
(
{
"message"
:
"Asset name is required"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
check_existing
=
models
.
AMSAsset
.
objects
.
filter
(
name
=
form
[
'name'
])
.
count
()
if
check_existing
>=
1
:
return
Response
(
{
"message"
:
"Asset with this name already exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
if
'asset_type'
not
in
form
:
return
Response
(
{
"message"
:
"The key asset_type is required"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
valid_asset_type
=
models
.
AMSAssetType
.
objects
.
filter
(
Q
(
code
=
form
[
'asset_type'
])
&
Q
(
is_active
=
True
)
)
.
count
()
if
valid_asset_type
<=
0
:
return
Response
(
{
"message"
:
"Invalid Asset Type"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
function
(
self
,
request
,
*
args
,
**
kwargs
)
return
wrapper
def
AssetStockValidation
(
function
):
@
wraps
(
function
)
def
wrapper
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
request
.
data
fields
=
[
"asset_type"
]
fields
.
append
(
"unique_identifier"
)
fields
.
append
(
"status"
)
fields
.
append
(
"acquisition_type"
)
fields
.
append
(
"location"
)
fields
.
append
(
"user_client"
)
fields
.
append
(
"manager"
)
fields
.
append
(
"dynamic_field"
)
fields
.
append
(
"asset_group"
)
fields
.
append
(
"asset"
)
print
(
fields
)
for
data
in
fields
:
if
str
(
data
)
not
in
form
:
return
Response
(
{
"message"
:
f
"The key {data.replace('_', ' ')} is required"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
for
data
in
form
.
keys
():
if
str
(
data
)
!=
'dynamic_field'
:
if
len
(
data
)
==
0
:
return
Response
(
{
"message"
:
f
"{data.replace('_', ' ')} is required"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
unique_identifier
=
models
.
AMSAssetStock
.
objects
.
filter
(
unique_identifier
=
form
[
'unique_identifier'
]
)
.
count
()
if
unique_identifier
>=
1
:
return
Response
(
{
"message"
:
"unique identifier already exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
user_client
=
models
.
User
.
objects
.
filter
(
code
=
form
[
'user_client'
]
)
.
count
()
if
user_client
<=
0
:
return
Response
(
{
"message"
:
"User does not exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
manager
=
models
.
User
.
objects
.
filter
(
code
=
form
[
'manager'
]
)
.
count
()
if
manager
<=
0
:
return
Response
(
{
"message"
:
"User for manager does not exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
asset
=
models
.
AMSAsset
.
objects
.
filter
(
code
=
form
[
'asset'
]
)
.
count
()
if
asset
<=
0
:
return
Response
(
{
"message"
:
"Asset does not exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
asset_group
=
models
.
AMSAssetGroup
.
objects
.
filter
(
code
=
form
[
'asset_group'
]
)
.
count
()
if
asset_group
<=
0
:
return
Response
(
{
"message"
:
"Asset Group does not exists."
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
valid_asset_type
=
models
.
AMSAssetType
.
objects
.
filter
(
Q
(
code
=
form
[
'asset_type'
])
&
Q
(
is_active
=
True
)
)
.
count
()
if
valid_asset_type
<=
0
:
return
Response
(
{
"message"
:
"Invalid Asset Type"
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
function
(
self
,
request
,
*
args
,
**
kwargs
)
return
wrapper
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