Show More
@@ -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 = '/' |
@@ -5085,7 +5087,7 b' class UserBookmark(Base, BaseModel):' | |||
|
5085 | 5087 | .all() |
|
5086 | 5088 | |
|
5087 | 5089 | def __unicode__(self): |
|
5088 |
return u'<UserBookmark(% |
|
|
5090 | return u'<UserBookmark(%s @ %r)>' % (self.position, self.redirect_url) | |
|
5089 | 5091 | |
|
5090 | 5092 | |
|
5091 | 5093 | class FileStore(Base, BaseModel): |
@@ -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): |
@@ -5170,6 +5176,42 b' class FileStore(Base, BaseModel):' | |||
|
5170 | 5176 | return store_entry |
|
5171 | 5177 | |
|
5172 | 5178 | @classmethod |
|
5179 | def store_metadata(cls, file_store_id, args, commit=True): | |
|
5180 | file_store = FileStore.get(file_store_id) | |
|
5181 | if file_store is None: | |
|
5182 | return | |
|
5183 | ||
|
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 | ||
|
5198 | meta_entry = FileStoreMetadata() | |
|
5199 | meta_entry.file_store = file_store | |
|
5200 | meta_entry.file_store_meta_section = section | |
|
5201 | meta_entry.file_store_meta_key = key | |
|
5202 | meta_entry.file_store_meta_value_type = value_type | |
|
5203 | meta_entry.file_store_meta_value = value | |
|
5204 | ||
|
5205 | Session().add(meta_entry) | |
|
5206 | ||
|
5207 | try: | |
|
5208 | if commit: | |
|
5209 | Session().commit() | |
|
5210 | except IntegrityError: | |
|
5211 | Session().rollback() | |
|
5212 | raise ArtifactMetadataDuplicate('Duplicate section/key found for this file.') | |
|
5213 | ||
|
5214 | @classmethod | |
|
5173 | 5215 | def bump_access_counter(cls, file_uid, commit=True): |
|
5174 | 5216 | FileStore().query()\ |
|
5175 | 5217 | .filter(FileStore.file_uid == file_uid)\ |
@@ -5185,7 +5227,7 b' class FileStore(Base, BaseModel):' | |||
|
5185 | 5227 | class FileStoreMetadata(Base, BaseModel): |
|
5186 | 5228 | __tablename__ = 'file_store_metadata' |
|
5187 | 5229 | __table_args__ = ( |
|
5188 | UniqueConstraint('file_store_meta_section', 'file_store_meta_key'), | |
|
5230 | UniqueConstraint('file_store_id', 'file_store_meta_section', 'file_store_meta_key'), | |
|
5189 | 5231 | Index('file_store_meta_section_idx', 'file_store_meta_section'), |
|
5190 | 5232 | Index('file_store_meta_key_idx', 'file_store_meta_key'), |
|
5191 | 5233 | base_table_args |
@@ -5220,19 +5262,28 b' class FileStoreMetadata(Base, BaseModel)' | |||
|
5220 | 5262 | |
|
5221 | 5263 | file_store = relationship('FileStore', lazy='joined') |
|
5222 | 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 | ||
|
5223 | 5271 | @hybrid_property |
|
5224 | 5272 | def file_store_meta_value(self): |
|
5225 | v = self._file_store_meta_value | |
|
5226 | _type = self._file_store_meta_value | |
|
5227 | if _type: | |
|
5228 | _type = self._file_store_meta_value.split('.')[0] | |
|
5229 | # decode the encrypted value | |
|
5230 | if '.encrypted' in self._file_store_meta_value: | |
|
5273 | val = self._file_store_meta_value | |
|
5274 | ||
|
5275 | if self._file_store_meta_value_type: | |
|
5276 | # e.g unicode.encrypted == unicode | |
|
5277 | _type = self._file_store_meta_value_type.split('.')[0] | |
|
5278 | # decode the encrypted value if it's encrypted field type | |
|
5279 | if '.encrypted' in self._file_store_meta_value_type: | |
|
5231 | 5280 | cipher = EncryptedTextValue() |
|
5232 | v = safe_unicode(cipher.process_result_value(v, None)) | |
|
5233 | ||
|
5281 | val = safe_unicode(cipher.process_result_value(val, None)) | |
|
5282 | # do final type conversion | |
|
5234 | 5283 | converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode'] |
|
5235 |
|
|
|
5284 | val = converter(val) | |
|
5285 | ||
|
5286 | return val | |
|
5236 | 5287 | |
|
5237 | 5288 | @file_store_meta_value.setter |
|
5238 | 5289 | def file_store_meta_value(self, val): |
@@ -5250,11 +5301,19 b' class FileStoreMetadata(Base, BaseModel)' | |||
|
5250 | 5301 | @file_store_meta_value_type.setter |
|
5251 | 5302 | def file_store_meta_value_type(self, val): |
|
5252 | 5303 | # e.g unicode.encrypted |
|
5253 | if val.split('.')[0] not in self.SETTINGS_TYPES: | |
|
5254 | raise Exception('type must be one of %s got %s' | |
|
5255 | % (self.SETTINGS_TYPES.keys(), val)) | |
|
5304 | self.valid_value_type(val) | |
|
5256 | 5305 | self._file_store_meta_value_type = val |
|
5257 | 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 | ||
|
5258 | 5317 | def __repr__(self): |
|
5259 | 5318 | return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section, |
|
5260 | 5319 | self.file_store_meta_key, self.file_store_meta_value) |
@@ -5204,7 +5204,7 b' class FileStore(Base, BaseModel):' | |||
|
5204 | 5204 | class FileStoreMetadata(Base, BaseModel): |
|
5205 | 5205 | __tablename__ = 'file_store_metadata' |
|
5206 | 5206 | __table_args__ = ( |
|
5207 | UniqueConstraint('file_store_meta_section', 'file_store_meta_key'), | |
|
5207 | UniqueConstraint('file_store_id', 'file_store_meta_section', 'file_store_meta_key'), | |
|
5208 | 5208 | Index('file_store_meta_section_idx', 'file_store_meta_section'), |
|
5209 | 5209 | Index('file_store_meta_key_idx', 'file_store_meta_key'), |
|
5210 | 5210 | base_table_args |
General Comments 0
You need to be logged in to leave comments.
Login now