def __exit__(self, exc_type, exc_value, traceback): if exc_value is None: exc_type = _TryException exc_value = _TryException() if not settings.IS_UNIT_TEST: console_log("No error and transaction rollback successfully") super().__exit__(exc_type, exc_value, traceback)
def convert_filter(self, model, parts, value, lookup_type, raw): if self.filter: assert lookup_type == "exact", console_log( "filter 가 지정된 subfield 에 대해서는 exact 검색만 가능합니다.", lookup_type) return self.filter(model, value) assert lookup_type not in ("isnull", ), console_log( "subfield 에 대해 지원되지 않는 lookup_type 입니다.", lookup_type) return { "{}__{}__{}".format(self.field_name, LOOKUP_SEP.join(parts), lookup_type): json_encode(value) }
def main(**kwargs): parser = ArgumentParser(**kwargs) parser.add_argument("--input_file_path", type=str, default=None) parser.add_argument("--output_file_path", type=str, default=None) args = parser.parse_args() console_log("{} start".format(__file__)) with ReaderContext(path=args.input_file_path) as input_f, WriterContext( path=args.output_file_path) as output_f: for line in LineStripReader(input_f): output_f.write(line) output_f.write("\n") console_log("{} end".format(__file__))
def __set__(self, instance, value): cls, subfield_type, normalize, lf_name, field_name, subfield_name, create_only = ( instance.__class__, self.subfield_type, self.normalize, self.subfield_name, self.field_name, self.subfield_name, self.create_only, ) json_field = getattr(instance, field_name) if normalize and value is not None: old = value value = normalize(value) if instance.status == Status.NEW and old != value: if instance.raw is None: instance.raw = {} instance.raw[lf_name] = old # value 를 subfield_type 으로 변환. 따라서 json_decode() 는 형변환도 구현해야 함 value = json_decode(value, subfield_type) # old old = json_field[subfield_name] if callable(old): old = None old = json_decode(old, subfield_type) if old != value: # TODO : if 블럭 밖에 있으면 에러 발생. 장고 내부 코드 확인 후 조처 ( django.db.models.query ) """ if annotation_col_map: for attr_name, col_pos in annotation_col_map.items(): setattr(obj, attr_name, row[col_pos]) """ if create_only: assert instance.status in (Status.CREATING, Status.NEW), console_log( "{}.{} = {} ({})".format( self.owner.__name__, self.subfield_name, value, instance.status)) assert old is None assert value is not None assert self.check_schema(value), console_log( "{}.check_schema({}) is fail!".format(self.subfield_name, value)) instance.assert_changeable(field_name) json_field[subfield_name] = json_encode(value) instance.onchange_subfield(field_name, subfield_name, old, value) return old
def json_schema(d): if isinstance(d, dict): schema = {} schema.update(d) for k, v in d.items(): if isinstance(v, dict): schema[k] = json_schema(v) elif isinstance(v, list): schema[k] = json_schema(v) elif isinstance(v, int): schema[k] = 0 elif isinstance(v, str): schema[k] = "" else: schema[k] = None elif isinstance(d, list): schema = [] if d: v = d[0] if isinstance(v, dict): schema.append(json_schema(v)) elif isinstance(v, list): schema.append(json_schema(v)) elif isinstance(v, int): schema.append(0) elif isinstance(v, str): schema.append("") else: schema.append(None) else: assert False, console_log("d 가 json type 이 아닙니다", d) return schema
def check_schema(self, value): # callable if callable(value): return True # null if self.null is False: assert value is not None, console_log( "{}.{} 은 null 이 허용되지 않습니다.".format(self.owner.__name__, self.subfield_name)) # check, choices if value is not None: choices = self.choices if choices: if value not in choices: return False check = self.check if check: result = check(value) if not result: return False # subfield_type subfield_type = self.subfield_type if subfield_type is None or value is None: return True return isinstance(value, subfield_type)
def check_dirty_or_new_instances_with_no_save(self): # TODO : 관련 구조 정리 후에 NEW 에 대해서도 체크 problems = [ instance for instance in self.instances.values() if not instance._syncdb_required and instance.status == Status.DIRTY ] assert not problems, console_log( "값이 수정되었는데 save() 가 호출되지 않은 인스턴스가 존재합니다 : {}".format(problems))
def __new__(mcs, cls_name, bases, classdict): has_alias_fields = {k: v.alias for k, v in classdict.items() if isinstance(v, ValueSubfield) and v.alias} for k, alias in has_alias_fields.items(): # 중복되는 alias 가 있는경우 assert alias not in classdict, console_log("중복되는 alias 가 존재합니다. {}".format(alias)) # 상위클래스에 alias 의 이름이 존재하는 경우 SubfieldWrapper 인지 확인하고 pass if alias in bases[0].__dict__.keys(): base_alias = bases[0].__dict__[alias] assert isinstance(base_alias, SubfieldWrapper), console_log( "상위클래스에 SubfieldWrapper 가 아닌 {} 가 존재합니다.".format(alias) ) assert base_alias.original_subfield_name == k else: classdict[alias] = SubfieldWrapper(k) cls = super().__new__(mcs, cls_name, bases, classdict) t = getattr(Type, cls_name) t._model = cls assert cls.subfields return cls
def __setattr__(self, field_name, value): # __dict__ setter if field_name[0] == "_" or not self._is_initialized: self.__dict__[field_name] = value return # object setter # TODO : 모두 확정된 후 로컬상수변수로 대체하여 튜닝 if field_name in self.field_names_needs_object_setter: return object.__setattr__(self, field_name, value) # for overrides super().delete() if field_name == "id" and value is None: return # set field old = getattr(self, field_name) if old != value: # for django admin if value is None and field_name in ("data", "computed"): return # check pre condition self.assert_changeable(field_name) # change value if field_name == "data": for subfield, subvalue in value.items(): setattr(self, subfield, subvalue) elif field_name == "status": assert old.check_route(value), "해당 status 변경은 허용되지 않습니다: {} --> {}".format(old, value) self.__dict__["status"] = value elif field_name == "computed": assert not settings.IS_UNIT_TEST, "computed field 직접 세팅은 허용되지 않습니다." # for django admin console_log("try change of computed field is ignored") else: if self.status in (Status.NORMAL, Status.WORKING): self.__setattr__("status", Status.DIRTY) self.__dict__[field_name] = value # make revert patch if field_name not in self._mjson_revert_patch and field_name in self.field_names: self._mjson_revert_patch[field_name] = json_encode(old) # raw if field_name == "raw": assert self.status in (Status.CREATING, Status.NEW) self.init_data_by_raw()
def __call__(self, request): # 기존 로그인/체크인 정보 복원 session, usk, ask = (request.session, settings.USER_SESSION_KEY, settings.ACTOR_SESSION_KEY) try: user = User.objects.get( id=session[usk]) if usk in session else None actor = Actor.objects.get( id=session[ask]) if ask in session else None except Exception as e: console_log(e) user = None actor = None # api 호출 tran_cls = ReadonlyTransaction if request.method == "GET" else Transaction try: with tran_cls(checkin_actor=actor, login_user=user) as tran: res = self.get_response(request) if getattr(request, "need_rollback", False): raise _NeedsRollbackException elif res.status_code >= 500: raise _Http500Exception for m in request._messages: if m.level == constants.ERROR: res.status_code = 400 except _Http500Exception: pass except _NeedsRollbackException: pass except Exception as e: res = render_exception(request, e, 500) # 바뀐 로그인/체크인 정보 변경 after_user, after_actor = tran.login_user, tran.checkin_actor if user != after_user: session[usk] = after_user.id if after_user else None if actor != after_actor: session[ask] = after_actor.id if after_actor else None return res
def init_db(): from django.contrib.auth import get_user_model from django.conf import settings from base.utils import console_log User = get_user_model() admin = User.objects.filter(username="******").order_by("-id").first() if admin is None: admin = User() admin.username = "******" admin.set_password(settings.PASSWORD) admin.is_superuser = True admin.is_staff = True admin.save() console_log("admin created") else: console_log("admin already created. so passed") return False from base.transaction import Transaction from server.specs.spec_db_initialize import InitializeDBSpec from django.db import IntegrityError try: with Transaction(): InitializeDBSpec().setUp() except IntegrityError: console_log("InitializeDBSpec already called. so passed") return False return True
def _init_no_inited_subfields(self): for k, v in self.data.items(): if v is None and k[0] != "_": subfield = self.get_subfield(k) assert subfield.null, console_log("{} 는 null 이 허용되지 않습니다.".format(subfield.subfield_name)) elif callable(v): self.data[k] = json_encode(safe_call(v, self)) for k, v in self.computed.items(): if v is None and k[0] != "_": subfield = self.get_subfield(k) assert subfield.null elif callable(v): self.computed[k] = json_encode(safe_call(v, self))
def hugh_iterator(self, chunk_size=CHUNK_SIZE): assert issubclass(self._iterable_class, ModelIterable) assert self.query.order_by in ((), ("id",)), console_log( "hugh_iterator() 가 허용되지 않는 order_by 가 적용되었습니다.", self.query.order_by ) assert self.query.extra_order_by == (), console_log( "hugh_iterator() 가 허용되지 않는 extra_order_by 가 적용되었습니다.", self.query.extra_order_by ) last_id = 0 last_index_in_chunk = chunk_size - 1 qs = None qs_last_index = None total_count = self.count() for i in range(total_count): last_index_in_chunk += 1 if last_index_in_chunk >= chunk_size: last_index_in_chunk = 0 qs = self.filter(id__gt=last_id).order_by("id")[:chunk_size] qs_last_index = len(qs) - 1 if last_index_in_chunk <= qs_last_index: instance = qs[last_index_in_chunk] last_id = instance.id yield instance
def assert_changeable(self, field_name=None): super().assert_changeable(field_name) if field_name: assert field_name not in ("id", "type"), "id, type field 는 절대 수정할 수 없습니다." if field_name == "raw": assert self.status in (Status.CREATING, Status.NEW), "raw 컬럼은 최초 생성시에만 세팅 가능합니다." elif settings.IS_UNIT_TEST: assert field_name == "data" or self._force_changer_count >= 1, console_log( "data field 를 제외하면 기본적으로 수정 금지입니다 : {}".format(field_name) ) else: # for django admin assert ( field_name in ("data", "computed") or self._force_changer_count >= 1 ), "data field 를 제외하면 기본적으로 수정 금지입니다. computed field 는 수정되지 않고 무시됩니다."
def model(self): assert self._model, console_log( "Type.{} 에 매핑되는 model 이 없습니다.".format(self)) return self._model
def on_create(self): assert self.__class__.objects.count() == 0, console_log( "Singleton 모델의 객체는 1개를 초과할 수 없습니다.") self.uname = self.__class__.__name__
def rollback_working(self): assert self.status is Status.WORKING, console_log("Status 가 WORKING 일때만 호출이 가능합니다.") self._set_fields(status=Status.NORMAL)
def setUp(self): Dummy.objects.create(temp=1) Dummy.objects.create(temp=2) Dummy.objects.create(temp=3) console_log("end of setUp()")
def render_func_test(duration): console_log("test duration {}".format(duration))
def render(self, v): console_log(v)
def print(self, *args): console_log(*args)
def given(self): Dummy.objects.create(temp=1) Dummy.objects.create(temp=2) Dummy.objects.create(temp=3) console_log("end of given()")