Parcourir la source

add test celery file

tomoya il y a 2 ans
Parent
commit
203a523a5e

+ 12 - 2
backend/app/app/api/api_v1/endpoints/utils.py

@@ -1,6 +1,6 @@
 from typing import Any
 
-from fastapi import APIRouter, Depends
+from fastapi import APIRouter, Depends, UploadFile, File
 from pydantic.networks import EmailStr
 
 from app import models, schemas
@@ -11,7 +11,7 @@ from app.utils import send_test_email
 router = APIRouter()
 
 
-@router.post("/test-celery/", response_model=schemas.Msg, status_code=201)
+@router.post("/test-celery/msg", response_model=schemas.Msg, status_code=201)
 def test_celery(
     msg: schemas.Msg,
     current_user: models.User = Depends(deps.get_current_active_superuser),
@@ -22,6 +22,16 @@ def test_celery(
     celery_app.send_task("app.worker.test_celery", args=[msg.msg])
     return {"msg": "Word received"}
 
+@router.post("/test-celery/file", response_model=schemas.Msg, status_code=201)
+def test_celery(
+    file: UploadFile=File(),
+    current_user: models.User = Depends(deps.get_current_active_superuser),
+) -> Any:
+    """
+    Test Celery worker.
+    """
+    celery_app.send_task("app.worker.test_celery", args=[file.filename])
+    return {"msg": "File received"}
 
 @router.post("/test-email/", response_model=schemas.Msg, status_code=201)
 def test_email(

+ 9 - 3
frontend/src/api.ts

@@ -2,10 +2,11 @@ import axios from "axios";
 import { apiUrl } from "@/env";
 import type { IUserProfile, IUserProfileUpdate, IUserProfileCreate, IUserProfileRegister } from "@/interfaces";
 
-function authHeaders(token: string) {
+function authHeaders(token: string, contentType:string="application/json") {
   return {
     headers: {
       Authorization: `Bearer ${token}`,
+      "Content-Type": contentType,
     },
   };
 }
@@ -49,7 +50,12 @@ export const api = {
   async registerUser(data: IUserProfileRegister) {
     return axios.post(`${apiUrl}/api/v1/users/register`, data);
   },
-  async testCelery(token: string, data:{msg:string}){
-    return axios.post<{msg:string}>(`${apiUrl}/api/v1/utils/test-celery`, data, authHeaders(token));
+  async testCeleryMsg(token: string, data:{msg: string}){
+    return axios.post<{msg:string}>(`${apiUrl}/api/v1/utils/test-celery/msg`, data, authHeaders(token));
+  },
+  async testCeleryFile(token: string, file: File){
+    const formData = new FormData();
+    formData.append("file", file)
+    return axios.post<{msg:string}>(`${apiUrl}/api/v1/utils/test-celery/file`, formData, authHeaders(token, "maltipart/form-data"));
   },
 };

+ 22 - 2
frontend/src/stores/admin.ts

@@ -85,14 +85,14 @@ export const useAdminStore = defineStore("AdminStoreId", {
               await mainStore.checkApiError(error);
             }
         },
-        async actionTestCelery(payload: {msg:string}) {
+        async actionTestCeleryMsg(payload: {msg:string}) {
           const mainStore = useMainStore();
           try {
             const loadingNotification = { content: "sending", showProgress: true };
             mainStore.addNotification(loadingNotification);
             const response = (
               await Promise.all([
-                api.testCelery(mainStore.token, payload),
+                api.testCeleryMsg(mainStore.token, payload),
                 await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
               ])
             );
@@ -104,6 +104,26 @@ export const useAdminStore = defineStore("AdminStoreId", {
           } catch (error) {
             await mainStore.checkApiError(error);
           }
+        },
+        async actionTestCeleryFile(payload: {file: File}) {
+          const mainStore = useMainStore();
+          try {
+            const loadingNotification = { content: "sending", showProgress: true };
+            mainStore.addNotification(loadingNotification);
+            const response = (
+              await Promise.all([
+                api.testCeleryFile(mainStore.token, payload),
+                await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
+              ])
+            );
+            mainStore.removeNotification(loadingNotification);
+            mainStore.addNotification({
+              content: "File received",
+                color: "success",
+            })
+          } catch (error) {
+            await mainStore.checkApiError(error);
+          }
         }
     },
 });

+ 46 - 11
frontend/src/views/main/admin/TestCelery.vue

@@ -2,20 +2,42 @@
   <v-container fluid>
     <v-card class="ma-3 pa-3">
       <v-card-title primary-title>
-        <div class="headline primary--text">Test Celery</div>
+        <div class="headline primary--text">Message</div>
       </v-card-title>
       <v-card-text>
-          <v-form v-model="valid" ref="form">
-            <v-text-field @keyup.enter="submit" label="Message" v-model="msg" :rules="required" ></v-text-field>
+          <v-form v-model="validMsg" ref="msgForm">
+            <v-text-field @keyup.enter="msgSubmit" label="Message" v-model="msg" :rules="required" ></v-text-field>
           </v-form>
       </v-card-text>
       <v-card-actions>
         <v-spacer></v-spacer>
-        <v-btn @click="submit" :disabled="!valid">
+        <v-btn @click="msgSubmit" :disabled="!validMsg">
               Send
             </v-btn>
       </v-card-actions>
     </v-card>
+    <v-card class="ma-3 pa-3">
+      <v-card-title primary-title>
+        <div class="headline primary--text">File</div>
+      </v-card-title>
+      <v-card-text>
+          <v-form v-model="validFile" ref="fileForm">
+            <v-file-input
+              v-model="zipFiles"
+              :rules="[v=>!v]"
+              accept=".zip"
+              label="File input"
+              prepend-icon="folder_zip"
+            ></v-file-input>
+          </v-form>
+      </v-card-text>
+      <v-card-actions>
+        <v-spacer></v-spacer>
+        <v-btn @click="fileSubmit" :disabled="!validFile">
+              Send
+        </v-btn>
+      </v-card-actions>
+    </v-card>
   </v-container>
 </template>
 <script setup lang="ts">
@@ -23,17 +45,30 @@ import { ref} from 'vue';
 import { required } from '@/utils';
 import { useAdminStore } from '@/stores/admin';
 
-const valid = ref(true);
+const validMsg = ref(true);
 const msg = ref('');
-const form = ref(null);
+const msgForm = ref(null);
+
+const validFile = ref(true);
+const zipFiles = ref();
+const fileForm = ref();
+
 
 const adminStore = useAdminStore();
 
-async function submit() {
-  await (form as any).value.validate();
-  if (valid.value) {
-    await adminStore.actionTestCelery({msg:msg.value});
-    (form as any).value.reset();
+async function msgSubmit() {
+  await (msgForm as any).value.validate();
+  if (validMsg.value) {
+    await adminStore.actionTestCeleryMsg({msg:msg.value});
+    (msgForm as any).value.reset();
+  }
+}
+
+async function fileSubmit() {
+  await (fileForm as any).value.validate();
+  if (validFile.value) {
+    await adminStore.actionTestCeleryFile(zipFiles.value[0]);
+    (fileForm as any).value.reset();
   }
 }
 </script>