diff --git a/.gitignore b/.gitignore index 43cabbf..086cf43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +/static /upload_file /generate_paper *.pyc \ No newline at end of file diff --git a/ec_user/forms.py b/ec_user/forms.py index dcde108..9f6a072 100644 --- a/ec_user/forms.py +++ b/ec_user/forms.py @@ -1,7 +1,7 @@ from django import forms from .models import EcUser, ContactInfo, SchoolInfo, AcademicInfo, HealthInfo, SelfEvaluation -from .models import HobbiesInterests, SocialPractice, FamilyInfo, AwardsPunishments +from .models import HobbiesInterests, SocialPractice, FamilyInfo, AwardsInfo, PunishmentsInfo class BaiscInfoForm(forms.ModelForm): class Meta: @@ -105,15 +105,21 @@ class FamilyInfoForm(forms.ModelForm): } # AwardsPunishments 表单 -class AwardsPunishmentsForm(forms.ModelForm): +class AwardsInfoForm(forms.ModelForm): class Meta: - model = AwardsPunishments - fields = ['award_name', 'award_date', 'award_organization', 'discipline_date', 'discipline_issue', 'discipline_outcome'] + model = AwardsInfo + fields = ['award_name', 'award_date', 'award_organization'] labels = { - 'award_name': '奖项名称', 'award_date': '获奖日期', 'award_organization': '颁奖机构', + } + +class PunishmentsInfoForm(forms.ModelForm): + class Meta: + model = PunishmentsInfo + fields = ['discipline_date', 'discipline_issue', 'discipline_outcome'] + labels = { 'discipline_date': '处分日期', 'discipline_issue': '处分问题', 'discipline_outcome': '处分结果', @@ -129,7 +135,8 @@ g_models = { 'hobbies_interests': HobbiesInterests, 'social_practice': SocialPractice, 'family_info': FamilyInfo, - 'awards_punishments': AwardsPunishments + 'awards_info': AwardsInfo, + 'punishments_info': PunishmentsInfo } g_form_classes = { @@ -141,5 +148,6 @@ g_form_classes = { 'hobbies_interests_form': HobbiesInterestsForm, 'social_practice_form': SocialPracticeForm, 'family_info_form': FamilyInfoForm, - 'awards_punishments_form': AwardsPunishmentsForm + 'awards_info_form': AwardsInfoForm, + 'punishments_info_form': PunishmentsInfoForm, } diff --git a/ec_user/migrations/0016_awardsinfo_punishmentsinfo_alter_ecuser_age_and_more.py b/ec_user/migrations/0016_awardsinfo_punishmentsinfo_alter_ecuser_age_and_more.py new file mode 100644 index 0000000..0854e47 --- /dev/null +++ b/ec_user/migrations/0016_awardsinfo_punishmentsinfo_alter_ecuser_age_and_more.py @@ -0,0 +1,91 @@ +# Generated by Django 4.2.19 on 2025-03-01 14:45 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ec_user', '0015_alter_hobbiesinterests_extracurricular_activities_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='AwardsInfo', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('award_name', models.CharField(max_length=100)), + ('award_date', models.DateField()), + ('award_organization', models.CharField(max_length=100)), + ], + ), + migrations.CreateModel( + name='PunishmentsInfo', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('discipline_date', models.DateField(blank=True, null=True)), + ('discipline_issue', models.CharField(blank=True, max_length=255, null=True)), + ('discipline_outcome', models.CharField(blank=True, max_length=255, null=True)), + ], + ), + migrations.AlterField( + model_name='ecuser', + name='age', + field=models.IntegerField(blank=True, null=True, verbose_name='年龄'), + ), + migrations.AlterField( + model_name='ecuser', + name='dob', + field=models.DateField(blank=True, null=True, verbose_name='出生日期'), + ), + migrations.AlterField( + model_name='ecuser', + name='ethnicity', + field=models.CharField(default='Unknown', max_length=50, verbose_name='民族'), + ), + migrations.AlterField( + model_name='ecuser', + name='gender', + field=models.CharField(choices=[('M', 'Male'), ('F', 'Female'), ('O', 'Other')], default='M', max_length=1, verbose_name='性别'), + ), + migrations.AlterField( + model_name='ecuser', + name='id_card_number', + field=models.CharField(max_length=18, unique=True, verbose_name='身份证号'), + ), + migrations.AlterField( + model_name='ecuser', + name='name', + field=models.CharField(max_length=100, verbose_name='姓名'), + ), + migrations.AlterField( + model_name='ecuser', + name='political_status', + field=models.CharField(blank=True, max_length=50, null=True, verbose_name='政治面貌'), + ), + migrations.AlterField( + model_name='ecuser', + name='subject_usage_count', + field=models.IntegerField(default=5, verbose_name='使用次数限制'), + ), + migrations.AlterField( + model_name='ecuser', + name='subjects', + field=models.ManyToManyField(blank=True, related_name='users', to='ec_user.subject', verbose_name='开放权限的科目'), + ), + migrations.DeleteModel( + name='AwardsPunishments', + ), + migrations.AddField( + model_name='punishmentsinfo', + name='user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AddField( + model_name='awardsinfo', + name='user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/ec_user/migrations/0017_alter_awardsinfo_award_date_and_more.py b/ec_user/migrations/0017_alter_awardsinfo_award_date_and_more.py new file mode 100644 index 0000000..7d788f6 --- /dev/null +++ b/ec_user/migrations/0017_alter_awardsinfo_award_date_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.19 on 2025-03-01 14:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ec_user', '0016_awardsinfo_punishmentsinfo_alter_ecuser_age_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='awardsinfo', + name='award_date', + field=models.DateField(blank=True, null=True), + ), + migrations.AlterField( + model_name='awardsinfo', + name='award_name', + field=models.CharField(blank=True, max_length=100, null=True), + ), + migrations.AlterField( + model_name='awardsinfo', + name='award_organization', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/ec_user/models.py b/ec_user/models.py index 1e8c013..e29c876 100644 --- a/ec_user/models.py +++ b/ec_user/models.py @@ -229,16 +229,24 @@ class FamilyInfo(models.Model): } return f"Family Info of {self.user.name}" -# AwardsPunishments(奖惩情况表) -class AwardsPunishments(models.Model): +# Awards(奖情况表) +class AwardsInfo(models.Model): + # 定义一个与EcUser一对多关联的外键 + user = models.ForeignKey(EcUser, on_delete=models.CASCADE) + award_name = models.CharField(max_length=100,null=True, blank=True) + award_date = models.DateField(null=True, blank=True) + award_organization = models.CharField(max_length=100, null=True, blank=True) + + def __str__(self): + return f"Awards of {self.user.name}" + +# Punishments(处分情况表) +class PunishmentsInfo(models.Model): # 定义一个与EcUser一对多关联的外键 user = models.ForeignKey(EcUser, on_delete=models.CASCADE) - award_name = models.CharField(max_length=100) - award_date = models.DateField() - award_organization = models.CharField(max_length=100) discipline_date = models.DateField(null=True, blank=True) discipline_issue = models.CharField(max_length=255, null=True, blank=True) discipline_outcome = models.CharField(max_length=255, null=True, blank=True) def __str__(self): - return f"Awards and Punishments of {self.user.name}" + return f"Punishments of {self.user.name}" \ No newline at end of file diff --git a/ec_user/templates/all_info_form.html b/ec_user/templates/all_info_form.html index 3aa7dcb..877b9b6 100644 --- a/ec_user/templates/all_info_form.html +++ b/ec_user/templates/all_info_form.html @@ -269,17 +269,33 @@ -
- + +
-

奖惩情况

- {{ awards_punishments_form.management_form }} - {% for form in awards_punishments_form %} +

奖励情况

+ {{ awards_info_form.management_form }} + {% for form in awards_info_form %}
{{ form.as_p }}
{% empty %} -

没有奖惩情况记录。

+

没有奖励情况记录。

+ {% endfor %} + +
+
+ +
+ +
+

处罚情况

+ {{ punishments_info_form.management_form }} + {% for form in punishments_info_form %} +
+ {{ form.as_p }} +
+ {% empty %} +

没有处罚情况记录。

{% endfor %}
diff --git a/ec_user/templates/api_awards_punishments_test.html b/ec_user/templates/api_awards_info_test.html similarity index 69% rename from ec_user/templates/api_awards_punishments_test.html rename to ec_user/templates/api_awards_info_test.html index fc2de8e..6060f9b 100644 --- a/ec_user/templates/api_awards_punishments_test.html +++ b/ec_user/templates/api_awards_info_test.html @@ -3,7 +3,7 @@ - Test Awards and Punishments API + Test Awards API -

Test Awards and Punishments API

+

Test Awards API

-

Get Awards and Punishments

+

Get Awards

- +
-

Update Awards and Punishments

+

Update Awards



@@ -39,23 +39,15 @@

+

- - -

- - -

- - -

- +
+ + diff --git a/ec_user/urls.py b/ec_user/urls.py index cc718ec..e779a5a 100644 --- a/ec_user/urls.py +++ b/ec_user/urls.py @@ -7,7 +7,7 @@ from . views import MyProtectedApiContactInfo, api_contact_info_test, MyProtecte from . views import MyProtectedApiAcademicInfo, api_academic_info_test, MyProtectedApiHealthInfo, api_health_info_test from . views import MyProtectedApiSelfEvaluationInfo, api_self_evaluation_test, MyProtectedFamilyInfo, api_family_info_test from . views import MyProtectedApiHobbiesInterests, api_hobbies_interests_test, MyProtectedSocialPractice, api_social_practice_test -from . views import MyProtectedAwardsPunishments, api_awards_punishments_test , MyProtectedBasicUserInfo +from . views import MyProtectedAwardsInfo, MyProtectedPunishmentsInfo, api_awards_info_test, api_punishments_info_test , MyProtectedBasicUserInfo from . views import all_info_form, all_info_edit @@ -45,8 +45,11 @@ urlpatterns = [ path('family_info/api/', MyProtectedFamilyInfo.as_view(), name='api_family_info'), path('api_family_info_test/', api_family_info_test, name='api_family_info_test'), - path('awards_punishments/api/', MyProtectedAwardsPunishments.as_view(), name='api_awards_punishments'), - path('api_awards_punishments_test/', api_awards_punishments_test, name='api_awards_punishments_test'), + path('awards_info/api/', MyProtectedAwardsInfo.as_view(), name='api_awards_info'), + path('api_awards_info_test/', api_awards_info_test, name='api_awards_info_test'), + + path('punishments_info/api/', MyProtectedPunishmentsInfo.as_view(), name='api_punishments_info'), + path('api_punishments_info_test/', api_punishments_info_test, name='api_punishments_info_test'), path('all_info_form//', all_info_form, name='all_info_form'), diff --git a/ec_user/views.py b/ec_user/views.py index cd3cc21..8cfc75c 100644 --- a/ec_user/views.py +++ b/ec_user/views.py @@ -11,7 +11,7 @@ from django.contrib.auth.hashers import check_password, make_password # 导入 from .models import EcUser, ContactInfo, SchoolInfo, AcademicInfo, HealthInfo, SelfEvaluation -from .models import HobbiesInterests, SocialPractice, FamilyInfo, AwardsPunishments +from .models import HobbiesInterests, SocialPractice, FamilyInfo, AwardsInfo, PunishmentsInfo from .forms import g_models, g_form_classes @@ -83,7 +83,7 @@ def all_info_form(request, user_id): instances = {} for key, model in g_models.items(): - if key in ['hobbies_interests', 'social_practice', 'family_info', 'awards_punishments']: + if key in ['hobbies_interests', 'social_practice', 'family_info', 'awards_info', 'punishments_info']: instances[key] = model.objects.filter(user=user) # 处理一对多关系 else: instance, _ = model.objects.get_or_create(user=user) @@ -93,7 +93,7 @@ def all_info_form(request, user_id): # 初始化所有表单 forms = {} for form_key, form_class in g_form_classes.items(): - if form_key in ['hobbies_interests_form', 'social_practice_form', 'family_info_form', 'awards_punishments_form']: + if form_key in ['hobbies_interests_form', 'social_practice_form', 'family_info_form', 'awards_info_form', 'punishments_info_form']: queryset = instances[form_key[:-5]] # 获取对应模型的查询集 # 如果查询集为空,设置 extra=1 来允许添加空表单 @@ -160,7 +160,8 @@ class MyProtectedUserInfo(APIView): "family_info": FamilyInfo.objects.filter(user=request.user).exists(), "hobbies_interests": HobbiesInterests.objects.filter(user=request.user).exists(), "social_practice": SocialPractice.objects.filter(user=request.user).exists(), - "awards_punishments": AwardsPunishments.objects.filter(user=request.user).exists(), + "awards_info": AwardsInfo.objects.filter(user=request.user).exists(), + "punishments_info": PunishmentsInfo.objects.filter(user=request.user).exists(), } }) @@ -604,10 +605,13 @@ class MyProtectedFamilyInfo(APIView): return JsonResponse({"message": "Family info updated successfully"}) -def api_awards_punishments_test(req): - return render(req, 'api_awards_punishments_test.html') +def api_awards_info_test(req): + return render(req, 'api_awards_info_test.html') -class MyProtectedAwardsPunishments(APIView): +def api_punishments_info_test(req): + return render(req, 'api_punishments_info_test.html') + +class MyProtectedAwardsInfo(APIView): authentication_classes = [CustomTokenAuthentication] # 使用自定义的 Token 认证 permission_classes = [IsAuthenticated] # 需要用户认证才能访问 @@ -615,21 +619,18 @@ class MyProtectedAwardsPunishments(APIView): user = request.user # 获取该用户的奖惩情况记录 - awards_punishments = AwardsPunishments.objects.filter(user=user) + awards_punishments = AwardsInfo.objects.filter(user=user) if awards_punishments.exists(): response_data = [ { "award_name": ap.award_name, "award_date": ap.award_date, - "award_organization": ap.award_organization, - "discipline_date": ap.discipline_date, - "discipline_issue": ap.discipline_issue, - "discipline_outcome": ap.discipline_outcome + "award_organization": ap.award_organization } for ap in awards_punishments ] return JsonResponse(response_data, safe=False) else: - return JsonResponse({"error": "No awards or punishments found"}, status=404) + return JsonResponse({"error": "No awards found"}, status=404) def post(self, request): user = request.user @@ -639,21 +640,55 @@ class MyProtectedAwardsPunishments(APIView): award_name = data.get("award_name") award_date = data.get("award_date") award_organization = data.get("award_organization") + except json.JSONDecodeError: + return JsonResponse({"error": "Invalid JSON format"}, status=400) + + awards_punishment = AwardsInfo.objects.create( + user=user, + award_name=award_name, + award_date=award_date, + award_organization=award_organization, + ) + + return JsonResponse({"message": "Awards info updated successfully"}) + +class MyProtectedPunishmentsInfo(APIView): + authentication_classes = [CustomTokenAuthentication] # 使用自定义的 Token 认证 + permission_classes = [IsAuthenticated] # 需要用户认证才能访问 + + def get(self, request): + user = request.user + + # 获取该用户的奖惩情况记录 + awards_punishments = PunishmentsInfo.objects.filter(user=user) + if awards_punishments.exists(): + response_data = [ + { + "discipline_date": ap.discipline_date, + "discipline_issue": ap.discipline_issue, + "discipline_outcome": ap.discipline_outcome + } for ap in awards_punishments + ] + return JsonResponse(response_data, safe=False) + else: + return JsonResponse({"error": "No punishments found"}, status=404) + + def post(self, request): + user = request.user + # 处理 POST 请求,创建或更新奖惩情况记录 + try: + data = json.loads(request.body) discipline_date = data.get("discipline_date") discipline_issue = data.get("discipline_issue") discipline_outcome = data.get("discipline_outcome") except json.JSONDecodeError: return JsonResponse({"error": "Invalid JSON format"}, status=400) - # 创建或更新 AwardsPunishments 记录 - awards_punishment = AwardsPunishments.objects.create( + awards_punishment = PunishmentsInfo.objects.create( user=user, - award_name=award_name, - award_date=award_date, - award_organization=award_organization, discipline_date=discipline_date, discipline_issue=discipline_issue, discipline_outcome=discipline_outcome ) - return JsonResponse({"message": "Awards and punishments info updated successfully"}) + return JsonResponse({"message": "punishments info updated successfully"}) \ No newline at end of file