def update(self, _id, **kwargs): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) if kwargs.get("name"): other = Attribute.get_by(name=kwargs['name'], first=True, to_dict=False) if other and other.id != attr.id: return abort(400, "Attribute name <{0}> cannot be duplicate!".format(kwargs['name'])) if kwargs.get("alias"): other = Attribute.get_by(alias=kwargs['alias'], first=True, to_dict=False) if other and other.id != attr.id: return abort(400, "Attribute alias <{0}> cannot be duplicate!".format(kwargs['alias'])) choice_value = kwargs.pop("choice_value", False) is_choice = True if choice_value else False kwargs['is_choice'] = is_choice attr.update(flush=True, **kwargs) if is_choice: self._add_choice_values(attr.id, attr.value_type, choice_value) else: self._del_choice_values(attr.id, attr.value_type) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("update attribute error, {0}".format(str(e))) return abort(400, "update attribute <{0}> failed".format(_id)) AttributeCache.clean(attr) return attr.id
def get_attribute_by_id(self, _id): attr = Attribute.get_by_id(_id).to_dict() if attr and attr["is_choice"]: attr.update( dict(choice_value=self.get_choice_values( attr["id"], attr["value_type"]))) return attr
def test_delete_attribute(session, client): attr_ins = init_attributes(1)[0] url = "/api/v0.1/attributes/" + str(attr_ins.id) resp = client.delete(url) assert resp.status_code == 200 # attr should be soft delete attr_ins = Attribute.get_by_id(attr_ins.id) assert attr_ins.deleted is True assert attr_ins.deleted_at
def get(cls, key): if key is None: return attr = cache.get('Field::Name::{0}'.format(key)) \ or cache.get('Field::ID::{0}'.format(key)) \ or cache.get('Field::Alias::{0}'.format(key)) if attr is None: attr = Attribute.get_by(name=key, first=True, to_dict=False) \ or Attribute.get_by_id(key) \ or Attribute.get_by(alias=key, first=True, to_dict=False) if attr is not None: cls.set(attr) return attr
def get(cls, key): if key is None: return attr = cache.get(cls.PREFIX_NAME.format(key)) attr = attr or cache.get(cls.PREFIX_ID.format(key)) attr = attr or cache.get(cls.PREFIX_ALIAS.format(key)) if attr is None: attr = Attribute.get_by(name=key, first=True, to_dict=False) attr = attr or Attribute.get_by_id(key) attr = attr or Attribute.get_by( alias=key, first=True, to_dict=False) if attr is not None: cls.set(attr) return attr
def test_create_attribute(session, client): url = "/api/v0.1/attributes" payload = {"name": "region", "alias": "区域", "value_type": "2"} resp = client.post(url, json=payload) # check resp status code and content assert resp.status_code == 200 assert resp.json["attr_id"] # check there is a attribute in database attr_id = resp.json["attr_id"] attr_ins = Attribute.get_by_id(attr_id) assert attr_ins.id == attr_id assert attr_ins.name == "region" assert attr_ins.alias == "区域"
def test_update_attribute(session, client): attr_ins = init_attributes(1)[0] url = "/api/v0.1/attributes/" + str(attr_ins.id) payload = { "name": "update", } resp = client.put(url, json=payload) # check resp status code and content assert resp.status_code == 200 assert resp.json["attr_id"] == attr_ins.id # check attribute updated in database attr_ins = Attribute.get_by_id(attr_ins.id) assert attr_ins.name == "update"
def delete(_id): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) name = attr.name if attr.is_choice: choice_table = type_map["choice"].get(attr.value_type) db.session.query(choice_table).filter(choice_table.attr_id == _id).delete() # FIXME: session conflict db.session.flush() AttributeCache.clean(attr) attr.soft_delete() for i in CITypeAttribute.get_by(attr_id=_id, to_dict=False): i.soft_delete() for i in PreferenceShowAttributes.get_by(attr_id=_id, to_dict=False): i.soft_delete() return name
def update(self, _id, **kwargs): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) choice_value = kwargs.pop("choice_value", False) is_choice = True if choice_value else False attr.update(flush=True, **kwargs) if is_choice: self._add_choice_values(attr.id, attr.value_type, choice_value) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("update attribute error, {0}".format(str(e))) return abort(400, "update attribute <{0}> failed".format(_id)) AttributeCache.clean(attr) return attr.id