##// END OF EJS Templates
artifacts: refactor metadata code...
marcink -
r3997:823cbf31 default
parent child Browse files
Show More
@@ -157,3 +157,15 b' class VCSServerUnavailable(HTTPBadGatewa'
157 157 if message:
158 158 self.explanation += ': ' + message
159 159 super(VCSServerUnavailable, self).__init__()
160
161
162 class ArtifactMetadataDuplicate(ValueError):
163
164 def __init__(self, *args, **kwargs):
165 self.err_section = kwargs.pop('err_section', None)
166 self.err_key = kwargs.pop('err_key', None)
167 super(ArtifactMetadataDuplicate, self).__init__(*args, **kwargs)
168
169
170 class ArtifactMetadataBadValueType(ValueError):
171 pass
@@ -67,6 +67,8 b' from rhodecode.lib.ext_json import json'
67 67 from rhodecode.lib.caching_query import FromCache
68 68 from rhodecode.lib.encrypt import AESCipher, validate_and_get_enc_data
69 69 from rhodecode.lib.encrypt2 import Encryptor
70 from rhodecode.lib.exceptions import (
71 ArtifactMetadataDuplicate, ArtifactMetadataBadValueType)
70 72 from rhodecode.model.meta import Base, Session
71 73
72 74 URL_SEP = '/'
@@ -5146,6 +5148,10 b' class FileStore(Base, BaseModel):'
5146 5148 repo_group = relationship('RepoGroup', lazy='joined')
5147 5149
5148 5150 @classmethod
5151 def get_by_store_uid(cls, file_store_uid):
5152 return FileStore.query().filter(FileStore.file_uid == file_store_uid).scalar()
5153
5154 @classmethod
5149 5155 def create(cls, file_uid, filename, file_hash, file_size, file_display_name='',
5150 5156 file_description='', enabled=True, hidden=False, check_acl=True,
5151 5157 user_id=None, scope_user_id=None, scope_repo_id=None, scope_repo_group_id=None):
@@ -5176,6 +5182,19 b' class FileStore(Base, BaseModel):'
5176 5182 return
5177 5183
5178 5184 for section, key, value, value_type in args:
5185 has_key = FileStoreMetadata().query() \
5186 .filter(FileStoreMetadata.file_store_id == file_store.file_store_id) \
5187 .filter(FileStoreMetadata.file_store_meta_section == section) \
5188 .filter(FileStoreMetadata.file_store_meta_key == key) \
5189 .scalar()
5190 if has_key:
5191 msg = 'key `{}` already defined under section `{}` for this file.'\
5192 .format(key, section)
5193 raise ArtifactMetadataDuplicate(msg, err_section=section, err_key=key)
5194
5195 # NOTE(marcink): raises ArtifactMetadataBadValueType
5196 FileStoreMetadata.valid_value_type(value_type)
5197
5179 5198 meta_entry = FileStoreMetadata()
5180 5199 meta_entry.file_store = file_store
5181 5200 meta_entry.file_store_meta_section = section
@@ -5185,8 +5204,12 b' class FileStore(Base, BaseModel):'
5185 5204
5186 5205 Session().add(meta_entry)
5187 5206
5188 if commit:
5189 Session().commit()
5207 try:
5208 if commit:
5209 Session().commit()
5210 except IntegrityError:
5211 Session().rollback()
5212 raise ArtifactMetadataDuplicate('Duplicate section/key found for this file.')
5190 5213
5191 5214 @classmethod
5192 5215 def bump_access_counter(cls, file_uid, commit=True):
@@ -5239,20 +5262,28 b' class FileStoreMetadata(Base, BaseModel)'
5239 5262
5240 5263 file_store = relationship('FileStore', lazy='joined')
5241 5264
5265 @classmethod
5266 def valid_value_type(cls, value):
5267 if value.split('.')[0] not in cls.SETTINGS_TYPES:
5268 raise ArtifactMetadataBadValueType(
5269 'value_type must be one of %s got %s' % (cls.SETTINGS_TYPES.keys(), value))
5270
5242 5271 @hybrid_property
5243 5272 def file_store_meta_value(self):
5244 v = self._file_store_meta_value
5245 _type = self._file_store_meta_value
5246 if _type:
5273 val = self._file_store_meta_value
5274
5275 if self._file_store_meta_value_type:
5247 5276 # e.g unicode.encrypted == unicode
5248 _type = self._file_store_meta_value.split('.')[0]
5249 # decode the encrypted value
5277 _type = self._file_store_meta_value_type.split('.')[0]
5278 # decode the encrypted value if it's encrypted field type
5250 5279 if '.encrypted' in self._file_store_meta_value_type:
5251 5280 cipher = EncryptedTextValue()
5252 v = safe_unicode(cipher.process_result_value(v, None))
5253
5254 converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode']
5255 return converter(v)
5281 val = safe_unicode(cipher.process_result_value(val, None))
5282 # do final type conversion
5283 converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode']
5284 val = converter(val)
5285
5286 return val
5256 5287
5257 5288 @file_store_meta_value.setter
5258 5289 def file_store_meta_value(self, val):
@@ -5270,11 +5301,19 b' class FileStoreMetadata(Base, BaseModel)'
5270 5301 @file_store_meta_value_type.setter
5271 5302 def file_store_meta_value_type(self, val):
5272 5303 # e.g unicode.encrypted
5273 if val.split('.')[0] not in self.SETTINGS_TYPES:
5274 raise Exception('type must be one of %s got %s'
5275 % (self.SETTINGS_TYPES.keys(), val))
5304 self.valid_value_type(val)
5276 5305 self._file_store_meta_value_type = val
5277 5306
5307 def __json__(self):
5308 data = {
5309 'artifact': self.file_store.file_uid,
5310 'section': self.file_store_meta_section,
5311 'key': self.file_store_meta_key,
5312 'value': self.file_store_meta_value,
5313 }
5314
5315 return data
5316
5278 5317 def __repr__(self):
5279 5318 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section,
5280 5319 self.file_store_meta_key, self.file_store_meta_value)
General Comments 0
You need to be logged in to leave comments. Login now