Example #1
0
    def test_logentry_unicode(self):
        log_entry = LogEntry()

        log_entry.action_flag = ADDITION
        self.assertTrue(six.text_type(log_entry).startswith('Added '))

        log_entry.action_flag = CHANGE
        self.assertTrue(six.text_type(log_entry).startswith('Changed '))

        log_entry.action_flag = DELETION
        self.assertTrue(six.text_type(log_entry).startswith('Deleted '))

        # Make sure custom action_flags works
        log_entry.action_flag = 4
        self.assertEqual(six.text_type(log_entry), 'LogEntry Object')
Example #2
0
 def test_object_link(self):
     """Test the admin link to the log entry object."""
     log_entry = LogEntry(
         user=self.admin_user, action_flag=ADDITION,
         content_type_id=ContentType.objects.get_for_model(User).id,
         object_id=self.admin_user.id, object_repr=str(self.admin_user),
     )
     self.assertEqual(
         self.logentry_admin.object_link(log_entry),
         '<a href="{}">{}</a>'.format(log_entry.get_admin_url(), log_entry.object_repr)
     )
     # Test that a DELETION log entry returns object without a link
     log_entry.action_flag = DELETION
     self.assertEqual(
         self.logentry_admin.object_link(log_entry), log_entry.object_repr
     )
Example #3
0
    def log_change(self, request, object, message, old_object=None):
        """
        Log that an object has been successfully changed.

        The default implementation creates an admin LogEntry object.
        """
        entry = LogEntry()
        entry.user = request.user
        entry.content_type = ContentType.objects.get_for_model(object)
        entry.object_id = smart_unicode(object.pk)
        entry.object_repr = force_unicode(object)[:200]
        entry.action_flag = CHANGE
        entry.change_message = message
        entry.save()
        
        if old_object:
            entry_extend = LogEntryExtend()
            entry_extend.entry = entry
            entry_extend.data = old_object
            entry_extend.save()
Example #4
0
    def log_change(self, request, object, message, old_object=None):
        """
        Log that an object has been successfully changed.

        The default implementation creates an admin LogEntry object.
        """
        entry = LogEntry()
        entry.user = request.user
        entry.content_type = ContentType.objects.get_for_model(object)
        entry.object_id = smart_unicode(object.pk)
        entry.object_repr = force_unicode(object)[:200]
        entry.action_flag = CHANGE
        entry.change_message = message
        entry.save()

        if old_object:
            entry_extend = models.LogEntryExtend()
            entry_extend.entry = entry
            entry_extend.data = old_object
            entry_extend.save()
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()