<s> """<STR_LIT>""" <EOL> import hashlib <EOL> from django import forms <EOL> from django . conf import settings <EOL> from dms_plugins . pluginpoints import BeforeRetrievalPluginPoint <EOL> from dms_plugins . pluginpoints import BeforeStoragePluginPoint <EOL> from dms_plugins . pluginpoints import BeforeUpdatePluginPoint <EOL> from dms_plugins . workers import Plugin <EOL> from dms_plugins . workers import PluginError <EOL> class HashForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> OPTION = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> method = forms . ChoiceField ( choices = OPTION ) <EOL> def __init__ ( self , options , * args , ** kwargs ) : <EOL> self . options = options <EOL> super ( HashForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def save ( self , commit = True ) : <EOL> """<STR_LIT>""" <EOL> method = self . options [ <NUM_LIT:0> ] <EOL> method . name = '<STR_LIT>' <EOL> method . value = self . cleaned_data [ '<STR_LIT>' ] <EOL> if commit : <EOL> method . save ( ) <EOL> return method <EOL> class HashCodeValidationOnStoragePlugin ( Plugin , BeforeStoragePluginPoint ) : <EOL> """<STR_LIT>""" <EOL> title = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> plugin_type = "<STR_LIT>" <EOL> method = '<STR_LIT>' <EOL> has_configuration = True <EOL> configurable_fields = [ '<STR_LIT>' , ] <EOL> form = HashForm <EOL> def work ( self , document ) : <EOL> """<STR_LIT>""" <EOL> method = self . get_option ( '<STR_LIT>' , document . get_docrule ( ) ) <EOL> return HashCodeWorker ( self . method ) . work_store ( document , method ) <EOL> class HashCodeValidationOnUpdatePlugin ( Plugin , BeforeUpdatePluginPoint ) : <EOL> """<STR_LIT>""" <EOL> title = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> plugin_type = "<STR_LIT>" <EOL> method = '<STR_LIT>' <EOL> has_configuration = True <EOL> configurable_fields = [ '<STR_LIT>' , ] <EOL> form = HashForm <EOL> def work ( self , document ) : <EOL> """<STR_LIT>""" <EOL> method = self . get_option ( '<STR_LIT>' , document . get_docrule ( ) ) <EOL> return HashCodeWorker ( self . method ) . work_store ( document , method ) <EOL> class HashCodeValidationOnRetrievalPlugin ( Plugin , BeforeRetrievalPluginPoint ) : <EOL> """<STR_LIT>""" <EOL> title = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> plugin_type = "<STR_LIT>" <EOL> method = '<STR_LIT>' <EOL> has_configuration = True <EOL> configurable_fields = [ '<STR_LIT>' , ] <EOL> form = HashForm <EOL> def work ( self , document ) : <EOL> """<STR_LIT>""" <EOL> method = self . get_option ( '<STR_LIT>' , document . get_docrule ( ) ) <EOL> return HashCodeWorker ( self . method ) . work_retrieve ( document , method ) <EOL> class HashCodeWorker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method ) : <EOL> self . method = method <EOL> def get_hash ( self , document , method , salt = settings . SECRET_KEY ) : <EOL> """<STR_LIT>""" <EOL> h = hashlib . new ( method ) <EOL> h . update ( document ) <EOL> h . update ( salt ) <EOL> return h . hexdigest ( ) <EOL> def work_store ( self , document , method ) : <EOL> """<STR_LIT>""" <EOL> new_hashcode = self . get_hash ( document . get_file_obj ( ) . read ( ) , method ) <EOL> document . set_hashcode ( new_hashcode ) <EOL> document . save_hashcode ( new_hashcode ) <EOL> return document <EOL> def work_retrieve ( self , document , method ) : <EOL> """<STR_LIT>""" <EOL> if document . get_file_obj ( ) : <EOL> hashcode = document . get_hashcode ( ) <EOL> new_hashcode = self . get_hash ( document . get_file_obj ( ) . read ( ) , method ) <EOL> if hashcode and not ( new_hashcode == hashcode ) : <EOL> raise PluginError ( "<STR_LIT>" , <NUM_LIT> ) <EOL> return document </s>
