mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
fixed file upload
This commit is contained in:
@@ -37,7 +37,7 @@ def get_filetype(name):
|
|||||||
|
|
||||||
def is_file_type_allowed(filename, image_only=False):
|
def is_file_type_allowed(filename, image_only=False):
|
||||||
is_file_allowed = False
|
is_file_allowed = False
|
||||||
allowed_file_types = ['.pdf', '.docx', '.xlsx']
|
allowed_file_types = ['.pdf', '.docx', '.xlsx', '.css']
|
||||||
allowed_image_types = ['.png', '.jpg', '.jpeg', '.gif', '.webp']
|
allowed_image_types = ['.png', '.jpg', '.jpeg', '.gif', '.webp']
|
||||||
check_list = allowed_image_types
|
check_list = allowed_image_types
|
||||||
if not image_only:
|
if not image_only:
|
||||||
|
|||||||
@@ -268,19 +268,24 @@ class UserFileSerializer(serializers.ModelSerializer):
|
|||||||
> self.context['request'].space.max_file_storage_mb != 0):
|
> self.context['request'].space.max_file_storage_mb != 0):
|
||||||
raise ValidationError(_('You have reached your file upload limit.'))
|
raise ValidationError(_('You have reached your file upload limit.'))
|
||||||
|
|
||||||
def create(self, validated_data):
|
def check_file_type(self, validated_data):
|
||||||
if not is_file_type_allowed(validated_data['file'].name):
|
print('checking file type')
|
||||||
return None
|
if 'file' in validated_data:
|
||||||
|
print('filke present in data')
|
||||||
|
if not is_file_type_allowed(validated_data['file'].name, image_only=False):
|
||||||
|
print('is not allowed')
|
||||||
|
raise ValidationError(_('The given file type is not allowed.'))
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
self.check_file_limit(validated_data)
|
self.check_file_limit(validated_data)
|
||||||
|
self.check_file_type(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):
|
def update(self, instance, validated_data):
|
||||||
if not is_file_type_allowed(validated_data['file'].name):
|
|
||||||
return None
|
|
||||||
self.check_file_limit(validated_data)
|
self.check_file_limit(validated_data)
|
||||||
|
self.check_file_type(validated_data)
|
||||||
return super().update(instance, validated_data)
|
return super().update(instance, validated_data)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -456,7 +461,7 @@ class ConnectorConfigSerializer(SpacedModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = ConnectorConfig
|
model = ConnectorConfig
|
||||||
fields = (
|
fields = (
|
||||||
'id', 'name', 'type','url', 'token', 'todo_entity', 'enabled',
|
'id', 'name', 'type', 'url', 'token', 'todo_entity', 'enabled',
|
||||||
'on_shopping_list_entry_created_enabled', 'on_shopping_list_entry_updated_enabled',
|
'on_shopping_list_entry_created_enabled', 'on_shopping_list_entry_updated_enabled',
|
||||||
'on_shopping_list_entry_deleted_enabled', 'supports_description_field', 'created_by'
|
'on_shopping_list_entry_deleted_enabled', 'supports_description_field', 'created_by'
|
||||||
)
|
)
|
||||||
@@ -481,7 +486,7 @@ class StorageSerializer(WritableNestedModelSerializer, SpacedModelSerializer):
|
|||||||
'token', 'url', 'path', 'created_by'
|
'token', 'url', 'path', 'created_by'
|
||||||
)
|
)
|
||||||
|
|
||||||
read_only_fields = ( 'id', 'created_by',)
|
read_only_fields = ('id', 'created_by',)
|
||||||
|
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'password': {'write_only': True},
|
'password': {'write_only': True},
|
||||||
|
|||||||
@@ -79,11 +79,20 @@ onMounted(() => {
|
|||||||
* save file to database via fileApi composable
|
* save file to database via fileApi composable
|
||||||
*/
|
*/
|
||||||
function saveFile() {
|
function saveFile() {
|
||||||
|
loading.value = true
|
||||||
|
|
||||||
|
let event: ("create" | "save") = isUpdate() ? 'save' : 'create'
|
||||||
|
|
||||||
createOrUpdateUserFile(editingObj.value.name, file.value, editingObj.value.id).then(r => {
|
createOrUpdateUserFile(editingObj.value.name, file.value, editingObj.value.id).then(r => {
|
||||||
editingObj.value = r
|
editingObj.value = r
|
||||||
|
editingObjChanged.value = false
|
||||||
|
emit(event, r)
|
||||||
useMessageStore().addPreparedMessage(PreparedMessage.UPDATE_SUCCESS)
|
useMessageStore().addPreparedMessage(PreparedMessage.UPDATE_SUCCESS)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
|
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
|
||||||
|
}).finally(() => {
|
||||||
|
editingObjChanged.value = false
|
||||||
|
loading.value = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {useDjangoUrls} from "@/composables/useDjangoUrls";
|
import {useDjangoUrls} from "@/composables/useDjangoUrls";
|
||||||
import {ref} from "vue";
|
import {ref} from "vue";
|
||||||
import {getCookie} from "@/utils/cookie";
|
import {getCookie} from "@/utils/cookie";
|
||||||
import {RecipeFromSourceResponseFromJSON, RecipeImageFromJSON, UserFile, UserFileFromJSON} from "@/openapi";
|
import {RecipeFromSourceResponseFromJSON, RecipeImageFromJSON, ResponseError, UserFile, UserFileFromJSON} from "@/openapi";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,9 +40,13 @@ export function useFileApi() {
|
|||||||
headers: {'X-CSRFToken': getCookie('csrftoken')},
|
headers: {'X-CSRFToken': getCookie('csrftoken')},
|
||||||
body: formData
|
body: formData
|
||||||
}).then(r => {
|
}).then(r => {
|
||||||
return r.json().then(r => {
|
if (r.ok) {
|
||||||
return UserFileFromJSON(r)
|
return r.json().then(r => {
|
||||||
})
|
return UserFileFromJSON(r)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
throw new ResponseError(r)
|
||||||
|
}
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
fileApiLoading.value = false
|
fileApiLoading.value = false
|
||||||
})
|
})
|
||||||
@@ -81,10 +85,10 @@ export function useFileApi() {
|
|||||||
* @param file file object to upload
|
* @param file file object to upload
|
||||||
* @param text text to import
|
* @param text text to import
|
||||||
*/
|
*/
|
||||||
function doAiImport(file: File|null, text: string = '') {
|
function doAiImport(file: File | null, text: string = '') {
|
||||||
let formData = new FormData()
|
let formData = new FormData()
|
||||||
|
|
||||||
if(file != null){
|
if (file != null) {
|
||||||
formData.append('file', file)
|
formData.append('file', file)
|
||||||
} else {
|
} else {
|
||||||
formData.append('file', '')
|
formData.append('file', '')
|
||||||
|
|||||||
@@ -21,7 +21,9 @@
|
|||||||
<v-btn class="float-right" icon="$create" color="create" v-if="!genericModel.model.disableCreate">
|
<v-btn class="float-right" icon="$create" color="create" v-if="!genericModel.model.disableCreate">
|
||||||
<i class="fa-solid fa-plus"></i>
|
<i class="fa-solid fa-plus"></i>
|
||||||
<model-edit-dialog :close-after-create="false" :model="model"
|
<model-edit-dialog :close-after-create="false" :model="model"
|
||||||
@create="loadItems({page: tablePage, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery})"></model-edit-dialog>
|
@create="loadItems({page: tablePage, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery})"
|
||||||
|
@save="loadItems({page: tablePage, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery})"
|
||||||
|
@delete="loadItems({page: tablePage, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery})"></model-edit-dialog>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</template>
|
</template>
|
||||||
<v-card-actions v-if="genericModel.model.name == 'RecipeImport'">
|
<v-card-actions v-if="genericModel.model.name == 'RecipeImport'">
|
||||||
@@ -51,7 +53,8 @@
|
|||||||
<v-icon icon="$menu"></v-icon>
|
<v-icon icon="$menu"></v-icon>
|
||||||
<v-menu activator="parent" close-on-content-click>
|
<v-menu activator="parent" close-on-content-click>
|
||||||
<v-list density="compact">
|
<v-list density="compact">
|
||||||
<v-list-item prepend-icon="$edit" :to="{name: 'ModelEditPage', params: {model: model, id: item.id}}" v-if="!genericModel.model.disableCreate && !genericModel.model.disableUpdate && !genericModel.model.disableDelete" >
|
<v-list-item prepend-icon="$edit" :to="{name: 'ModelEditPage', params: {model: model, id: item.id}}"
|
||||||
|
v-if="!genericModel.model.disableCreate && !genericModel.model.disableUpdate && !genericModel.model.disableDelete">
|
||||||
{{ $t('Edit') }}
|
{{ $t('Edit') }}
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
<v-list-item prepend-icon="fa-solid fa-arrows-to-dot" v-if="genericModel.model.isMerge" link>
|
<v-list-item prepend-icon="fa-solid fa-arrows-to-dot" v-if="genericModel.model.isMerge" link>
|
||||||
@@ -71,7 +74,7 @@
|
|||||||
{{ $t('Import') }}
|
{{ $t('Import') }}
|
||||||
<sync-dialog :sync="item"></sync-dialog>
|
<sync-dialog :sync="item"></sync-dialog>
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
<v-list-item prepend-icon="fa-solid fa-rotate" v-if="genericModel.model.name == 'RecipeImport'" @click="importRecipe(item)">
|
<v-list-item prepend-icon="fa-solid fa-rotate" v-if="genericModel.model.name == 'RecipeImport'" @click="importRecipe(item)">
|
||||||
{{ $t('Import') }}
|
{{ $t('Import') }}
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
</v-list>
|
</v-list>
|
||||||
@@ -209,8 +212,8 @@ function changeModel(m: Model) {
|
|||||||
* convert a RecipeImport to a "real" external recipes and reload the table
|
* convert a RecipeImport to a "real" external recipes and reload the table
|
||||||
* @param item
|
* @param item
|
||||||
*/
|
*/
|
||||||
function importRecipe(item: RecipeImport){
|
function importRecipe(item: RecipeImport) {
|
||||||
let api = new ApiApi()
|
let api = new ApiApi()
|
||||||
api.apiRecipeImportImportRecipeCreate({id: item.id!, recipeImport: item}).then(r => {
|
api.apiRecipeImportImportRecipeCreate({id: item.id!, recipeImport: item}).then(r => {
|
||||||
loadItems({page: 1, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery.value})
|
loadItems({page: 1, itemsPerPage: useUserPreferenceStore().deviceSettings.general_tableItemsPerPage, search: searchQuery.value})
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
@@ -221,7 +224,7 @@ let api = new ApiApi()
|
|||||||
/**
|
/**
|
||||||
* convert all RecipeImports to "real" external recipes and reload the table (should be empty afterwards)
|
* convert all RecipeImports to "real" external recipes and reload the table (should be empty afterwards)
|
||||||
*/
|
*/
|
||||||
function importAllRecipes(){
|
function importAllRecipes() {
|
||||||
let api = new ApiApi()
|
let api = new ApiApi()
|
||||||
|
|
||||||
api.apiRecipeImportImportAllCreate({recipeImport: {} as RecipeImport}).then(r => {
|
api.apiRecipeImportImportAllCreate({recipeImport: {} as RecipeImport}).then(r => {
|
||||||
|
|||||||
Reference in New Issue
Block a user