Commit a1b2a762 authored by John Red Medrano's avatar John Red Medrano

added attachment endpoint

parent a3cb719a
from rest_framework import serializers
from app.entities import models
class MasterAttachmentSerializer(serializers.ModelSerializer):
class Meta:
model = models.MasterAttachment
fields = '__all__'
read_only = ('uploaded_by', 'created', 'code',)
import copy
from rest_framework import status, viewsets
from rest_framework.response import Response
from app.entities.models import MasterAttachment
from app.applicationlayer.master.attachment.serializer import MasterAttachmentSerializer
from app.applicationlayer.utils import CustomPagination, status_message_response
from django.db import transaction
from rest_framework.exceptions import ValidationError
class MasterAttachmentViewSet(viewsets.ModelViewSet):
queryset = MasterAttachment.objects.all()
serializer_class = MasterAttachmentSerializer
pagination_class = CustomPagination
# @decorators.rms.user_list
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
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 Attachment found',
serializer.data
)
return self.get_paginated_response(message)
serializer = self.get_serializer(queryset, many=True)
return Response(
serializer.data,
status=status.HTTP_200_OK
)
@transaction.atomic
def create(self, request, *args, **kwargs):
ids = []
for instance in self.request.data.getlist('url'):
data = MasterAttachment.objects.create(url=instance)
ids.append(data.id)
return Response(
{"ids": ids},
status=status.HTTP_201_CREATED
)
......@@ -5,11 +5,13 @@ from app.applicationlayer.master.Account.views import AdminAccountViewSet
from app.applicationlayer.master.company.views import AdminCompanyViewSet
from app.applicationlayer.master.department.views import AdminDepartmentViewSet
from app.applicationlayer.master.user_type.views import UserTypeViewSet
from app.applicationlayer.master.attachment.views import MasterAttachmentViewSet
router = routers.DefaultRouter()
router.register(r'users', AdminAccountViewSet)
router.register(r'companies', AdminCompanyViewSet)
router.register(r'departments', AdminDepartmentViewSet)
router.register(r'attachments', MasterAttachmentViewSet)
# router.register(r'user-types', UserTypeViewSet)
urlpatterns = [
......
# Generated by Django 2.2 on 2019-09-26 10:40
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('entities', '0007_auto_20190924_1206'),
]
operations = [
migrations.CreateModel(
name='MasterAttachment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.FileField(blank=True, null=True, upload_to='uploads/')),
],
options={
'db_table': 'master_attachments',
},
),
migrations.AlterField(
model_name='allowedcompany',
name='company_pivot',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='allowed_company_company_pivot', to='entities.Company', to_field='code'),
),
migrations.AlterField(
model_name='allowedcompany',
name='group_pivots',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='allowed_company_group_pivots', to='entities.Department', to_field='code'),
),
migrations.AlterField(
model_name='allowedcompany',
name='id_number',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='allowed_company_id_number', to=settings.AUTH_USER_MODEL, to_field='code'),
),
migrations.AlterField(
model_name='changerequestformattachments',
name='file_upload',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='form_attachments', to='entities.MasterAttachment'),
),
migrations.AlterField(
model_name='changerequestformattachments',
name='tmp_attach',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='template_attachments', to='entities.ChangeRequestTemplateAttachments', to_field='code'),
),
migrations.AlterField(
model_name='changerequesttemplateattachments',
name='file_upload',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='template_attachments', to='entities.MasterAttachment'),
),
]
......@@ -435,6 +435,16 @@ class BaseStakeholder(models.Model):
abstract = True
class MasterAttachment(models.Model):
url = models.FileField(
upload_to='uploads/',
blank=True,
null=True)
class Meta:
db_table = 'master_attachments'
class BaseAttachment(models.Model):
attachment_type = models.CharField(max_length=255)
attachment_name = models.CharField(max_length=255)
......@@ -451,10 +461,6 @@ class BaseAttachment(models.Model):
User,
on_delete=models.DO_NOTHING,
to_field='code')
file_upload = models.FileField(
upload_to='uploads/',
blank=True,
null=True)
created = models.DateTimeField(
blank=True,
null=True)
......@@ -580,6 +586,13 @@ class ChangeRequestTemplateAttachments(BaseAttachment):
code = models.CharField(
max_length=255,
unique=True)
file_upload = models.ForeignKey(
MasterAttachment,
on_delete=models.PROTECT,
related_name='template_attachments',
blank=True,
null=True
)
template_no = models.ForeignKey(
ChangeRequestTemplateHeader,
on_delete=models.DO_NOTHING,
......@@ -816,10 +829,18 @@ class ChangeRequestFormAttachments(BaseAttachment):
on_delete=models.DO_NOTHING,
to_field='form_code',
related_name='frm_attachments')
file_upload = models.ForeignKey(
MasterAttachment,
on_delete=models.PROTECT,
related_name='form_attachments',
blank=True,
null=True
)
tmp_attach = models.ForeignKey(
ChangeRequestTemplateAttachments,
null=True,
blank=True,
related_name="template_attachments",
on_delete=models.DO_NOTHING,
to_field='code')
......
......@@ -14,9 +14,14 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import path, include, re_path
from django.conf.urls import url
from app.applicationlayer.management.notification import views as notifview
from django.views.static import serve
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
......@@ -28,4 +33,12 @@ urlpatterns = [
path('api/v1/master/', include('app.applicationlayer.master.urls')),
url(r'^chat/$', notifview.index, name='index'),
url(r'^chat/(?P<room_name>[^/]+)/$', notifview.room, name='room'),
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment