Python flask TypeError:|:“ bool”和“ BinaryExpression”的不受支持的操作数类型

时间:2019-08-27 02:04:52

标签: python sqlalchemy

我正在尝试发送放置请求以更新学生标签,但是我在本地计算机上使用邮递员收到类型错误,我认为这与学生服务中的电子邮件有关,但我不是确定为什么。非常感谢您的帮助,因为我是python的新手,谢谢!

控制台:

  File "/Users/thomashunt/projects/ct-platform-api/apis/student_api.py", line 519, in put
    return StudentService.student_ldi_attendees(submission)
  File "/Users/thomashunt/projects/ct-platform-api/services/students.py", line 237, in student_ldi_attendees
    student = repo.find_by_email(Person.email)
  File "/Users/thomashunt/projects/ct-platform-api/services/database/person_data.py", line 27, in find_by_email
    return self.session.query(self.person_user).filter((Person.email == email) | (Person.alt_email == email)).first()
TypeError: unsupported operand type(s) for |: 'bool' and 'BinaryExpression'

ct平台API

@ns.route('/student-ldi-attendees')
class studentldiattendeesEndpoint(Resource):
    @SecurityService.requires_system
    @ns.expect(student_ldi_attendees_model, validate=True)
    @ns.marshal_with(student_person_model)
    def put(self):
        logging.info('student LDI 2019 tag appended')
        submission = studentldiattendees.from_dict(request.json)
        return StudentService.student_ldi_attendees(submission)

为学生服务

@staticmethod
def student_ldi_attendees(submission: studentldiattendees ) -> Person:
    repo = PersonData()
   # advisor = repo.find_by_email(submission.advisor_email)

    email = submission.email.lower()
    student = repo.find_by_email(Person.email)

    if not student:
        raise RecordNotFoundException('No Record with this email in the database')

    submission.set_model(Person)

    Person.student_detail.student_tags = submission.student_tags

    repo.session.commit()

    return student

person_data.py

class PersonData(BaseRepository):
    person_user = with_polymorphic(Person, '*')

    def find_by_id_list(self, id_list: List[str]) -> List[Person]:
        return self.session.query(self.person_user).filter(Person.id.in_(id_list)).all()

    def load(self, person_id: str) -> Person:
        return self.session.query(self.person_user).filter(Person.id == person_id).first()

    def list(self, page: int=1, page_size: int=100):
        page_query = self.session.query(self.person_user).paginate(page=page, per_page=page_size,
                                                                   max_per_page=page_size,
                                                                   error_out=False)
        return page_query.items
//line 27 below
    def find_by_email(self, email: str) -> Person:
        return self.session.query(self.person_user).filter((Person.email == email) | (Person.alt_email == email)).first()

    def save(self, data: Person, by_user_id: str = None) -> Person:
        if data.id is None:
            data.id = uuid.uuid4()
            data.created_by_user_id = by_user_id
            data.date_created = datetime.utcnow()
        else:
            if data.contact_detail and data.contact_detail.location_address:
                flag_modified(data.contact_detail, 'location_address')
            if data.student_detail and data.student_detail.location_address:
                flag_modified(data.student_detail, 'location_address')

        data.date_updated = data.date_updated or datetime.utcnow()
        data.updated_by_user_id = by_user_id or data.updated_by_user_id

        self.session.add(data)
        return data

    def delete(self, data: Person):
        self.session.delete(data)

学生外来者模型:

class studentldiattendees(object):
    def __init__(self):
        self.first_name = None
        self.last_name = None
        self.email = None
        self.student_tags: List[str] = None

    def set_model(self, model: Person):
        model.student_tags = self.student_tags
       # if self.student_tags:
       #     TagsEdit.set_model(self, model)
      #  else:
       #     model.student_tags = []
        model.first_name = self.first_name
        model.last_name = self.last_name
        model.email = self.email

    @staticmethod
    def from_dict(dct):
        print(dct)
        entity = studentldiattendees()
        entity.first_name = dct.get('first_name')
        entity.last_name = dct.get('last_name')
        entity.email = dct.get('email')
        entity.student_tags = TagsEdit.from_dict(dct.get('student_tags'))
        return entity

班级人士     类Person(db.Model):         表名 ='人'

    id = db.Column(db_ext.UUID(as_uuid=True), primary_key=True, server_default=sa_text("uuid_generate_v4()"))
    person_number = db.Column(db.Integer, db.Sequence('person_number_sequence'))
    date_created = db.Column(db.DateTime)
    date_updated = db.Column(db.DateTime)
    created_by_user_id = db.Column(db_ext.UUID(as_uuid=True))
    updated_by_user_id = db.Column(db_ext.UUID(as_uuid=True))
    first_name = db.Column(db.String(30))
    birth_name = db.Column(db.String(30))  # If different to preferred (first) name
    middle_name = db.Column(db.String(30))
    last_name = db.Column(db.String(30))
    email = db.Column(db.String(150))
    alt_email = db.Column(db.String(150))
    phone_numbers = db.Column(PhoneListDecorator)
    facebook_url = db.Column(db.Text(length=150))
    linkedin_url = db.Column(db.Text(length=150))
    twitter_url = db.Column(db.Text(length=150))
    instagram_url = db.Column(db.Text(length=150))
    gender = db.Column(db.String(10))
    is_advisor = db.Column(db.Boolean)
    is_client = db.Column(db.Boolean)
    is_student = db.Column(db.Boolean)
    is_deleted = db.Column(db.Boolean)
    deleted_by_person_id = db.Column(db.Text)
    date_deleted = db.Column(db.DateTime)    
    is_no_contact = db.Column(db.Boolean)
    set_no_contact_by_user_id = db.Column(db.String)
    student_detail = db.relationship("Student", uselist=False, lazy='joined')
    contact_detail = db.relationship("Contact", uselist=False, lazy='joined')
    person_user = db.Column(db.Text)

    __mapper_args__ = {
        'polymorphic_identity': 'person',
        'polymorphic_on': person_user,
        'with_polymorphic': '*'
    }

    def __init__(self):
        """
        Initialise object props we want to manage together. Do not need to do all props
        such as child sets and can let the database manage those
        """
        self.id: str = None
        self.date_created: datetime = None
        self.date_updated: datetime = None
        self.created_by_user_id: str = None
        self.updated_by_user_id: str = None
        self.first_name: str = None
        self.birth_name: str = None
        self.last_name: str = None
        self.middle_name: str = None
        self.email: str = None
        self.alt_email: str = None
        self.phone_numbers: List[PhoneNumber] = []
        self.facebook_url: str = None
        self.linkedin_url: str = None
        self.twitter_url: str = None
        self.instagram_url: str = None
        self.gender: str = None
        self.is_advisor: bool = False
        self.is_client: bool = False
        self.is_student: bool = False
        self.is_deleted: bool = False
        self.deleted_by_person_id: str = None
        self.is_no_contact: bool = False
        self.set_no_contact_by_user_id: str = None
        self.student_detail: Student = None
        self.contact_detail: Contact = None

    def get_formatted_name(self):
        name_parts = self.get_name_parts()
        if len(name_parts) == 0:
            return "Unknown Person"
        else:
            return " ".join(name_parts)

    def get_name_parts(self):
        name_parts = []
        if self.first_name:
            name_parts.append(self.first_name)
        if self.birth_name and self.birth_name != self.first_name:
            name_parts.append("({0})".format(self.birth_name))
        if self.last_name:
            name_parts.append(self.last_name)
        return name_parts

0 个答案:

没有答案