Ejemplo n.º 1
0
def save_form(request):
    try:
        from django.contrib.admin.models import LogEntry
        from django.contrib.contenttypes.models import ContentType
        matriculation_type = ContentType.objects.get(app_label='school',
                                                     model='matriculation')
        student_id = int(request.POST.get('student_id'))
        grade_section_id = int(request.POST.get('grade_section'))
        teaching_year = int(request.POST.get('teaching_year'))
        status = int(request.POST.get('status'))

        matriculation_exist = Matriculation.objects.filter(
            student_id=student_id,
            teaching_year=teaching_year,
        ).first()

        django_log_entry = LogEntry()
        if matriculation_exist:
            matriculation_exist.student_id = student_id
            matriculation_exist.grade_section_id = grade_section_id
            matriculation_exist.status = status
            matriculation_exist.save()

            django_log_entry.action_flag = 2
            key_message = 'changed'
            django_log_entry.object_id = matriculation_exist.id
            django_log_entry.object_repr = matriculation_exist.__str__()
            change_message = {
                key_message: {
                    'fields': ['estudiante', 'grado y sección', 'estado']
                }
            }

            message = 'Matrícula actualizada exitosamente'
        else:
            matriculation = Matriculation()
            matriculation.student_id = student_id
            matriculation.grade_section_id = grade_section_id
            matriculation.status = status
            matriculation.save()

            django_log_entry.action_flag = 1
            key_message = 'added'
            django_log_entry.object_id = matriculation.id
            django_log_entry.object_repr = matriculation.__str__()
            change_message = {key_message: {}}

            message = 'Matrícula guardada exitosamente'

        django_log_entry.change_message = json.dumps([change_message])
        django_log_entry.content_type_id = matriculation_type.id
        django_log_entry.user_id = request.user.id

        django_log_entry.save()

        response = {'message': message, 'status': True}
    except:
        response = {'status': False, 'message': 'Error al guardar'}

    return JsonResponse(response)
Ejemplo n.º 2
0
def control_edition_note(list_control_fields_edited,
                         note,
                         teacher_id=None,
                         supervisor_id=None):
    from .models import NoteControlEdition
    from django.contrib.admin.models import LogEntry
    from django.contrib.contenttypes.models import ContentType
    import json
    note_type = ContentType.objects.get(app_label='school', model='note')

    fields = []
    for register_edited in list_control_fields_edited:
        note_edited_control = NoteControlEdition()
        note_edited_control.note_id = note.id
        note_edited_control.edit_field = register_edited.get('field_name')
        note_edited_control.value_edit_field = register_edited.get('value')
        note_edited_control.supervisor_id = supervisor_id
        note_edited_control.save()

        key = fields_note_spanish[register_edited['field_name']]
        fields.append('{} - {}'.format(key, register_edited['value']))

    flag_add = NoteControlEdition.objects.filter(note_id=note.id).count()
    flag_add = 1 if flag_add <= 1 else 2
    django_log_entry = LogEntry()
    django_log_entry.object_id = note.id
    django_log_entry.object_repr = note.__str__()
    django_log_entry.action_flag = flag_add

    key_message = 'added' if flag_add <= 1 else 'changed'
    if key_message == 'added':
        change_message = {key_message: {}}
    else:
        change_message = {key_message: {'fields': fields}}

    django_log_entry.change_message = json.dumps([change_message])
    django_log_entry.content_type_id = note_type.id
    django_log_entry.user_id = supervisor_id or teacher_id

    django_log_entry.save()