11from django .shortcuts import redirect , render
22from blog .models import Post
3- from connectors .githubShowcase .GitHubAPI import get_data ,check_github_username
4- from .models import profile_register
3+
54import json
65
76
87def index (request ):
98 blog = Post .objects .all ()[:3 ]
109 return render (request ,"projects/index.html" ,{"post" :blog ,'redirect_url' :request .get_full_path ()})
11-
12- def profile_add (request ):
13-
14- if request .method == "POST" :
15- if request .user .is_authenticated and request .user .is_active :
16- get_username = request .POST .get ("username" )
17- if check_github_username (get_username ):
18- if is_profile_already (get_username ):
19- return render (request ,"projects/GitHubShowcase/add_profile.html" ,{"error" :"Profile already exits" ,"url" :True ,"user" :get_username })
20- else :
21- add_profile = profile_register .objects .create (github_username = get_username ,user = request .user .username )
22- add_profile .save ()
23- return redirect ("/projects/github-showcase/profile/{}" .format (get_username ))
24- else :
25- return render (request ,"projects/GitHubShowcase/add_profile.html" ,{"error" :"No such GitHub username" })
26- else :
27- return render (request ,"config/error.html" ,{"error" :"By pass blocked" })
28- else :
29- if request .user .is_authenticated and request .user .is_active :
30- if is_user_created_profile (request .user .username ):
31- return render (request ,"config/error.html" ,{"error" :"You can only create one profile with one account" })
32- else :
33- return render (request ,"projects/GitHubShowcase/add_profile.html" )
34- else :
35- return redirect ("/account/login/?redirect-url={}" .format (request .get_full_path ()))
36-
37-
38- def profile (request ,username ):
39- if is_profile_already (username ):
40- try :
41- if request .COOKIES ['profile_data' ]:
42- profile_data = json .loads (request .COOKIES ['profile_data' ])
43- response = render (request ,"projects/GitHubShowcase/profile.html" ,{"profile" :profile_data ,})
44- except :
45- profile_data = get_data (username )
46- response = render (request ,"projects/GitHubShowcase/profile.html" ,{"profile" :profile_data })
47- response .set_cookie (key = 'profile_data' , value = json .dumps (profile_data ),max_age = 1800 )
48-
49- if profile_data is not None :
50- try :
51- if profile_data ['RateLimits' ]:
52- return render (request ,"config/error.html" ,{"error" :"GitHub API Rate Limits Exceeded , try after some time" })
53- except :
54- return response
55- else :
56- return render (request ,"config/error.html" ,{"error" :"Oops somethings went wrong, please contact support" })
57- else :
58- return render (request ,"config/error.html" ,{"error" :"No such profile found , create-account to create one" })
59-
60- def githubShowcase (request ):
61- return render (request ,'projects/GitHubShowcase/index.html' )
62-
63- def is_profile_already (github_username ):
64- query = profile_register .objects .filter (github_username = github_username ).exists ()
65- return query
66-
67- def is_user_created_profile (username ):
68- query = profile_register .objects .filter (user = username ).exists ()
69- return query
0 commit comments