new file system updates
This commit is contained in:
parent
d4e332456b
commit
061aefd233
@ -1,13 +1,12 @@
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
from gettext import gettext as _
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet, Sum
|
||||||
from drf_writable_nested import (UniqueFieldsMixin,
|
from drf_writable_nested import (UniqueFieldsMixin,
|
||||||
WritableNestedModelSerializer)
|
WritableNestedModelSerializer)
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.exceptions import ValidationError, NotAuthenticated, NotFound, ParseError
|
from rest_framework.exceptions import ValidationError, NotFound
|
||||||
from rest_framework.fields import ModelField
|
|
||||||
from rest_framework.serializers import BaseSerializer, Serializer
|
|
||||||
|
|
||||||
from cookbook.models import (Comment, CookLog, Food, Ingredient, Keyword,
|
from cookbook.models import (Comment, CookLog, Food, Ingredient, Keyword,
|
||||||
MealPlan, MealType, NutritionInformation, Recipe,
|
MealPlan, MealType, NutritionInformation, Recipe,
|
||||||
@ -105,11 +104,28 @@ class UserPreferenceSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class UserFileSerializer(serializers.ModelSerializer):
|
class UserFileSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
def check_file_limit(self, validated_data):
|
||||||
|
if self.context['request'].space.max_file_storage_mb == -1:
|
||||||
|
raise ValidationError(_('File uploads are not enabled for this Space.'))
|
||||||
|
|
||||||
|
try:
|
||||||
|
current_file_size_mb = UserFile.objects.filter(space=self.context['request'].space).aggregate(Sum('file_size_kb'))['file_size_kb__sum'] / 1000
|
||||||
|
except TypeError:
|
||||||
|
current_file_size_mb = 0
|
||||||
|
|
||||||
|
if (validated_data['file'].size / 1000 / 1000 + current_file_size_mb - 5) > self.context['request'].space.max_file_storage_mb != 0:
|
||||||
|
raise ValidationError(_('You have reached your file upload limit.'))
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
|
self.check_file_limit(validated_data)
|
||||||
validated_data['created_by'] = self.context['request'].user
|
validated_data['created_by'] = self.context['request'].user
|
||||||
validated_data['space'] = self.context['request'].space
|
validated_data['space'] = self.context['request'].space
|
||||||
return super().create(validated_data)
|
return super().create(validated_data)
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
self.check_file_limit(validated_data)
|
||||||
|
return super().update(instance, validated_data)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UserFile
|
model = UserFile
|
||||||
fields = ('name', 'file', 'file_size_kb', 'id',)
|
fields = ('name', 'file', 'file_size_kb', 'id',)
|
||||||
@ -117,6 +133,20 @@ class UserFileSerializer(serializers.ModelSerializer):
|
|||||||
extra_kwargs = {"file": {"required": False, }}
|
extra_kwargs = {"file": {"required": False, }}
|
||||||
|
|
||||||
|
|
||||||
|
class UserFileViewSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
raise ValidationError('Cannot create File over this view')
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
return instance
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = UserFile
|
||||||
|
fields = ('name', 'file', 'id',)
|
||||||
|
read_only_fields = ('id', 'file')
|
||||||
|
|
||||||
|
|
||||||
class StorageSerializer(SpacedModelSerializer):
|
class StorageSerializer(SpacedModelSerializer):
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
@ -265,7 +295,7 @@ class StepSerializer(WritableNestedModelSerializer):
|
|||||||
ingredients = IngredientSerializer(many=True)
|
ingredients = IngredientSerializer(many=True)
|
||||||
ingredients_markdown = serializers.SerializerMethodField('get_ingredients_markdown')
|
ingredients_markdown = serializers.SerializerMethodField('get_ingredients_markdown')
|
||||||
ingredients_vue = serializers.SerializerMethodField('get_ingredients_vue')
|
ingredients_vue = serializers.SerializerMethodField('get_ingredients_vue')
|
||||||
file = UserFileSerializer(allow_null=True)
|
file = UserFileViewSerializer(allow_null=True)
|
||||||
|
|
||||||
def get_ingredients_vue(self, obj):
|
def get_ingredients_vue(self, obj):
|
||||||
return obj.get_instruction_render()
|
return obj.get_instruction_render()
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -598,9 +598,7 @@
|
|||||||
this.sortSteps()
|
this.sortSteps()
|
||||||
for (let s of this.recipe.steps) {
|
for (let s of this.recipe.steps) {
|
||||||
this.sortIngredients(s)
|
this.sortIngredients(s)
|
||||||
if (s.file !== null){
|
|
||||||
delete s.file.file //TODO stupid quick hack because I cant figure out how to fix the writable serializer right now
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.$http.put("{% url 'api:recipe-detail' recipe.pk %}", this.recipe,
|
this.$http.put("{% url 'api:recipe-detail' recipe.pk %}", this.recipe,
|
||||||
{}).then((response) => {
|
{}).then((response) => {
|
||||||
|
@ -495,6 +495,7 @@ class BookmarkletImportViewSet(viewsets.ModelViewSet):
|
|||||||
return self.queryset.filter(space=self.request.space).all()
|
return self.queryset.filter(space=self.request.space).all()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class UserFileViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class UserFileViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
queryset = UserFile.objects
|
queryset = UserFile.objects
|
||||||
serializer_class = UserFileSerializer
|
serializer_class = UserFileSerializer
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th>{{ $t('Name') }}</th>
|
<th>{{ $t('Name') }}</th>
|
||||||
<th>{{ $t('Size') }} (MB)</th>
|
<th>{{ $t('Size') }} (MB)</th>
|
||||||
|
<th>{{ $t('Download') }}</th>
|
||||||
<th>{{ $t('Edit') }}</th>
|
<th>{{ $t('Edit') }}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -37,7 +38,9 @@
|
|||||||
<td>{{ f.file_size_kb / 1000 }}</td>
|
<td>{{ f.file_size_kb / 1000 }}</td>
|
||||||
<td><a :href="f.file" target="_blank" rel="noreferrer nofollow">{{ $t('Download') }}</a></td>
|
<td><a :href="f.file" target="_blank" rel="noreferrer nofollow">{{ $t('Download') }}</a></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
<file-editor @change="loadInitial()" :file_id="f.id"></file-editor>
|
<file-editor @change="loadInitial()" :file_id="f.id"></file-editor>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
@ -10,10 +10,26 @@
|
|||||||
<template v-if="file!==undefined">
|
<template v-if="file!==undefined">
|
||||||
{{ $t('Name') }}
|
{{ $t('Name') }}
|
||||||
<b-input v-model="file.name"></b-input>
|
<b-input v-model="file.name"></b-input>
|
||||||
|
<br/>
|
||||||
{{ $t('File') }}
|
{{ $t('File') }}
|
||||||
<b-form-file v-model="file.file"></b-form-file>
|
<b-form-file v-model="file.file"></b-form-file>
|
||||||
</template>
|
</template>
|
||||||
|
<br/>
|
||||||
|
<br/>
|
||||||
|
<template #modal-footer="">
|
||||||
|
<b-button size="sm" variant="success" @click="modalOk()">
|
||||||
|
{{ $t('Ok') }}
|
||||||
|
</b-button>
|
||||||
|
|
||||||
|
<b-button size="sm" variant="secondary" @click="$bvModal.hide('modal-file-editor'+file_id)">
|
||||||
|
{{ $t('Cancel') }}
|
||||||
|
</b-button>
|
||||||
|
|
||||||
|
<b-button size="sm" variant="danger" @click="deleteFile()">
|
||||||
|
{{ $t('Delete') }}
|
||||||
|
</b-button>
|
||||||
|
</template>
|
||||||
|
|
||||||
</b-modal>
|
</b-modal>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -66,13 +82,12 @@ export default {
|
|||||||
if (!(typeof this.file.file === 'string' || this.file.file instanceof String)) { // only update file if it was changed
|
if (!(typeof this.file.file === 'string' || this.file.file instanceof String)) { // only update file if it was changed
|
||||||
passedFile = this.file.file
|
passedFile = this.file.file
|
||||||
}
|
}
|
||||||
console.log(passedFile)
|
|
||||||
|
|
||||||
apiClient.updateUserFile(this.file.id, this.file.name, passedFile).then(request => {
|
apiClient.updateUserFile(this.file.id, this.file.name, passedFile).then(request => {
|
||||||
makeToast(this.$t('Success'), this.$t('success_updating_resource'), 'success')
|
makeToast(this.$t('Success'), this.$t('success_updating_resource'), 'success')
|
||||||
this.$emit('change',)
|
this.$emit('change',)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
makeToast(this.$t('Error'), this.$t('err_updating_resource'), 'danger')
|
makeToast(this.$t('Error'), this.$t('err_updating_resource') + '\n' + err.response.data, 'danger')
|
||||||
console.log(err.request, err.response)
|
console.log(err.request, err.response)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -81,10 +96,24 @@ export default {
|
|||||||
|
|
||||||
apiClient.createUserFile(this.file.name, this.file.file).then(request => {
|
apiClient.createUserFile(this.file.name, this.file.file).then(request => {
|
||||||
makeToast(this.$t('Success'), this.$t('success_creating_resource'), 'success')
|
makeToast(this.$t('Success'), this.$t('success_creating_resource'), 'success')
|
||||||
|
this.$emit('change',)
|
||||||
|
this.file = {
|
||||||
|
name: '',
|
||||||
|
file: undefined
|
||||||
|
}
|
||||||
|
}).catch(err => {
|
||||||
|
makeToast(this.$t('Error'), this.$t('err_creating_resource') + '\n' + err.response.data, 'danger')
|
||||||
|
console.log(err.request, err.response)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
deleteFile: function () {
|
||||||
|
let apiClient = new ApiApiFactory()
|
||||||
|
|
||||||
|
apiClient.destroyUserFile(this.file.id).then(results => {
|
||||||
|
makeToast(this.$t('Success'), this.$t('success_deleting_resource'), 'success')
|
||||||
this.$emit('change',)
|
this.$emit('change',)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
makeToast(this.$t('Error'), this.$t('err_creating_resource'), 'danger')
|
makeToast(this.$t('Error'), this.$t('err_deleting_resource') + '\n' + err.response.data, 'danger')
|
||||||
console.log(err.request, err.response)
|
console.log(err.request, err.response)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,11 @@
|
|||||||
"err_fetching_resource": "There was an error fetching a resource!",
|
"err_fetching_resource": "There was an error fetching a resource!",
|
||||||
"err_creating_resource": "There was an error creating a resource!",
|
"err_creating_resource": "There was an error creating a resource!",
|
||||||
"err_updating_resource": "There was an error updating a resource!",
|
"err_updating_resource": "There was an error updating a resource!",
|
||||||
|
"err_deleting_resource": "There was an error deleting a resource!",
|
||||||
"success_fetching_resource": "Successfully fetched a resource!",
|
"success_fetching_resource": "Successfully fetched a resource!",
|
||||||
"success_creating_resource": "Successfully created a resource!",
|
"success_creating_resource": "Successfully created a resource!",
|
||||||
"success_updating_resource": "Successfully updated a resource!",
|
"success_updating_resource": "Successfully updated a resource!",
|
||||||
|
"success_deleting_resource": "Successfully deleted a resource!",
|
||||||
|
|
||||||
"import_running": "Import running, please wait!",
|
"import_running": "Import running, please wait!",
|
||||||
"all_fields_optional": "All fields are optional and can be left empty.",
|
"all_fields_optional": "All fields are optional and can be left empty.",
|
||||||
@ -60,7 +62,10 @@
|
|||||||
"Files": "Files",
|
"Files": "Files",
|
||||||
"File": "File",
|
"File": "File",
|
||||||
"Edit": "Edit",
|
"Edit": "Edit",
|
||||||
|
"Cancel": "Cancel",
|
||||||
|
"Delete": "Delete",
|
||||||
"Open": "Open",
|
"Open": "Open",
|
||||||
|
"Ok": "Open",
|
||||||
"Save": "Save",
|
"Save": "Save",
|
||||||
"Step": "Step",
|
"Step": "Step",
|
||||||
"Search": "Search",
|
"Search": "Search",
|
||||||
|
Loading…
Reference in New Issue
Block a user