def save_asset(request):
    asset = request.validated['asset']
    if asset.mode == u'test':
        set_modetest_titles(asset)
    patch = get_revision_changes(asset.serialize("plain"),
                                 request.validated['asset_src'])
    if patch:
        now = get_now()
        status_changes = [
            p for p in patch if not p['path'].startswith('/bids/')
            and p['path'].endswith("/status") and p['op'] == "replace"
        ]
        for change in status_changes:
            obj = resolve_pointer(asset, change['path'].replace('/status', ''))
            if obj and hasattr(obj, "date"):
                date_path = change['path'].replace('/status', '/date')
                if obj.date and not any(
                    [p for p in patch if date_path == p['path']]):
                    patch.append({
                        "op": "replace",
                        "path": date_path,
                        "value": obj.date.isoformat()
                    })
                elif not obj.date:
                    patch.append({"op": "remove", "path": date_path})
                obj.date = now
        asset.revisions.append(
            type(asset).revisions.model_class({
                'author': request.authenticated_userid,
                'changes': patch,
                'rev': asset.rev
            }))
        old_dateModified = asset.dateModified
        if getattr(asset, 'modified', True):
            asset.dateModified = now
        try:
            asset.store(request.registry.db)
        except ModelValidationError, e:
            for i in e.message:
                request.errors.add('body', i, e.message[i])
            request.errors.status = 422
        except ResourceConflict, e:  # pragma: no cover
            request.errors.add('body', 'data', str(e))
            request.errors.status = 409
Beispiel #2
0
    def test_set_modetest_titles(self):
        class Item(object):
            def __init__(self, title=None):
                self.title = title
                self.title_en = title
                self.title_ru = title

        item = Item('test')
        set_modetest_titles(item)

        self.assertEqual(
            item.title,
            u'[\u0422\u0415\u0421\u0422\u0423\u0412\u0410\u041d\u041d\u042f] test'
        )
        self.assertEqual(item.title_en, u'[TESTING] test')
        self.assertEqual(
            item.title_ru,
            u'[\u0422\u0415\u0421\u0422\u0418\u0420\u041e\u0412\u0410\u041d\u0418\u0415] test'
        )
Beispiel #3
0
def save_lot(request):
    lot = request.validated['lot']
    if lot.mode == u'test':
        set_modetest_titles(lot)
    patch = get_revision_changes(lot.serialize("plain"), request.validated['lot_src'])
    if patch:
        revision = prepare_revision(lot, patch, request.authenticated_userid)
        lot.revisions.append(type(lot).revisions.model_class(revision))
        old_dateModified = lot.dateModified
        if getattr(lot, 'modified', True):
            lot.dateModified = get_now()
        try:
            lot.store(request.registry.db)
        except ModelValidationError, e:
            for i in e.message:
                request.errors.add('body', i, e.message[i])
            request.errors.status = 422
        except ResourceConflict, e:  # pragma: no cover
            request.errors.add('body', 'data', str(e))
            request.errors.status = 409