Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions onboarding/migrations/0008_files_for_page_of_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.1.10 on 2021-08-25 11:34

from django.db import migrations, models
import django.db.models.deletion
import onboarding.models


class Migration(migrations.Migration):

dependencies = [
('onboarding', '0007_model_listing_sessions_for_users'),
]

operations = [
migrations.CreateModel(
name='PageFile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('data_file', models.FileField(upload_to=onboarding.models.page_file_upload_to, validators=[onboarding.models.validate_file_extension])),
('name', models.CharField(max_length=100)),
('content_type', models.CharField(blank=True, max_length=200, null=True)),
('size', models.DecimalField(decimal_places=2, max_digits=6)),
('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onboarding.company')),
('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onboarding.page')),
],
),
]
65 changes: 65 additions & 0 deletions onboarding/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.core.exceptions import ValidationError


def upload_to(instance, filename):
Expand All @@ -20,6 +21,39 @@ def upload_to(instance, filename):
return f"users/{instance.pk}/{now:%Y%m%d%H%M%S}{milliseconds}{extension}"


def page_file_upload_to(instance, filename):
"""
:param instance: object of PageFile model
:param filename: name (with extension) of the file being uploaded
"""
company_id = instance.company
return f"company/{company_id}/{instance.page}/{filename}"


def validate_file_extension(value):
# valid_extensions = ['.doc', '.docx', '.pdf', '.jpg', '.xls', '.pptx']
content_type = ""
try:
if content_type in value.file:
content_type = value.file.content_type
else:
content_type = value.content_type

whitelist = ['application/msword', 'application/pdf', 'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/rtf', 'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'text/plain',
'image/png', 'image/bmp', 'image/gif', 'image/jpe', 'image/jpeg', 'image/jpeg', 'image/svg+xml', 'image/x-icon']
except (AttributeError, KeyError):
content_type = os.path.splitext(value.name)[1]
whitelist = ['.doc', '.docx', '.pdf', '.xls', '.pptx', '.jpg', '.png', '.gif', '.txt']

if not content_type in whitelist:
raise ValidationError('Unsupported file extension.')


class Company(models.Model):

company_logo = models.ImageField(upload_to=upload_to, null=True, blank=True)
Expand Down Expand Up @@ -174,6 +208,37 @@ def __str__(self):
return self.title


class PageFile(models.Model):
"""
Stores files for respective Page (many-to-one).
data_file - the file which is to be storred
name - keeps original name of the file
content_type - content type of data_file guessed by file extension by DJango
company - Company that owns this file
size - number of kilobytes of uploaded file
"""
data_file = models.FileField(upload_to=page_file_upload_to, validators=[validate_file_extension])
page = models.ForeignKey(Page, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
content_type = models.CharField(max_length=200, null=True, blank=True)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
# description = models.TextField(max_length=700, help_text='Enter a description about file', null=True, blank=True)
size = models.DecimalField(max_digits=6, decimal_places=2)
# updated_on = models.DateTimeField(auto_now=True)

# class Meta:
# constraints = [
# models.UniqueConstraint(fields=['data_file', 'company'], name='unique file for each company')
# ]

def delete(self):
self.data_file.storage.delete(self.data_file.name)
super().delete()

def __str__(self):
return f"file: {self.name}"


class Section(models.Model):
"""
Owner - company where the HR/user who created the section is employed
Expand Down
40 changes: 39 additions & 1 deletion onboarding/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers
from django.contrib.auth import get_user_model
from onboarding.models import ContactRequestDetail, Package, Page, Section, User, PackagesUsers
from onboarding.models import ContactRequestDetail, Package, Page, PageFile, Section, User, PackagesUsers
from onboarding.models import Answer, Company, CompanyQuestionAndAnswer
from . import mock_password

Expand Down Expand Up @@ -291,6 +291,44 @@ class Meta:
}


# FILES for PAGE
class PageFileSerializer(serializers.ModelSerializer):
class Meta:
model = PageFile
fields = '__all__'
read_only_fields = ('name', 'content_type', 'company', 'size')

def validate(self, validated_data):
try:
validated_data['content_type'] = validated_data['data_file'].content_type
except (KeyError, AttributeError):
pass
validated_data['name'] = validated_data['data_file'].name
validated_data['size'] = validated_data['data_file'].size / 1024.0
return validated_data


#class PageFileMetaDataSerializer(serializers.ModelSerializer):
# class Meta:
# model = PageFile
# fields = (
# 'id',
# 'page',
# 'name',
# 'company',
# 'description',
# 'size'
# )
# read_only_fields = ('company', 'size')


class PageDataFileSerializer(serializers.ModelSerializer):
class Meta:
model = PageFile
fields = ('data_file',)
read_only_fields = ('data_file',) # [f.name for f in PageFile._meta.get_fields()]


# PACKAGE with PAGEs
class PackagePagesSerializer(serializers.ModelSerializer):
page_set = PageSerializer(many=True)
Expand Down
Loading