def generator(company): z = zipfile.ZipFile(upload, 'r') data = json.loads(z.read('index.json')) total = len(data) now = 0 email_dup = 0 try: for subject_data in data: now += 1 error_code = None try: photos = [] success_photo = failed_photo = 0 for photo in subject_data['photos']: photo, error = create_user_photo(StringIO.StringIO(z.read(photo)), company.id) if not error: photos.append(photo) success_photo += 1 else: failed_photo += 1 del subject_data['id'] del subject_data['photos'] subject_data['company_id'] = company.id subject_data['avatar'] = '' date = subject_data['birthday'] if type(date) is int: subject_data['birthday'] = datetime.date.fromtimestamp(date) date = subject_data['entry_date'] if type(date) is int: subject_data['entry_date'] = datetime.date.fromtimestamp(date) # 判断email是否已经存在 email = subject_data['email'] if email and Subject.query.filter_by(email=email).first(): print email email_dup += 1 error_code = ErrorCode.ERROR_EMAIL_EXISTED if error_code is None: subject = Subject(**subject_data) subject.photos = photos db.session.add(subject) except: logger.error('/subject/import.zip exception: ' + str(traceback.format_exc())) error_code = ErrorCode.ERROR_UNKNOWN # 建议email字段默认为空字符串,如果有重复,则赋值 yield json.dumps( dict(now=now, total=total, name=subject_data.get('name'), success=success_photo, fail=failed_photo, email=email if error_code == ErrorCode.ERROR_EMAIL_EXISTED else '')) + '\n' print 'email duplicate', email_dup update_company_data_version(company, 0) z.close() except: db.session.rollback() z.close()
def subject_invite_page(): subject_id = request.args.get('subject_id') token = request.args.get('token') if not subject_id or not token: return "非法请求" try: subject_id = int(subject_id) except: return "非法请求" subject = Subject.query.filter(Subject.id == subject_id, Subject.subject_type != SubjectType.TYPE_EMPLOYEE).first() if not subject: return "链接已失效" if g.TIMESTAMP > subject.end_time or subject.job_number != token: return "链接已失效" if request.method == 'GET': return render_template('page/subject/invite.html', subject=subject.get_json(with_photos=True), error='') elif request.method == 'POST': payload = request.files['photo'] photo, err = create_user_photo(payload, subject.company_id) if err: return err db.session.add(photo) db.session.commit() _update_photos(subject, [photo.id]) update_company_data_version(subject.company, subject.id) return success_result(photo.get_json())
def subject_photo_create(): try: payload = request.files['photo'] subject_id = int(request.form['subject_id']) old_photo_id = int(request.form.get('old_photo_id', 0)) except: return error_result(ErrorCode.ERROR_INVALID_PARAM) if not payload: return error_result(ErrorCode.ERROR_INVALID_PARAM) if g.subject.visitors.filter_by(id=subject_id).first() is None: return error_result(ErrorCode.ERROR_NOT_ALLOWED) photo, error = create_user_photo(payload, g.subject.company_id) if error: return error photo.subject_id = subject_id db.session.add(photo) db.session.commit() #delete old photo if old_photo_id: old_photo = Photo.query.get(old_photo_id) if old_photo.subject.inviter_id != g.subject.id: return error_result(ErrorCode.ERROR_NOT_ALLOWED) storage.remove(old_photo.url) db.session.delete(old_photo) db.session.query(PhotoAlternative).filter(PhotoAlternative.subject_id == old_photo.subject_id). \ filter(PhotoAlternative.url == old_photo.url).delete() db.session.commit() if subject_id: update_company_data_version(g.subject.company, subject_id) return success_result(photo.get_json())
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())
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))
def subject_photo_create(): try: payload = request.files['photo'] subject_id = int(request.form.get('subject_id', 0)) old_photo_id = int(request.form.get('old_photo_id', 0)) except: return error_result(ErrorCode.ERROR_INVALID_PARAM) if not payload: return error_result(ErrorCode.ERROR_INVALID_PARAM) if subject_id: subject = Subject.query.filter_by(id=subject_id).first() if subject is None: return error_result(ErrorCode.ERROR_INVALID_PARAM) subject_type = subject.subject_type 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) photo, error = create_user_photo(payload, g.user.company_id) if error: return error # delete old photo if subject_id and old_photo_id: old_photo = Photo.query.get(old_photo_id) if old_photo and old_photo.subject_id == subject_id: storage.remove(old_photo.url) db.session.delete(old_photo) db.session.query(PhotoAlternative).filter(PhotoAlternative.subject_id == old_photo.subject_id). \ filter(PhotoAlternative.url == old_photo.url).delete() if subject_id: photo.subject_id = subject_id db.session.add(photo) db.session.commit() if subject_id: update_company_data_version(g.user.company, subject.id) return success_result(photo.get_json())
def add_visitor(): params = request.args or request.get_json() or request.form device, err = get_device_from_params(params) if err is not None: return error_result(err) name = params.get('name') vip = params.get('vip', False) photo = request.files.get('photo') company = device.company if not photo: error_result(ErrorCode.ERROR_INVALID_PARAM) if not name: name = generate_name() + u'(临时名)' user_photo, error = create_user_photo(photo, company.id) if error: return error subject_type = SubjectType.TYPE_VIP if vip else SubjectType.TYPE_VISITOR start_time = g.TIMESTAMP - 5 * 60 end_time = g.TIMESTAMP + 2 * 3600 subject = Subject(company_id=company.id, name=name, subject_type=subject_type, start_time=start_time, end_time=end_time) db.session.add(subject) db.session.commit() user_photo.subject_id = subject.id db.session.add(user_photo) db.session.commit() update_company_data_version(company, subject.id) return success_result(subject.get_data())