-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
1340 lines (1082 loc) · 50.6 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''API v1 for the database'''
from fastapi import APIRouter, Request, Depends, HTTPException, status, Response, UploadFile, Form
from fastapi.responses import FileResponse
from PIL import Image
import os
# from fastapi.templating import Jinja2Templates
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer
from sqlalchemy import func
from sqlalchemy.future import select
from sqlalchemy.sql import and_
from sqlalchemy.orm import selectinload, joinedload
from sqlalchemy.ext.asyncio import AsyncSession
from passlib.context import CryptContext
from uuid import uuid4
from jose import jwt, JWTError
from db import get_session, init_db
from models import User, UserSignup, UserUpdate, Token, Project, Tag, Category, CategoryWithTags, ProjectWithUserAndTags, ProjectFull, ProjectUpdate, UserInfo, ProjectFeatured, project_tags, TagCount, CategoryUpdate, TagUpdate, ProjectCount, CategoryCreate, TagCreate
from datetime import timedelta, datetime
from typing import List
import asyncio
import re
API_PREFIX = "/api/v1"
router = APIRouter(prefix=API_PREFIX)
# openssl rand -hex 32
SECRET_KEY = "d8e632e42229356dbbcd5fdc366a05e9bfaca0193ba016e4fd6cf03307d90241"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
SALT = "fa3e1b071f78d55d833c2df51a3089e5"
DEFAULT_PAGE = 1
DEFAULT_PAGE_SIZE = 30
MAX_PAGE_SIZE = 50
# jinja2 template for server side render html
# templates = Jinja2Templates(directory="templates")
# it is tokenUrl instead of kebab-case token_url to unify with OAuth2 scheme
# tokenUrl is to specify OpenAPI route address of token endpoint for frontend login
# OAuthPasswordBearer extracts Bearer from Authorization Header and send it to tokenUrl
oauth2_bearer = OAuth2PasswordBearer(tokenUrl=API_PREFIX + "/user/token")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def get_current_time():
return datetime.utcnow()
@router.on_event("startup")
async def startup():
await init_db()
# for further refactor the functions down below should be moved to a utils package
def is_admin(user: User) -> bool:
return True if user.role else False
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password + SALT, hashed_password)
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def unauthorized_error(detail: str) -> HTTPException:
return HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail=detail,
headers={"WWW-Authenticate": "Bearer"})
def _create_token(
data: dict, expires: timedelta = timedelta(minutes=15)) -> str:
to_encode = data.copy()
expire = datetime.utcnow() + expires
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def _decode_token(token: str) -> dict:
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except JWTError:
raise unauthorized_error("Could not validate credentials")
# warning: this function returns all the users without limit and offset
async def get_all_users_db(session: AsyncSession = Depends(
get_session)) -> List[User]:
result = await session.execute(select(User))
return result.scalars().all()
async def get_user_with_id_db(
user_id: int, session: AsyncSession = Depends(get_session)) -> User:
result = await session.execute(select(User).where(User.id == user_id))
return result.scalar_one_or_none()
async def get_user_with_email_db(
email: str, session: AsyncSession = Depends(get_session)) -> User:
result = await session.execute(select(User).where(User.email == email))
return result.scalar_one_or_none()
# users = result.scalars().all()
# return [User(**user.__dict__) for user in users]
# get current user from token by decoding it
async def get_current_user(token: str = Depends(oauth2_bearer),
session: AsyncSession = Depends(
get_session)) -> User:
error = unauthorized_error("Could not validate credentials")
payload = _decode_token(token=token)
username = payload.get("sub")
if not username:
raise error
expires = payload.get("exp")
if expires < int(datetime.utcnow().timestamp()):
raise unauthorized_error("Token expired")
user = await get_user_with_email_db(username, session)
if user is None:
raise unauthorized_error(
f"User {username} not found, probably email changed")
if user.password_version != payload.get("password_version"):
raise unauthorized_error("Password updated, please login again")
if user.role != payload.get("role"):
raise unauthorized_error("Role updated, please login again")
return user
@router.post("/user/signup", response_model=Token)
async def create_user(user: UserSignup,
session: AsyncSession = Depends(get_session)):
if not user.email or not re.match(
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", user.email):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Email invalid")
existed_user = await get_user_with_email_db(user.email, session)
if existed_user is not None:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Email registered already")
if not user.password or len(user.password) < 8:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Password invalid, should be at least 8 characters")
new_user = User(email=user.email,
hashed_password=get_password_hash(user.password + SALT),
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
password_version=0,
id=str(uuid4()))
session.add(new_user)
await session.commit()
await session.refresh(new_user)
token = _create_token(
data={
"sub": user.username,
"password_version": 0,
"role": 0
},
expires=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
response = {
"access_token":
token,
"token_type":
"bearer",
"email":
user.email,
"role":
0,
"expires_at":
datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
}
return response
@router.put("/user/update/{id}", response_model=User)
async def update_user(user: UserUpdate,
id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
r = await session.execute(select(User).where(User.id == id))
user_to_update = r.scalar_one_or_none()
if not user_to_update:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="User not found")
if not is_admin(current_user) and user_to_update.id != current_user.id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="non-admin user can only update self")
data = user.dict(exclude_unset=True)
for k, v in data.items():
if v is not None:
if k == "password":
if len(v) < 8:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=
"Password invalid, should be at least 8 characters")
if get_password_hash(v +
SALT) == user_to_update.hashed_password:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Password not changed")
user_to_update.password_version += 1
k, v = "hashed_password", get_password_hash(v + SALT)
elif k == "email":
result = await session.execute(
select(User).where(User.email == v))
searched_user = result.scalar_one_or_none()
if searched_user and searched_user.id != current_user.id: # if the queried user with same email is not user self
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"email {v} registered already")
setattr(user_to_update, k, v)
user_to_update.updated_at = datetime.utcnow()
session.add(user_to_update)
await session.commit(
) # flush is actually not needed here since commit will flush automatically
await session.refresh(user_to_update)
user_data = user_to_update.__dict__
user_data.pop("hashed_password")
return user_data
@router.delete('/user/delete/{id}')
async def delete_user(id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not id:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid user id")
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
result = await session.execute(select(User).where(User.id == id))
original_instance = result.scalar_one_or_none()
if not original_instance:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="User not found")
if not is_admin(current_user) and original_instance.id != current_user.id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Non-admin user can only delete self")
await session.delete(original_instance)
await session.commit()
await session.flush()
return {"status": "success"}
@router.post('/user/token',
summary="Create access and refresh tokens for user",
response_model=Token)
async def login(form: OAuth2PasswordRequestForm = Depends(),
session: AsyncSession = Depends(get_session)):
user = await get_user_with_email_db(form.username, session)
if not user or not verify_password(form.password, user.hashed_password):
raise unauthorized_error("Incorrect username or password")
token = _create_token(
data={
"sub": form.username,
"password_version": user.password_version,
"role": user.role
},
expires=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
response = {
"access_token":
token,
"token_type":
"bearer",
"email":
user.email,
"role":
user.role,
"expires_at":
datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
}
return response
@router.get("/user/info", response_model=UserInfo)
async def get_current_user_info(current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
r = await session.execute(select(User).where(User.id == current_user.id))
return r.scalar_one_or_none()
@router.get("/admin/users/total")
async def admin_get_total_users(
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
count = (await session.execute(select(func.count(User.id))
)).scalar_one_or_none()
if not count:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="No user found")
return {"total": count}
@router.get("/admin/users", response_model=List[UserInfo])
async def admin_get_all_users(response: Response,
per_page: int = DEFAULT_PAGE_SIZE,
page: int = DEFAULT_PAGE,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
count = (await session.execute(select(func.count(User.id))
)).scalar_one_or_none()
response.headers['X-Total-Count'] = str(count)
response.headers['X-Total-Pages'] = str(count // per_page +
(1 if count % per_page else 0))
result = await session.execute(
select(User).offset(max((page - 1) * per_page,
0)).limit(min(per_page, MAX_PAGE_SIZE)))
users = result.scalars().all()
ids = [user.id for user in users]
tasks = [get_user_projects_count_by_id(id, session) for id in ids]
users_projects_counts = await asyncio.gather(*tasks)
results = [
UserInfo(id=user.id,
created_at=user.created_at,
email=user.email,
username=user.username if user.username else None,
is_active=user.is_active,
role=user.role) for user in users
]
for i in range(len(results)):
results[i].projects_counts = users_projects_counts[i]
return results
@router.put("/admin/project/live/{id}")
async def toggle_project_is_live(
id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
result = await session.execute(select(Project).where(Project.id == id))
project = result.scalar_one_or_none()
if not project:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Project not found")
project.is_live = not project.is_live
session.add(project)
await session.commit()
await session.refresh(project)
return {"status": "success", "is_live": project.is_live}
@router.get("/admin/projects/total")
async def admin_get_total_projects(
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
count = (await session.execute(select(func.count(Project.id))
)).scalar_one_or_none()
if not count:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="No project found")
return {"total": count}
@router.get("/admin/projects/total/visit")
async def admin_get_total_projects_visit(
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
count = (await session.execute(select(func.sum(Project.visit_count))
)).scalar_one_or_none()
if not count:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="No project found")
return {"total": count}
@router.get("/admin/projects", response_model=List[ProjectWithUserAndTags])
async def admin_get_all_projects(
response: Response,
per_page: int = DEFAULT_PAGE_SIZE,
page: int = DEFAULT_PAGE,
is_live: bool = None,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Only admin can access this endpoint")
query = select(func.count(Project.id))
if is_live is not None:
query = query.where(Project.is_live == is_live)
count = (await session.execute(query)).scalar_one_or_none()
response.headers['X-Total-Count'] = str(count)
response.headers['X-Total-Pages'] = str(count // per_page +
(1 if count % per_page else 0))
query = select(Project).options(selectinload(Project.user),
selectinload(Project.tags))
if is_live is not None:
query = query.where(Project.is_live == is_live)
query = query.order_by(Project.date.desc()).offset(
max((page - 1) * per_page, 0)).limit(min(per_page, MAX_PAGE_SIZE))
result = await session.execute(query)
return result.scalars().all()
@router.post("/admin/category", response_model=Category)
async def create_category(new_category: CategoryCreate,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can create category")
if not new_category.categoryName:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="category name is required")
r = await session.execute(
select(Category).where(
Category.categoryName == new_category.categoryName))
if r.scalar_one_or_none():
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="category already exists")
new_category_instance = Category(categoryName=new_category.categoryName)
session.add(new_category_instance)
await session.commit()
await session.refresh(new_category_instance)
return new_category_instance
@router.put("/admin/category/{id}", response_model=Category)
async def update_category(id: int,
updated_category: CategoryCreate,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can update category")
r = await session.execute(
select(Category).where(Category.categoryId == id))
original_instance = r.scalar_one_or_none()
if not original_instance:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Category not found")
for k, v in updated_category.__dict__.items():
if v is not None:
setattr(original_instance, k, v)
session.add(original_instance)
await session.commit()
await session.refresh(original_instance)
return original_instance
@router.delete("/admin/category/{id}")
async def delete_category(id: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can delete category")
r = await session.execute(
select(Category).where(Category.categoryId == id))
original_instance = r.scalar_one_or_none()
if not original_instance:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Category not found")
await session.delete(original_instance)
await session.commit()
await session.flush()
return {"status": "success"}
@router.post("/admin/tag", response_model=Tag)
async def create_tag(new_tag: TagCreate,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can create category")
if new_tag.tagNameShort is None:
new_tag.tagNameShort = new_tag.tagName
if not new_tag.tagName or not new_tag.tagNameShort:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="tag name and short name are required")
if (await
session.execute(select(Tag).where(Tag.tagName == new_tag.tagName)
)).scalar_one_or_none():
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Tag already exists")
if not (await session.execute(
select(Category).where(Category.categoryId == new_tag.categoryId)
)).scalar_one_or_none():
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Category not found")
new_tag_instance = Tag(
tagName=new_tag.tagName,
tagNameShort=new_tag.tagNameShort,
categoryId=new_tag.categoryId if new_tag.categoryId else None)
session.add(new_tag_instance)
await session.commit()
await session.refresh(new_tag_instance)
return new_tag_instance
@router.put("/admin/tag/{id}", response_model=TagUpdate)
async def update_tag(id: int,
updated_tag: TagUpdate,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can update tag")
if id != updated_tag.tagId:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="id in url and body must match")
r = await session.execute(select(Tag).where(Tag.tagId == id))
original_instance = r.scalar_one_or_none()
if not original_instance:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Tag not found")
for k, v in updated_tag.__dict__.items():
if v is not None:
if k == "categoryId":
r = await session.execute(
select(Category).where(Category.categoryId == v))
if not r.scalar_one_or_none():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Category not found")
setattr(original_instance, k, v)
session.add(original_instance)
await session.commit()
await session.refresh(original_instance)
return original_instance
@router.delete("/admin/tag/{id}")
async def delete_tag(id: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can delete tag")
r = await session.execute(select(Tag).where(Tag.tagId == id))
original_instance = r.scalar_one_or_none()
if not original_instance:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Tag not found")
await session.delete(original_instance)
await session.commit()
await session.flush()
return {"status": "success"}
@router.get("/admin/projects/recent/{n}")
async def get_projects_updated_count_in_n_days(
n: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can get recent projects")
if n < 1:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="n must be greater than 0")
# Calculate the start date for the query
n_days_ago = datetime.utcnow() - timedelta(days=n - 1)
# Define a subquery to group the projects by date and count the number of projects
subquery = (select(
func.date(Project.date).label('date'),
func.count(Project.id).label('count')).where(
Project.date >= n_days_ago).group_by(func.date(
Project.date)).alias())
# Select the counts of projects updated on each day in the last n days
query = (select(subquery.c.date,
func.coalesce(subquery.c.count,
0).label("count")).order_by(subquery.c.date.desc()))
# Execute the query and get the results
r = await session.execute(query)
results = [{"date": row[0], "count": row[1]} for row in r]
dates = [result["date"] for result in results]
for date in range(n):
date = (n_days_ago + timedelta(days=date)).strftime("%Y-%m-%d")
if date not in dates:
results.append({"date": date, "count": 0})
results.sort(key=lambda x: x["date"])
return results
@router.get("/admin/tags/popular/{n}", response_model=List[TagCount])
async def get_top_n_popular_tags(
n: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can get popular tags")
# get the count of each tag
subquery = (select(project_tags.tag_id,
func.count(
project_tags.tag_id).label('count')).group_by(
project_tags.tag_id).alias('subquery'))
# select the top 10 most used tags
query = (select(subquery.c.tag_id, subquery.c.count).order_by(
subquery.c.count.desc()).limit(n))
r = await session.execute(query)
sub_results = [(row[0], row[1]) for row in r]
results = []
for sub_result in sub_results:
r = await session.execute(
select(Tag).where(Tag.tagId == sub_result[0]))
tag = r.scalar_one_or_none()
results.append(TagCount(tag=tag, count=sub_result[1]))
return results
async def get_user_projects_count_by_id(id: str, session: AsyncSession):
r = await session.execute(
select(func.count(Project.id)).where(Project.user_id == id,
Project.is_live == True))
live_count = r.scalar_one_or_none()
r = await session.execute(
select(func.count(Project.id)).where(Project.user_id == id,
Project.is_live == False))
draft_count = r.scalar_one_or_none()
r = await session.execute(
select(func.count(Project.id)).where(Project.user_id == id))
total_count = r.scalar_one_or_none()
return ProjectCount(live_count=live_count,
draft_count=draft_count,
total_count=total_count)
@router.get("/admin/user/{id}/projects",
response_model=List[ProjectWithUserAndTags])
async def get_projects_by_user_id(
id: str,
response: Response,
per_page: int = DEFAULT_PAGE_SIZE,
page: int = DEFAULT_PAGE,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not is_admin(current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="only admin can get projects by user id")
id = id.replace("-", "")
if len(id) != 32:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid user id")
count = (await session.execute(
select(func.count(Project.id)).where(Project.user_id == id)
)).scalar_one_or_none()
response.headers['X-Total-Count'] = str(count)
response.headers['X-Total-Pages'] = str(count // per_page +
(1 if count % per_page else 0))
result = await session.execute(
select(Project).options(
selectinload(Project.user),
selectinload(Project.tags)).where(Project.user_id == id).offset(
max((page - 1) * per_page,
0)).limit(min(per_page, MAX_PAGE_SIZE)))
return result.scalars().all()
@router.post("/user/project")
async def create_project(title: str = Form(),
link: str = Form(),
completionDate: str = Form(),
description: str = Form(),
content: str = Form(),
tags: str = Form(""),
imageFile: UploadFile = None,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
data = {
"title": title,
"link": link,
"date": completionDate,
"description": description,
"content": content,
"tags": [int(tagId) for tagId in tags.split(",")] if tags else []
}
tags = []
for tagId in data["tags"]:
if not isinstance(tagId, int):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Tag must be an integer")
r = await session.execute(select(Tag).where(Tag.tagId == tagId))
tag = r.scalar_one_or_none()
if not tag:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Tag with ID {tagId} not found")
tags.append(tag)
data["tags"] = tags
new_project = Project(**data,
id=str(uuid4()),
user_id=current_user.id,
#date=datetime.utcnow(),
user=current_user)
#TODO refactor to a separate function
if imageFile:
tempFilePath = "./database/content/images/temp-" + str(
new_project.id) + "-" + imageFile.filename
try:
contents = imageFile.file.read()
with open(tempFilePath, 'wb') as f:
f.write(contents)
except Exception:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
finally:
imageFile.file.close()
try:
im = Image.open(tempFilePath)
im.verify()
except Exception:
if os.path.exists(tempFilePath):
os.remove(tempFilePath)
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Invalid image file format")
finally:
im.close()
filePath = "./database/content/images/" + str(new_project.id) + ".png"
try:
im = Image.open(tempFilePath)
im.save(filePath)
except Exception:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
finally:
im.close()
if os.path.exists(tempFilePath):
os.remove(tempFilePath)
session.add(new_project)
await session.commit()
await session.refresh(new_project)
return new_project
@router.delete("/user/project/{id}")
async def delete_project(id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
r = await session.execute(
select(Project).options(selectinload(Project.user),
selectinload(
Project.tags)).where(Project.id == id))
originalProject = r.scalar_one_or_none()
if not originalProject:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Project with ID {id} not found")
if not is_admin(
current_user) and originalProject.user.id != current_user.id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
await session.delete(originalProject)
await session.commit()
await session.flush()
return {"status": "success"}
@router.put("/user/project/{id}", response_model=ProjectWithUserAndTags)
async def modify_project(project: ProjectUpdate,
id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
r = await session.execute(
select(Project).options(selectinload(Project.user),
selectinload(
Project.tags)).where(Project.id == id))
originalProject = r.scalar_one_or_none()
if not originalProject:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Project with ID {id} not found")
if originalProject.user.id != current_user.id and not is_admin(
current_user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
data = project.dict(exclude_unset=True)
for k, v in data.items():
if v is not None:
if k == "is_live":
if not is_admin(current_user):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
setattr(originalProject, k, v)
elif k == "tags":
tags = []
for tagId in v:
if not isinstance(tagId, int):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"TagId {tagId} must be an integer")
r = await session.execute(
select(Tag).where(Tag.tagId == tagId))
tag = r.scalar_one_or_none()
if not tag:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Tag with ID {tagId} not found")
tags.append(tag)
setattr(originalProject, k, tags)
else:
setattr(originalProject, k, v)
session.add(originalProject)
await session.commit()
await session.refresh(originalProject)
return originalProject
@router.get("/user/project/{id}", response_model=ProjectFull)
async def get_project(id: str,
session: AsyncSession = Depends(get_session),
current_user: User = Depends(get_current_user)):
if not current_user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
r = await session.execute(
select(Project).options(selectinload(Project.user),
selectinload(
Project.tags)).where(Project.id == id))
project = r.scalar_one_or_none()
if not project:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Project with ID {id} not found")
if not is_admin(current_user) and project.user.id != current_user.id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized")
return project
@router.get("/user/projects", response_model=List[ProjectWithUserAndTags])
async def query_user_projects(
response: Response,
start_date: datetime = datetime.min,
end_date: datetime = Depends(get_current_time),
per_page: int = DEFAULT_PAGE_SIZE,
page: int = DEFAULT_PAGE,
keyword: str = "",