<s> from django . utils import simplejson as json <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . text import get_text_list <EOL> from django . utils . encoding import force_unicode <EOL> from django . db import models <EOL> from django . db . models . query import QuerySet <EOL> from django . db . models . fields . files import FieldFile <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . forms import model_to_dict <EOL> from django . core import serializers <EOL> from django . core . files . uploadedfile import TemporaryUploadedFile , InMemoryUploadedFile <EOL> from audit_log . models import AuditLogEntry , CHANGE , ADDITION , DELETION <EOL> from django . conf import settings <EOL> ALWAYS_EXCLUDED_FIELDS = ( '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> def get_auditable_fields ( form ) : <EOL> """<STR_LIT>""" <EOL> is_auditable = getattr ( form . _meta . model . _meta , '<STR_LIT>' , True ) <EOL> if is_auditable and form . changed_data : <EOL> auditable_fields = set ( form . changed_data ) <EOL> explicit_auditable_fields = set ( getattr ( form . _meta , '<STR_LIT>' , None ) or auditable_fields ) <EOL> explicit_non_auditable_fields = set ( getattr ( form . _meta , '<STR_LIT>' , [ ] ) ) <EOL> explicit_non_auditable_fields . union ( set ( ALWAYS_EXCLUDED_FIELDS ) ) <EOL> result = auditable_fields . intersection ( explicit_auditable_fields ) . difference ( explicit_non_auditable_fields ) <EOL> return list ( result ) <EOL> else : <EOL> return [ ] <EOL> def field_serializer ( field_value ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( field_value , '<STR_LIT>' ) or isinstance ( field_value , QuerySet ) : <EOL> field_value = [ model_to_dict ( m ) for m in field_value . all ( ) ] <EOL> elif isinstance ( field_value , models . Model ) : <EOL> field_value = model_to_dict ( field_value ) <EOL> for k , v in field_value . iteritems ( ) : <EOL> field_value [ k ] = field_serializer ( v ) <EOL> elif isinstance ( field_value , InMemoryUploadedFile ) or isinstance ( field_value , TemporaryUploadedFile ) : <EOL> try : <EOL> field_value = { <EOL> '<STR_LIT:name>' : getattr ( field_value , '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:size>' : getattr ( field_value , '<STR_LIT:size>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> except AttributeError : <EOL> field_value = force_unicode ( field_value ) <EOL> elif hasattr ( field_value , '<STR_LIT:path>' ) or isinstance ( field_value , FieldFile ) : <EOL> try : <EOL> field_value = { <EOL> '<STR_LIT:url>' : getattr ( field_value , '<STR_LIT:url>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:path>' : getattr ( field_value , '<STR_LIT:path>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:size>' : getattr ( field_value , '<STR_LIT:size>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : getattr ( field_value , '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( field_value , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> if field_value [ '<STR_LIT:path>' ] : <EOL> field_value [ '<STR_LIT:path>' ] = field_value [ '<STR_LIT:path>' ] . split ( settings . MEDIA_ROOT ) [ <NUM_LIT:1> ] <EOL> except ValueError : <EOL> field_value = force_unicode ( field_value ) <EOL> else : <EOL> field_value = force_unicode ( field_value ) <EOL> return field_value <EOL> def collect_old_values ( obj , form = None , formsets = None , as_json_string = False ) : <EOL> """<STR_LIT>""" <EOL> result = { <EOL> "<STR_LIT>" : { } , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> unsaved_object = obj . __class__ . objects . get ( pk = obj . pk ) <EOL> if form : <EOL> for field_name in get_auditable_fields ( form ) : <EOL> field_value = getattr ( unsaved_object , field_name ) <EOL> result [ "<STR_LIT>" ] [ field_name ] = field_serializer ( field_value ) <EOL> if formsets : <EOL> model_related_objects = { } <EOL> for formset in formsets : <EOL> accessor_name = formset . rel_name <EOL> related_objects = getattr ( unsaved_object , accessor_name ) . all ( ) <EOL> model_related_objects [ accessor_name ] = related_objects <EOL> for related_name , related_objects in model_related_objects . iteritems ( ) : <EOL> result_formset_data = { <EOL> "<STR_LIT:related_name>" : related_name , <EOL> "<STR_LIT>" : [ model_to_dict ( r_obj ) for r_obj in related_objects ] <EOL> } <EOL> result [ "<STR_LIT>" ] . append ( result_formset_data ) <EOL> if as_json_string : <EOL> return json . dumps ( result ) <EOL> else : <EOL> return result <EOL> def collect_new_values ( form = None , formsets = None , as_json_string = False ) : <EOL> """<STR_LIT>""" <EOL> result = { <EOL> "<STR_LIT>" : { } , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> if form : <EOL> for field_name in get_auditable_fields ( form ) : <EOL> field_value = form . cleaned_data [ field_name ] <EOL> result [ "<STR_LIT>" ] [ field_name ] = field_serializer ( field_serializer ( field_value ) ) <EOL> if formsets : <EOL> for formset in formsets : <EOL> formset_data = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> added_data = [ ] <EOL> for added_object in formset . new_objects : <EOL> object_values = { <EOL> '<STR_LIT>' : force_unicode ( added_object . _meta . verbose_name ) , <EOL> '<STR_LIT>' : force_unicode ( added_object ) , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> for field in added_object . _meta . local_fields : <EOL> field_name = field . get_attname ( ) <EOL> field_value = getattr ( added_object , field_name ) <EOL> object_values [ '<STR_LIT>' ] [ field_name ] = field_serializer ( field_value ) <EOL> added_data . append ( object_values ) <EOL> formset_data [ '<STR_LIT>' ] = added_data <EOL> changed_data = [ ] <EOL> for changed_object , changed_fields in formset . changed_objects : <EOL> object_values = { <EOL> '<STR_LIT>' : force_unicode ( changed_object . _meta . verbose_name ) , <EOL> '<STR_LIT>' : force_unicode ( changed_object ) , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> for field_name in changed_fields : <EOL> field_value = getattr ( changed_object , field_name ) <EOL> object_values [ '<STR_LIT>' ] [ field_name ] = field_serializer ( field_value ) <EOL> changed_data . append ( object_values ) <EOL> formset_data [ '<STR_LIT>' ] = changed_data <EOL> deleted_data = [ ] <EOL> for deleted_object in formset . deleted_objects : <EOL> object_values = { <EOL> '<STR_LIT>' : force_unicode ( deleted_object . _meta . verbose_name ) , <EOL> '<STR_LIT>' : force_unicode ( deleted_object ) , <EOL> } <EOL> deleted_data . append ( object_values ) <EOL> formset_data [ '<STR_LIT>' ] = deleted_data <EOL> if any ( [ added_data , changed_data , deleted_data ] ) : <EOL> result [ "<STR_LIT>" ] . append ( formset_data ) <EOL> if as_json_string : <EOL> return json . dumps ( result ) <EOL> else : <EOL> return result <EOL> def construct_message_from_formset ( formsets ) : <EOL> message = [ ] <EOL> for formset in formsets : <EOL> for added_object in formset . new_objects : <EOL> message . append ( _ ( u'<STR_LIT>' ) <EOL> % { '<STR_LIT:name>' : force_unicode ( added_object . _meta . verbose_name ) , <EOL> '<STR_LIT:object>' : force_unicode ( added_object ) } ) <EOL> for changed_object , changed_fields in formset . changed_objects : <EOL> message . append ( _ ( u'<STR_LIT>' ) <EOL> % { '<STR_LIT:list>' : get_text_list ( changed_fields , _ ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT:name>' : force_unicode ( changed_object . _meta . verbose_name ) , <EOL> '<STR_LIT:object>' : force_unicode ( changed_object ) } ) <EOL> for deleted_object in formset . deleted_objects : <EOL> message . append ( _ ( u'<STR_LIT>' ) <EOL> % { '<STR_LIT:name>' : force_unicode ( deleted_object . _meta . verbose_name ) , <EOL> '<STR_LIT:object>' : force_unicode ( deleted_object ) } ) <EOL> return message <EOL> def construct_change_message ( form = None , formsets = None ) : <EOL> """<STR_LIT>""" <EOL> message = [ ] <EOL> if form and form . changed_data : <EOL> message . append ( _ ( u'<STR_LIT>' ) % get_text_list ( form . changed_data , _ ( '<STR_LIT>' ) ) ) <EOL> if formsets : <EOL> message . extend ( construct_message_from_formset ( formsets ) ) <EOL> message = u'<STR_LIT:\n>' . join ( message ) <EOL> return message or _ ( u'<STR_LIT>' ) <EOL> def construct_create_message ( form = None , formsets = None ) : <EOL> """<STR_LIT>""" <EOL> message = [ u'<STR_LIT:%s>' % force_unicode ( field ) for field in form . cleaned_data if form ] <EOL> if formsets : <EOL> message . extend ( construct_message_from_formset ( formsets ) ) <EOL> message = u'<STR_LIT>' + u'<STR_LIT:\n>' . join ( message ) <EOL> return message or _ ( u'<STR_LIT>' ) <EOL> def construct_delete_message ( obj ) : <EOL> return u"<STR_LIT>" % ( <EOL> obj . pk , <EOL> force_unicode ( obj . _meta . verbose_name ) , <EOL> force_unicode ( obj ) <EOL> ) <EOL> def _do_log ( user , obj , action_flag , change_message , old_values = '<STR_LIT>' , new_values = '<STR_LIT>' ) : <EOL> AuditLogEntry . objects . log_action ( <EOL> user_id = user . pk , <EOL> content_type_id = ContentType . objects . get_for_model ( obj ) . pk , <EOL> object_id = obj . pk , <EOL> object_repr = force_unicode ( obj ) , <EOL> action_flag = action_flag , <EOL> change_message = change_message , <EOL> old_values = old_values , <EOL> new_values = new_values <EOL> ) <EOL> def log_delete ( user , obj , message ) : <EOL> """<STR_LIT>""" <EOL> _do_log ( user , obj , DELETION , message ) <EOL> def log_change ( user , obj , message , old_values , new_values ) : <EOL> """<STR_LIT>""" <EOL> _do_log ( user , obj , CHANGE , message , old_values , new_values ) <EOL> def log_create ( user , obj , message , old_values , new_values ) : <EOL> """<STR_LIT>""" <EOL> _do_log ( user , obj , ADDITION , message , old_values , new_values ) </s>
