2nd step

swadhingithub committed 2 years ago
85573d0
1 parent b5d5013
commit 85573d0
12 files changed
+293 -1
▼api/v1/handlers/__init__.py
+0-0
0
0
▼api/v1/handlers/teacherpannel.py
+30-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from fastapi import APIRouter,Depends,HTTPException,status
4
+from services import TeacherService
5
+from models import *
6
+from schemas.teacher_shema import OutTeacher,UpdateTeacher,TeacherBase
7
+import pymongo
8
+
9
+
10
+teacher_router = APIRouter()
11
+
12
+@teacher_router.post("/teacher",response_model=OutTeacher)
13
+async def create_teacher(data:TeacherBase):
14
+ try:
15
+ return await TeacherService.create_teacher(data)
16
+ except pymongo.errors.DuplicateKeyError:
17
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
18
+ detail="Username already exists")
19
+
20
+
21
+@teacher_router.post("/update_teacher",
22
+ response_model=OutTeacher)
23
+async def update_teacher(data:UpdateTeacher):
24
+ try:
25
+ return await TeacherService.update_teacher(data)
26
+ except pymongo.errors.DuplicateKeyError:
27
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
28
+ detail="user Does not exists")
29
+
30
+
0
31
▼models/studentmodel.py
+1-1
2
2
from typing import Any, Optional
3
3
from datetime import datetime
4
4
from uuid import UUID,uuid4
5
-from beanie import Document, Indexed, DocumentTemplate
5
+from beanie import Document, Indexed
6
6
7
7
class Student(Document):
8
8
user_id: UUID = Indexed(default_factory=uuid4)
9
9
▼routes/routers.py
+0-0
0
0
▼schemas/plagermodel_shema.py
+0-0
0
0
▼schemas/plagur.py
+25-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from pydantic import BaseModel,EmailStr,Field
4
+from models.plagermodel import Plager
5
+
6
+class PlagerBase(BaseModel):
7
+ file_name: str=Field(...,description="Name of the file")
8
+ title: str=Field(...,description="Title of the file")
9
+ owner: List[str]=Field(...,description="Owner of the file")
10
+ plagarism: str=Field(...,description="Plagarism of the file")
11
+ description: str=Field(...,description="Description of the file")
12
+
13
+class OutPlager(PlagerBase):
14
+ file_name: str
15
+ title: str
16
+ owner: List[str]
17
+ plagarism: str
18
+ description: str
19
+
20
+class UpdatePlager(BaseModel):
21
+ file_name: Optional[str]
22
+ title: Optional[str]
23
+
24
+
25
+
0
26
▼schemas/studentschemas.py
+38-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from pydantic import BaseModel,EmailStr,Field
4
+from models.studentmodel import Student
5
+
6
+class StudentBase(BaseModel):
7
+ name: str=Field(...,description="Name of the student")
8
+ email: EmailStr=Field(...,description="Email of the student")
9
+ username: str=Field(...,min_length=5,max_length=23,description="Username of the student")
10
+ password: str=Field(...,min_length=8,max_length=23,description="Password of the student")
11
+ frist_name: Optional[str]=Field(None,description="First name of the student")
12
+ last_name: Optional[str]=Field(None,description="Last name of the student")
13
+ is_active: Optional[bool]=Field(True,description="Is the student active")
14
+ is_superuser: Optional[bool]=Field(False,description="Is the student superuser")
15
+ userpicture: Optional[str]=Field(None,description="Profile picture of the student")
16
+
17
+
18
+class StudentOut(BaseModel):
19
+ name: str=Field(...,description="Name of the student")
20
+ email: EmailStr=Field(...,description="Email of the student")
21
+ username: str=Field(...,min_length=5,max_length=23,description="Username of the student")
22
+ frist_name: Optional[str]=Field(None,description="First name of the student")
23
+ last_name: Optional[str]=Field(None,description="Last name of the student")
24
+ is_active: Optional[bool]=Field(True,description="Is the student active")
25
+ is_superuser: Optional[bool]=Field(False,description="Is the student superuser")
26
+ userpicture: Optional[str]=Field(None,description="Profile picture of the student")
27
+
28
+
29
+class StudentUpdate(BaseModel):
30
+ name: Optional[str]=Field(None,description="Name of the student")
31
+ email: Optional[EmailStr]=Field(None,description="Email of the student")
32
+ username: Optional[str]=Field(None,min_length=5,max_length=23,description="Username of the student")
33
+ frist_name: Optional[str]=Field(None,description="First name of the student")
34
+ last_name: Optional[str]=Field(None,description="Last name of the student")
35
+ is_active: Optional[bool]=Field(None,description="Is the student active")
36
+ is_superuser: Optional[bool]=Field(None,description="Is the student superuser")
37
+ userpicture: Optional[str]=Field(None,description="Profile picture of the student")
38
+
0
39
▼schemas/teacher_shema.py
+44-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from pydantic import BaseModel,EmailStr,Field
4
+from models.TeachersMOdel import Teacher
5
+
6
+class TeacherBase(BaseModel):
7
+ teacher_id: str=Field(...,description="ID of the teacher")
8
+ teacher_deg: Optional[str]=Field(None,description="Degree of the teacher")
9
+ teacher_profile: Optional[str]=Field(None,description="Profile of the teacher")
10
+ name: str=Field(...,description="Name of the teacher")
11
+ email: EmailStr=Field(...,description="Email of the teacher")
12
+ username: str=Field(...,min_length=5,max_length=23,description="Username of the teacher")
13
+ password: str=Field(...,min_length=8,max_length=23,description="Password of the teacher")
14
+ frist_name: Optional[str]=Field(None,description="First name of the teacher")
15
+ last_name: Optional[str]=Field(None,description="Last name of the teacher")
16
+ is_active: Optional[bool]=Field(True,description="Is the teacher active")
17
+ is_superuser: Optional[bool]=Field(False,description="Is the teacher superuser")
18
+ userpicture: Optional[str]=Field(None,description="Profile picture of the teacher")
19
+
20
+
21
+class OutTeacher(TeacherBase):
22
+ teacher_id: str
23
+ user_id: UUID
24
+ teacher_deg: Optional[str]
25
+ teacher_profile: Optional[str]
26
+ name: str
27
+ email: EmailStr
28
+ username: str
29
+ frist_name: Optional[str]
30
+ last_name: Optional[str]
31
+ is_active: Optional[bool]
32
+ is_superuser: Optional[bool]
33
+ userpicture: Optional[str]
34
+
35
+
36
+class UpdateTeacher(BaseModel):
37
+ teacher_deg: Optional[str]
38
+ teacher_profile: Optional[str]
39
+ name: Optional[str]
40
+ email: Optional[EmailStr]
41
+ username: Optional[str]
42
+ frist_name: Optional[str]
43
+ last_name: Optional[str]
44
+ userpicture: Optional[str]
0
45
▼services/TeacherService.py
+57-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from schemas.teacher_shema import OutTeacher,UpdateTeacher,TeacherBase
4
+from models.TeachersMOdel import Teacher
5
+
6
+import pymongo
7
+
8
+class TeacherService:
9
+ @staticmethod
10
+ async def create_teacher(user:TeacherBase):
11
+ teacher_in=Teacher(
12
+ teacher_id=user.teacher_id,
13
+ name=user.name,
14
+ email=user.email,
15
+ username=user.username,
16
+ password=user.password,
17
+ frist_name=user.frist_name,
18
+ last_name=user.last_name,
19
+
20
+ )
21
+ await teacher_in.save()
22
+ return teacher_in
23
+
24
+ @staticmethod
25
+ async def get_teacher_byid(teacher_id:str)->Optional[Teacher]:
26
+ teacher=await Teacher.find_one(Teacher.teacher_id==teacher_id)
27
+ return teacher
28
+
29
+ @staticmethod
30
+ async def get_teacher_byusername(username:str)->Optional[Teacher]:
31
+ teacher=await Teacher.find_one(Teacher.username==username)
32
+ return teacher
33
+
34
+ @staticmethod
35
+ async def get_teacher_byemail(email:str)->Optional[Teacher]:
36
+ teacher=await Teacher.find_one(Teacher.email==email)
37
+ return teacher
38
+
39
+ @staticmethod
40
+ async def get_teacher_byname(name:str)->Optional[Teacher]:
41
+ teacher=await Teacher.find_one(Teacher.name==name)
42
+ return teacher
43
+
44
+ @staticmethod
45
+ async def get_all_teachers()->List[Teacher]:
46
+ teachers=await Teacher.find_all()
47
+ return teachers
48
+
49
+ @staticmethod
50
+ async def update_teacher(teacher_id:str,teacher:UpdateTeacher)->Optional[Teacher]:
51
+ teacher=await Teacher.find_one(Teacher.teacher_id==teacher_id)
52
+ if not teacher:
53
+ raise pymongo.errors.OperationFailure("Teacher not found")
54
+
55
+ await teacher.update({"$set":teacher.dict( exclude_unset=True)})
56
+
57
+ return teacher
0
58
▼services/converttotext.py
+33-0
1
+# Import libraries
2
+from typing import Collection
3
+import nltk
4
+from nltk.corpus import stopwords
5
+from gensim.models import TfidfModel
6
+
7
+# Define functions for preprocessing and similarity calculation
8
+def preprocess_text(text):
9
+ tokens = nltk.word_tokenize(text)
10
+ tokens = [t for t in tokens if t not in stopwords.words("english")]
11
+ stemmer = nltk.PorterStemmer()
12
+ tokens = [stemmer.stem(t) for t in tokens]
13
+ return tokens
14
+
15
+def calculate_similarity(text1, text2):
16
+ # Preprocess text
17
+ tokens1 = preprocess_text(text1)
18
+ tokens2 = preprocess_text(text2)
19
+
20
+ # Create TF-IDF model
21
+ dictionary = Collection.defaultdict(int)
22
+ for token in tokens1 + tokens2:
23
+ dictionary[token] += 1
24
+ model = TfidfModel([dictionary])
25
+
26
+ # Convert texts to vectors
27
+ vec1 = model[tokens1]
28
+ vec2 = model[tokens2]
29
+
30
+ # Calculate cosine similarity
31
+ similarity_score = vec1.dot(vec2) / (vec1.norm() * vec2.norm())
32
+
33
+ return similarity_score
0
34
▼services/findPlager.py
+17-0
1
+import requests
2
+url="https://www.editpad.org/plag_req_v5"
3
+data="fuck"
4
+API_KEY="5cec2091350e7a1d8e1f2bd281756b962dcea5d8"
5
+
6
+
7
+data=f"{API_KEY}&{data}"
8
+response = requests.post(url, data=data)
9
+
10
+test=response.json()
11
+print(test)
12
+
13
+
14
+
15
+
16
+
17
+
0
18
▼services/studemtservice.py
+48-0
1
+from typing import Optional, List
2
+from uuid import UUID
3
+from schemas.studentschemas import StudentBase,StudentOut,StudentUpdate
4
+from models.studentmodel import Student
5
+import pymongo
6
+
7
+class StudentService:
8
+ @staticmethod
9
+ async def create_student(user:StudentBase):
10
+ student_in=Student(
11
+ student_id=user.student_id,
12
+ first_name=user.first_name,
13
+ last_name=user.last_name,
14
+ user_name=user.user_name,
15
+ email=user.email,
16
+ hashed_password=user.hashed_password,
17
+ is_active=user.is_active,
18
+ is_superuser=user.is_superuser,
19
+ userpicture=user.userpicture
20
+ )
21
+ await student_in.save()
22
+ return student_in
23
+
24
+ @staticmethod
25
+ async def get_student_byid(student_id:str)->Optional[Student]:
26
+ student=await Student.find_one(Student.student_id==student_id)
27
+ return student
28
+
29
+ @staticmethod
30
+ async def get_student_byusername(username:str)->Optional[Student]:
31
+ student=await Student.find_one(Student.user_name==username)
32
+ return student
33
+
34
+ @staticmethod
35
+ async def get_student_byemail(email:str)->Optional[Student]:
36
+ student=await Student.find_one(Student.email==email)
37
+ return student
38
+
39
+ @staticmethod
40
+ async def update_student(student_id:str,student:StudentUpdate)->Optional[Student]:
41
+ student=await Student.find_one(Student.student_id==student_id)
42
+ if not student:
43
+ raise pymongo.errors.OperationFailure("Student not found")
44
+
45
+ await student.update({"$set":student.dict( exclude_unset=True)})
46
+
47
+ return student
48
+
0
49