SushantGautam commited on
Commit
7afb991
·
1 Parent(s): 82abdb6

Refactor WSGI and manage.py to utilize refresh_author_db function; add refresh_author_db_view for admin access control

Browse files
Files changed (5) hide show
  1. BridgeMentor/utils.py +24 -0
  2. BridgeMentor/wsgi.py +2 -23
  3. core/urls.py +2 -1
  4. core/views.py +10 -0
  5. manage.py +2 -22
BridgeMentor/utils.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import time
4
+
5
+
6
+ def refresh_author_db(check_time=False):
7
+ db_file = os.path.join(os.getcwd(), "db.sqlite3")
8
+ if check_time & (os.path.exists(db_file) and (time.time() - os.path.getmtime(db_file) < 300)):
9
+ print("db.sqlite3 exists and is recent. Skipping download.")
10
+ else:
11
+ from huggingface_hub import hf_hub_download
12
+ custom_cache_dir = os.path.expanduser("/app/.cache/huggingface")
13
+
14
+ file_path = hf_hub_download(
15
+ repo_id="SushantGautam/BridgeMentor",
16
+ filename="db.sqlite3",
17
+ repo_type="dataset",
18
+ cache_dir=custom_cache_dir
19
+ )
20
+ print(f"Downloaded to: {file_path}")
21
+ destination_path = os.path.join(os.getcwd(), "db.sqlite3")
22
+ shutil.copy(file_path, destination_path)
23
+ print(
24
+ f"db.sqlite3 Copied to current directory: {destination_path}")
BridgeMentor/wsgi.py CHANGED
@@ -8,31 +8,10 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
8
  """
9
 
10
  import os
11
- import shutil
12
  from django.core.wsgi import get_wsgi_application
13
- import time
14
-
15
  if os.environ.get('INSIDEDOCKER'):
16
- db_file = os.path.join(os.getcwd(), "db.sqlite3")
17
-
18
- if os.path.exists(db_file) and (time.time() - os.path.getmtime(db_file) < 300):
19
- print("db.sqlite3 exists and is recent. Skipping download.")
20
- else:
21
- from huggingface_hub import hf_hub_download
22
- custom_cache_dir = os.path.expanduser("/app/.cache/huggingface")
23
-
24
- file_path = hf_hub_download(
25
- repo_id="SushantGautam/BridgeMentor",
26
- filename="db.sqlite3",
27
- repo_type="dataset",
28
- cache_dir=custom_cache_dir
29
- )
30
- print(f"Downloaded to: {file_path}")
31
- destination_path = os.path.join(os.getcwd(), "db.sqlite3")
32
- shutil.copy(file_path, destination_path)
33
- print(f"db.sqlite3 Copied to current directory: {destination_path}")
34
-
35
-
36
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BridgeMentor.settings')
37
 
38
  application = get_wsgi_application()
 
8
  """
9
 
10
  import os
 
11
  from django.core.wsgi import get_wsgi_application
12
+ from .utils import refresh_author_db
 
13
  if os.environ.get('INSIDEDOCKER'):
14
+ refresh_author_db(check_time=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BridgeMentor.settings')
16
 
17
  application = get_wsgi_application()
core/urls.py CHANGED
@@ -1,7 +1,8 @@
1
  from django.urls import path
2
- from .views import home_page, search_hierarchy
3
 
4
  urlpatterns = [
5
  path('', home_page, name='home'),
6
  path("api/search/", search_hierarchy, name="search-hierarchy"),
 
7
  ]
 
1
  from django.urls import path
2
+ from .views import home_page, search_hierarchy, refresh_author_db_view
3
 
4
  urlpatterns = [
5
  path('', home_page, name='home'),
6
  path("api/search/", search_hierarchy, name="search-hierarchy"),
7
+ path("refresh_author_db", refresh_author_db_view, name="refresh-author-db")
8
  ]
core/views.py CHANGED
@@ -2,11 +2,21 @@ from .models import Domain, Field, Subfield, Topic
2
  from django.http import JsonResponse
3
  from django.shortcuts import render
4
 
 
 
5
 
6
  def home_page(request):
7
  return render(request, 'home.html')
8
 
9
 
 
 
 
 
 
 
 
 
10
  def search_hierarchy(request):
11
  q = request.GET.get("q", "").strip()
12
  if not q:
 
2
  from django.http import JsonResponse
3
  from django.shortcuts import render
4
 
5
+ from BridgeMentor.utils import refresh_author_db
6
+
7
 
8
  def home_page(request):
9
  return render(request, 'home.html')
10
 
11
 
12
+ def refresh_author_db_view(request):
13
+ # check if admin
14
+ if not request.user.is_superuser:
15
+ return JsonResponse({"error": "Permission denied"}, status=403)
16
+ refresh_author_db(check_time=False)
17
+ return JsonResponse({"status": "success", "message": "Database refreshed successfully. <a href='/'>Go Home</a>"}, status=200)
18
+
19
+
20
  def search_hierarchy(request):
21
  q = request.GET.get("q", "").strip()
22
  if not q:
manage.py CHANGED
@@ -2,30 +2,10 @@
2
  """Django's command-line utility for administrative tasks."""
3
  import os
4
  import sys
5
- import time
6
-
7
- import shutil
8
 
 
9
  if os.environ.get('INSIDEDOCKER'):
10
- db_file = os.path.join(os.getcwd(), "db.sqlite3")
11
-
12
- if os.path.exists(db_file) and (time.time() - os.path.getmtime(db_file) < 300):
13
- print("db.sqlite3 exists and is recent. Skipping download.")
14
- else:
15
- from huggingface_hub import hf_hub_download
16
- custom_cache_dir = os.path.expanduser("/app/.cache/huggingface")
17
-
18
- file_path = hf_hub_download(
19
- repo_id="SushantGautam/BridgeMentor",
20
- filename="db.sqlite3",
21
- repo_type="dataset",
22
- cache_dir=custom_cache_dir
23
- )
24
- print(f"Downloaded to: {file_path}")
25
- destination_path = os.path.join(os.getcwd(), "db.sqlite3")
26
- shutil.copy(file_path, destination_path)
27
- print(f"db.sqlite3 Copied to current directory: {destination_path}")
28
-
29
 
30
  def main():
31
  """Run administrative tasks."""
 
2
  """Django's command-line utility for administrative tasks."""
3
  import os
4
  import sys
 
 
 
5
 
6
+ from BridgeMentor.utils import refresh_author_db
7
  if os.environ.get('INSIDEDOCKER'):
8
+ refresh_author_db(check_time=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def main():
11
  """Run administrative tasks."""