Spaces:
Runtime error
Runtime error
File size: 18,788 Bytes
8472119 65c4ece 8472119 65c4ece 8472119 65c4ece 8472119 af376d8 8472119 | 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 | """
Django forms for EduMentorAI
"""
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordResetForm
from django.core.exceptions import ValidationError
import logging
from .models import Subject, Document, Quiz, UserProfile, ChatMessage
from allauth.account.forms import SignupForm
from django.contrib.auth import get_user_model
logger = logging.getLogger(__name__)
User = get_user_model()
class CustomSignupForm(SignupForm):
"""Custom signup form to handle existing users properly"""
def clean_email(self):
"""Override to add custom email validation"""
email = self.cleaned_data.get('email')
if User.objects.filter(email__iexact=email).exists():
raise forms.ValidationError(
"An account with this email address already exists. "
"Please sign in instead or use the 'Forgot Password' option if you can't remember your password."
)
return email
class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True
class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)
def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result
class SubjectForm(forms.ModelForm):
"""Form for creating/editing subjects"""
class Meta:
model = Subject
fields = ['name', 'code', 'description']
widgets = {
'name': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter subject name'
}),
'code': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'e.g., CS101'
}),
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Enter subject description (optional)'
})
}
class DocumentUploadForm(forms.ModelForm):
"""Form for uploading documents"""
class Meta:
model = Document
fields = ['title', 'file', 'subject', 'processing_mode']
widgets = {
'title': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter document title'
}),
'file': forms.FileInput(attrs={
'class': 'form-control',
'accept': '.pdf,.docx,.txt,.pptx,.doc,.ppt'
}),
'subject': forms.Select(attrs={
'class': 'form-control'
}),
'processing_mode': forms.RadioSelect(attrs={
'class': 'form-check-input'
})
}
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['subject'].queryset = Subject.objects.filter(created_by=user)
# Add help text
self.fields['file'].help_text = 'Supported formats: PDF, DOCX, TXT, PPTX (Max 50MB)'
self.fields['processing_mode'].help_text = 'Choose processing mode based on your document type and time preference'
# Make title optional and auto-generate from filename if not provided
self.fields['title'].required = False
def clean_file(self):
file = self.cleaned_data.get('file')
if file:
# Check file size (50MB limit to match template)
if file.size > 50 * 1024 * 1024:
raise forms.ValidationError('File size must be less than 50MB')
# Check file extension
allowed_extensions = ['.pdf', '.docx', '.txt', '.pptx', '.doc', '.ppt']
file_extension = '.' + file.name.split('.')[-1].lower()
if file_extension not in allowed_extensions:
raise forms.ValidationError(
'File type not supported. Please upload PDF, DOCX, TXT, or PPTX files.'
)
return file
def clean_title(self):
title = self.cleaned_data.get('title')
file = self.cleaned_data.get('file')
# Auto-generate title from filename if not provided
if not title and file:
title = file.name.rsplit('.', 1)[0] # Remove file extension
return title
class QuizCreateForm(forms.ModelForm):
"""Form for creating quizzes"""
class Meta:
model = Quiz
fields = ['title', 'subject', 'based_on_document', 'description',
'time_limit', 'total_questions']
widgets = {
'title': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter quiz title'
}),
'subject': forms.Select(attrs={
'class': 'form-control'
}),
'based_on_document': forms.Select(attrs={
'class': 'form-control'
}),
'description': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Enter quiz description (optional)'
}),
'time_limit': forms.NumberInput(attrs={
'class': 'form-control',
'min': 5,
'max': 180,
'placeholder': 'Time limit in minutes'
}),
'total_questions': forms.NumberInput(attrs={
'class': 'form-control',
'min': 1,
'max': 50,
'placeholder': 'Number of questions'
})
}
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['subject'].queryset = Subject.objects.filter(created_by=user)
self.fields['based_on_document'].queryset = Document.objects.filter(
uploaded_by=user, processed=True
)
# Make based_on_document optional
self.fields['based_on_document'].required = False
self.fields['based_on_document'].empty_label = "Generate from all subject documents"
class ProfileForm(forms.ModelForm):
"""Form for editing user profile"""
# Add user fields
first_name = forms.CharField(
max_length=30,
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'First name'
})
)
last_name = forms.CharField(
max_length=30,
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Last name'
})
)
email = forms.EmailField(
widget=forms.EmailInput(attrs={
'class': 'form-control',
'placeholder': 'Email address'
})
)
class Meta:
model = UserProfile
fields = ['bio', 'university', 'major', 'year_of_study', 'avatar']
widgets = {
'bio': forms.Textarea(attrs={
'class': 'form-control',
'rows': 4,
'placeholder': 'Tell us about yourself'
}),
'university': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Your university'
}),
'major': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Your major/field of study'
}),
'year_of_study': forms.NumberInput(attrs={
'class': 'form-control',
'min': 1,
'max': 8,
'placeholder': 'Year of study'
}),
'avatar': forms.FileInput(attrs={
'class': 'form-control',
'accept': 'image/*'
})
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Populate user fields if instance exists
if self.instance and self.instance.user:
user = self.instance.user
self.fields['first_name'].initial = user.first_name
self.fields['last_name'].initial = user.last_name
self.fields['email'].initial = user.email
def save(self, commit=True):
profile = super().save(commit=False)
if commit:
# Update user fields
user = profile.user
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.save()
profile.save()
return profile
class ChatMessageForm(forms.ModelForm):
"""Form for chat messages"""
class Meta:
model = ChatMessage
fields = ['message']
widgets = {
'message': forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Ask a question about your uploaded materials...',
'style': 'resize: none;'
})
}
class QuizQuestionForm(forms.Form):
"""Dynamic form for quiz questions"""
def __init__(self, *args, **kwargs):
questions = kwargs.pop('questions', [])
super().__init__(*args, **kwargs)
for question in questions:
field_name = f'question_{question.id}'
if question.question_type == 'mcq':
choices = [(choice.id, choice.choice_text) for choice in question.choices.all()]
self.fields[field_name] = forms.ChoiceField(
choices=choices,
widget=forms.RadioSelect(attrs={'class': 'form-check-input'}),
required=True,
label=question.question_text
)
elif question.question_type == 'tf':
self.fields[field_name] = forms.ChoiceField(
choices=[('True', 'True'), ('False', 'False')],
widget=forms.RadioSelect(attrs={'class': 'form-check-input'}),
required=True,
label=question.question_text
)
elif question.question_type in ['sa', 'fb']:
self.fields[field_name] = forms.CharField(
widget=forms.Textarea(attrs={
'class': 'form-control',
'rows': 3,
'placeholder': 'Enter your answer...'
}),
required=True,
label=question.question_text
)
class SearchForm(forms.Form):
"""Form for searching documents"""
query = forms.CharField(
max_length=500,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Search your documents...',
'autocomplete': 'off'
})
)
subject = forms.ModelChoiceField(
queryset=Subject.objects.none(),
required=False,
empty_label="All subjects",
widget=forms.Select(attrs={'class': 'form-control'})
)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['subject'].queryset = Subject.objects.filter(created_by=user)
class SlideGenerationForm(forms.Form):
"""Form for generating slides"""
document = forms.ModelChoiceField(
queryset=Document.objects.none(),
widget=forms.Select(attrs={'class': 'form-control'}),
help_text="Select a document to generate slides from"
)
topic = forms.CharField(
max_length=200,
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Specific topic or leave empty for full document'
}),
help_text="Optional: Specify a particular topic to focus on"
)
slide_count = forms.IntegerField(
min_value=3,
max_value=20,
initial=10,
widget=forms.NumberInput(attrs={
'class': 'form-control',
'min': 3,
'max': 20
}),
help_text="Number of slides to generate (3-20)"
)
include_images = forms.BooleanField(
required=False,
initial=True,
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
help_text="Search and add relevant images from the internet to slides"
)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['document'].queryset = Document.objects.filter(
uploaded_by=user, processed=True
)
class ChatModeSelectionForm(forms.Form):
"""Form for selecting chat mode and document/subject"""
CHAT_MODE_CHOICES = [
('document', 'Chat with a specific document'),
('subject', 'Chat within a subject (all documents)'),
]
chat_mode = forms.ChoiceField(
choices=CHAT_MODE_CHOICES,
widget=forms.RadioSelect(attrs={'class': 'form-check-input'}),
initial='document'
)
# For document mode
document = forms.ModelChoiceField(
queryset=Document.objects.none(),
required=False,
empty_label="Select a document...",
widget=forms.Select(attrs={
'class': 'form-control',
'id': 'id_document'
})
)
# For subject mode
subject = forms.ModelChoiceField(
queryset=Subject.objects.none(),
required=False,
empty_label="Select a subject...",
widget=forms.Select(attrs={
'class': 'form-control',
'id': 'id_subject'
})
)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['document'].queryset = Document.objects.filter(
uploaded_by=user, processed=True
).order_by('title')
self.fields['subject'].queryset = Subject.objects.filter(
created_by=user
).filter(
documents__processed=True
).distinct().order_by('name')
def clean(self):
cleaned_data = super().clean()
chat_mode = cleaned_data.get('chat_mode')
document = cleaned_data.get('document')
subject = cleaned_data.get('subject')
if chat_mode == 'document' and not document:
raise forms.ValidationError('Please select a document for document chat mode.')
if chat_mode == 'subject' and not subject:
raise forms.ValidationError('Please select a subject for subject chat mode.')
return cleaned_data
class BulkDocumentUploadForm(forms.Form):
"""Form for bulk document upload"""
files = MultipleFileField(
help_text="Select multiple files to upload at once"
)
subject = forms.ModelChoiceField(
queryset=Subject.objects.none(),
widget=forms.Select(attrs={'class': 'form-control'})
)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if user:
self.fields['subject'].queryset = Subject.objects.filter(created_by=user)
class UserProfileForm(forms.ModelForm):
"""Form for user profile management"""
# Add user fields that can be edited
first_name = forms.CharField(
max_length=30,
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'First name'
})
)
last_name = forms.CharField(
max_length=30,
required=False,
widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Last name'
})
)
email = forms.EmailField(
widget=forms.EmailInput(attrs={
'class': 'form-control',
'placeholder': 'Email address',
'readonly': True # Email changes should go through AllAuth
})
)
class Meta:
model = UserProfile
fields = ['bio', 'university', 'major', 'year_of_study', 'avatar']
widgets = {
'bio': forms.Textarea(attrs={
'class': 'form-control',
'rows': 4,
'placeholder': 'Tell us about yourself...'
}),
'university': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'University or Institution'
}),
'major': forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Major or Field of Study'
}),
'year_of_study': forms.NumberInput(attrs={
'class': 'form-control',
'placeholder': 'Year of Study (e.g., 1, 2, 3, 4)',
'min': 1,
'max': 10
}),
'avatar': forms.FileInput(attrs={
'class': 'form-control',
'accept': 'image/*'
})
}
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if self.user:
self.fields['first_name'].initial = self.user.first_name
self.fields['last_name'].initial = self.user.last_name
self.fields['email'].initial = self.user.email
def save(self, commit=True):
profile = super().save(commit=False)
if self.user:
# Update user fields
self.user.first_name = self.cleaned_data['first_name']
self.user.last_name = self.cleaned_data['last_name']
if commit:
self.user.save()
profile.user = self.user
if commit:
profile.save()
return profile
|