day 5
swadhinbiswas committed 2 years ago
b4adfb2
1 parent 4c62dcd
commit b4adfb2
7 files changed
+121 -0
Showing 7 changed files+121-0
▼api/v1/handlers/__pycache__/plaguehandel.cpython-311.pyc
+0-0
Binary file not shown.
▼api/v1/handlers/handler.py
+12-0
| @@ -0,0 +1,12 @@ | ||
1 | +from fastapi import APIRouter,HTTPException,UploadFile,File | |
2 | +from services import pdftoword | |
3 | +import pymongo | |
4 | +from schemas import articleschema | |
5 | + | |
6 | + | |
7 | +routerx=APIRouter() | |
8 | + | |
9 | +@routerx.post("/aticle",response_model=articleschema.ArticleOut) | |
10 | +async def store_article(): | |
11 | + pass | |
12 | + | |
0 | 13 | |
▼models/articlemodel.py
+46-0
| @@ -0,0 +1,46 @@ | ||
1 | +from beanie import Document, Indexed | |
2 | +from typing import List,Any | |
3 | + | |
4 | +class ArticleModel(Document): | |
5 | + title: str | |
6 | + owner: List[str] | |
7 | + filename:str | |
8 | + content:str | |
9 | + | |
10 | + | |
11 | + def __repr__(self) -> str: | |
12 | + return f"ArticleModel(title={self.title}, owner={self.owner})" | |
13 | + | |
14 | + def __str__(self) -> str: | |
15 | + return self.__repr__() | |
16 | + | |
17 | + def __eq__(self, other: Any) -> bool: | |
18 | + if isinstance(other, ArticleModel): | |
19 | + return self.title == other.title and self.owner == other.owner | |
20 | + return False | |
21 | + @property | |
22 | + def get_title(self): | |
23 | + return self.title | |
24 | + @property | |
25 | + def get_all_text(self): | |
26 | + return self.all_text | |
27 | + @property | |
28 | + def get_owner(self): | |
29 | + return self.owner | |
30 | + @property | |
31 | + def set_title(self, title): | |
32 | + self.title = title | |
33 | + | |
34 | + @classmethod | |
35 | + async def get_article_by_title(cls, title: str): | |
36 | + return await cls.get(title=title) | |
37 | + async def get_article_by_owner(cls, owner: str): | |
38 | + return await cls.get(owner=owner) | |
39 | + async def get_article_by_text(cls, all_text: str): | |
40 | + return await cls.get(all_text=all_text) | |
41 | + async def get_article_by_title_and_owner(cls, title: str, owner: str): | |
42 | + return await cls.get(title=title, owner=owner) | |
43 | + async def get_article_by_title_and_text(cls, title: str, all_text: str): | |
44 | + return await cls.get(title=title, all_text=all_text) | |
45 | + async def get_article_by_owner_and_text(cls, owner: str, all_text: str): | |
46 | + return await cls.get(owner=owner, all_text=all_text) | |
0 | 47 | |
▼schemas/__pycache__/plagur.cpython-311.pyc
+0-0
Binary file not shown.
▼schemas/articleschema.py
+19-0
| @@ -0,0 +1,19 @@ | ||
1 | +from typing import List,Optional | |
2 | +from pydantic import BaseModel,Field | |
3 | +from fastapi import File,UploadFile | |
4 | + | |
5 | +class ArticleSchema(BaseModel): | |
6 | + title: str = Field(...) | |
7 | + content:str=Field(...) | |
8 | + owner: List[str] = Field(...) | |
9 | + filename:str=Field(...) | |
10 | + | |
11 | + | |
12 | +class ArticleOut(BaseModel): | |
13 | + title: str | |
14 | + owner: List[str] | |
15 | + content:str | |
16 | + | |
17 | + | |
18 | + | |
19 | + | |
0 | 20 | |
▼services/articleservice.py
+33-0
| @@ -0,0 +1,33 @@ | ||
1 | +from calendar import c | |
2 | +from os import path | |
3 | +import shutil | |
4 | +import pdftoword | |
5 | +from typing import Optional,List | |
6 | +from fastapi import File,UploadFile | |
7 | +from schemas.articleschema import ArticleSchema | |
8 | +from models.articlemodel import ArticleModel | |
9 | +import pymongo | |
10 | + | |
11 | +class ArticleService: | |
12 | + @staticmethod | |
13 | + async def create_article(user:ArticleSchema, uploade_file:UploadFile=File(...)): | |
14 | + path=f"files/{uploade_file.filename}" | |
15 | + with open(path, "wb") as buffer: | |
16 | + shutil.copyfileobj(uploade_file.file, buffer) | |
17 | + | |
18 | + data=await pdftoword.pdftotext(path) | |
19 | + | |
20 | + | |
21 | + | |
22 | + article_in=ArticleModel( | |
23 | + title=user.title, | |
24 | + owner=user.owner, | |
25 | + filename=uploade_file.filename, | |
26 | + content=data, | |
27 | + ) | |
28 | + await article_in.save() | |
29 | + return article_in | |
30 | + @staticmethod | |
31 | + async def get_article_bytitle(title:str)->Optional[ArticleModel]: | |
32 | + article=await ArticleModel.find_one(ArticleModel.title==title) | |
33 | + return article | |
0 | 34 | |
▼services/pdftoword.py
+11-0
| @@ -0,0 +1,11 @@ | ||
1 | +from pypdf import PdfReader | |
2 | + | |
3 | +async def pdftotext(file): | |
4 | + with open(file, 'rb') as f: | |
5 | + reader = PdfReader(f) | |
6 | + text = '' | |
7 | + for page in reader.pages: | |
8 | + text += page.extract_text() | |
9 | + return text | |
10 | + | |
11 | + | |
0 | 12 | |