-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
82 lines (69 loc) · 2.77 KB
/
utils.py
File metadata and controls
82 lines (69 loc) · 2.77 KB
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
from django.utils import timezone
from .models import APIKey
def validate_api_key(request):
"""Validate API key from request headers"""
api_key = request.headers.get('X-API-Key') or request.GET.get('api_key')
if not api_key:
return None
try:
key_obj = APIKey.objects.get(key=api_key, is_active=True)
return key_obj
except APIKey.DoesNotExist:
return None
def load_mock_data():
"""Load mock data from filesystem into database"""
from django.conf import settings
from .models import Dataset, Fact, File
import os
import json
mock_data_dir = settings.MOCK_DATA_DIR
if not os.path.exists(mock_data_dir):
print(f"Mock data directory {mock_data_dir} does not exist")
return
for dataset_name in os.listdir(mock_data_dir):
dataset_path = os.path.join(mock_data_dir, dataset_name)
if not os.path.isdir(dataset_path):
continue
# Create or get dataset
dataset, created = Dataset.objects.get_or_create(
name=dataset_name,
defaults={'description': f'Mock dataset: {dataset_name}'}
)
for fact_id in os.listdir(dataset_path):
fact_path = os.path.join(dataset_path, fact_id)
if not os.path.isdir(fact_path):
continue
# Create or get fact
fact, created = Fact.objects.get_or_create(
dataset=dataset,
fact_id=fact_id
)
# Load files
for filename in os.listdir(fact_path):
file_path = os.path.join(fact_path, filename)
if os.path.isfile(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Determine file type
if filename.endswith('.html'):
file_type = 'html'
elif filename.endswith('.json'):
file_type = 'json'
# If it's questions.json, update fact's questions_data
if filename == 'questions.json':
try:
fact.questions_data = json.loads(content)
fact.save()
except json.JSONDecodeError:
pass
else:
file_type = 'text'
# Create or update file
File.objects.update_or_create(
fact=fact,
filename=filename,
defaults={
'file_type': file_type,
'content': content
}
)