示例#1
0
def subject_new():
    params = request.form or request.get_json()
    try:
        company_id = g.user.company_id
        subject_type = int(params['subject_type'])
        visitor_type = SubjectType.TYPE_VISITOR if params.get('visitor_type') is None else int(params['visitor_type'])
        name = params.get('name', '')
        email = params.get('email', '')
        phone = params.get('phone', '')
        gender = int(params.get('gender', Gender.MALE))
        avatar = params.get('avatar', '')
        department = params.get('department', '')
        title = params.get('title', '')
        description = params.get('description', '')
        start_time = int(params['start_time']) if subject_type != SubjectType.TYPE_EMPLOYEE else 0
        end_time = int(params['end_time']) if subject_type != SubjectType.TYPE_EMPLOYEE else 0
        photo_ids = params['photo_ids'] if 'photo_ids' in params else []
        purpose = int(params.get('purpose', VisitorPurpose.OTHER))
        interviewee = params.get('interviewee', '')
        come_from = params.get('come_from', '')
        job_number = params.get('job_number', '')
        remark = params.get('remark', '')
        birthday = int(params.get('birthday', 0))
        entry_date = int(params.get('entry_date', 0))
    except:
        return error_result(ErrorCode.ERROR_INVALID_PARAM)

    if ((subject_type == SubjectType.TYPE_VISITOR and not g.user.has_permission(AccountPermission.ADD_VISITOR)) or
            (subject_type == SubjectType.TYPE_EMPLOYEE and not g.user.has_permission(AccountPermission.ADD_EMPLOYEE))):
        return error_result(ErrorCode.ERROR_PERMISSION_DENIED)

    # VIP type
    if subject_type == SubjectType.TYPE_VISITOR:
        subject_type = visitor_type

    if email and Subject.query.filter_by(email=email).first():
        return error_result(ErrorCode.ERROR_EMAIL_EXISTED)

    subject = Subject(company_id=company_id, subject_type=subject_type, name=name, email=email, department=department,
                      gender=gender, avatar=avatar, title=title, description=description, start_time=start_time,
                      end_time=end_time, password='******', purpose=purpose, interviewee=interviewee, phone=phone,
                      come_from=come_from, job_number=job_number, remark=remark, create_time=g.TIMESTAMP)
    if birthday:
        subject.birthday = datetime.date.fromtimestamp(birthday)
    if entry_date:
        subject.entry_date = datetime.date.fromtimestamp(entry_date)
    if subject.avatar:
        avatar = storage.save_image_base64(subject.avatar, 'avatar', sync=True)
        if avatar:
            subject.avatar = avatar
        DisplayDevice.query.filter_by(company_id=company_id).update({'user_info_timestamp': g.TIMESTAMP})
    try:
        db.session.add(subject)
        db.session.commit()
        update_company_data_version(subject.company, subject.id)
        _update_photos(subject, photo_ids)
        return success_result(subject.get_json(with_photos=True))
    except:
        db.session.rollback()
        return error_result(ErrorCode.ERROR_UNKNOWN)
示例#2
0
def add_visitor():
    params = request.form

    try:
        name = params['name']
        photo = request.files.get('photo')
        purpose = params.get('purpose')
        interviewee = params.get('interviewee')
        inviter_id = g.subject.id
        come_from = params.get('come_from')
        phone = params.get('phone')
        remark = params.get('remark')
        description = params.get('description')
        company_id = g.subject.company_id
        start_time = int(params.get('start_time', 0))
        end_time = int(params.get('end_time', 0))
        vip = bool(int(params.get('vip', False)))
        subject_type = int(params.get('subject_type', 1))
    except:
        import traceback
        print traceback.format_exc()
        return error_result(ErrorCode.ERROR_INVALID_PARAM)

    if not start_time or not end_time:
        start_time = g.TIMESTAMP - 5 * 60
        end_time = g.TIMESTAMP + 2 * 3600

    if vip:
        subject_type = SubjectType.TYPE_VIP

    if interviewee is None:
        interviewee = g.subject.name

    subject = Subject(company_id=company_id,
                      name=name,
                      subject_type=subject_type,
                      remark=remark,
                      description=description,
                      start_time=start_time,
                      end_time=end_time,
                      purpose=purpose,
                      interviewee=interviewee,
                      inviter_id=inviter_id,
                      come_from=come_from,
                      phone=phone,
                      password='******',
                      create_time=g.TIMESTAMP)
    db.session.add(subject)
    db.session.commit()

    if photo:
        photo, error = create_user_photo(photo, subject.company_id)
        if error:
            return error
        subject.photos.append(photo)
        update_company_data_version(g.subject.company, subject.id)

    return success_result(subject.get_json())
示例#3
0
def subject_import_photo():
    try:
        photo = request.files['photo']
        name = photo.filename.rsplit('.', 1)[0]
    except:
        return error_result(ErrorCode.ERROR_INVALID_PARAM)
    photo, error = create_user_photo(photo, g.user.company_id)
    if error:
        return error
    subject = Subject(company_id=g.user.company_id, subject_type=SubjectType.TYPE_EMPLOYEE,
                      name=name, create_time=g.TIMESTAMP)
    subject.photos.append(photo)
    db.session.add(subject)
    db.session.commit()
    update_company_data_version(g.user.company, subject.id)
    return success_result(subject.get_json(with_photos=True))