def get_organization_list(): params = request.form or request.get_json() or request.args page, size = get_pagination(params) search = params.get('search') query = Organization.query if search: users = User.query.filter(User.username.contains(search)).all() ids = [user.organization_id for user in users] query = query.filter( or_(Organization.name.contains(search), Organization.id.in_(ids))) pagination = query.paginate(page, size) orgs = [] for org in pagination.items: data = org.get_json() data['company_count'] = org.companies.count() user = User.query.filter_by( organization_id=org.id, role_id=UserRole.ROLE_ORGANIZATION).first() if user: data['username'] = user.username orgs.append(data) return success_result(orgs, page_format(pagination))
def statistics_event(): position = request.args.get('position') page = int(request.args.get('page', 1)) size = int(request.args.get('size', 20)) unique = int(request.args.get('unique', 0)) start_time, end_time = _parse_start_end_time() query = Event.query.filter(Event.company_id == g.user.company_id, Event.timestamp > start_time, Event.timestamp <= end_time) if position: screen = Screen.query.filter_by(camera_position=position).first() if screen is None: return error_result(ErrorCode.ERROR_SCREEN_NOT_EXIST) query = query.filter(Event.screen_id == screen.id) events = query.order_by(Event.timestamp.desc()).all() result = [ event.get_json() for event in _filter_events(events, unique=bool(unique)) ] page_info = page_format(Pagination(None, page, size, len(result), None)) result = result[(page - 1) * size:page * size] return success_result(result, page_info)
def task_list(): params = request.args page, size = get_pagination(params) pagination = Task.query.order_by(Task.id.desc()).paginate(page, size) result = [] for task in pagination.items: result.append(task.get_json()) return success_result(result, page_format(pagination))
def get_attendance_record(): params = request.get_json() or request.form or request.args current, size = get_pagination(params) event_alias1 = db.aliased(Event) event_alias2 = db.aliased(Event) query = _build_query(params, Attendance.query.outerjoin(event_alias1, Attendance.earliest_event) .outerjoin(event_alias2, Attendance.latest_event) .join(Attendance.subject)) query = query.options(db.contains_eager(Attendance.earliest_event, alias=event_alias1), db.contains_eager(Attendance.latest_event, alias=event_alias2), db.contains_eager(Attendance.subject), ) pagination = query.paginate(current, size, False) page = page_format(pagination) result = [attendance.get_json(with_subject=True) for attendance in pagination.items] return success_result(result, page)
def get_attendance_stats(): params = request.get_json() or request.args current, size = get_pagination(params) date = params.get('date') search = params.get('search') try: start_date, end_date = month_range(date) except: return error_result(ErrorCode.ERROR_INVALID_PARAM) if search: search = search.replace('\\', '\\\\') query = db.session.query(Subject.id).filter(Subject.company_id == g.user.company_id, Subject.subject_type == SubjectType.TYPE_EMPLOYEE, db.or_(Subject.real_name.contains(search), Subject.pinyin.contains(search), )) pagination = fast_pagination(query, current, size) if not pagination.items: query = db.session.query(Subject.id).filter(Subject.company_id == g.user.company_id, Subject.subject_type == SubjectType.TYPE_EMPLOYEE, Subject.department.contains(search)) pagination = fast_pagination(query, current, size) else: query = db.session.query(Subject.id).filter(Subject.company_id == g.user.company_id, Subject.subject_type == SubjectType.TYPE_EMPLOYEE) pagination = fast_pagination(query, current, size) subjects = pagination.items subject_ids = [id for id, in subjects] subjects = Subject.query.outerjoin(Attendance, db.and_(Attendance.subject_id == Subject.id, Attendance.date < end_date, Attendance.date >= start_date))\ .options(db.contains_eager(Subject.all_attendances))\ .filter(Subject.id.in_(subject_ids)).all() calendar = AttendanceCalendar.get_or_create(g.user.company_id, datetime.date.today().year) result = [] for subject in subjects: subject_json = subject.get_json() stats = AttendanceStats() for _attendance in subject.all_attendances: if calendar.check_day(_attendance.date): stats.add_attendance(_attendance.clock_in, _attendance.clock_out, 1) subject_json['attendance'] = stats.to_dict() result.append(subject_json) return success_result(result, page_format(pagination))
def get_company_list(): params = request.args page, size = get_pagination(params) search = params.get('search') deployment = params.get('deployment') scenario = params.get('scenario') consigner = params.get('consigner') query = Company.query if consigner: query = query.filter_by(consigner=consigner) if search: users = User.query.filter(User.username.contains(search)).all() ids = map(lambda user: getattr(user, 'company_id'), users) query = query.filter( or_(Company.name.contains(search), Company.id.in_(ids))) if deployment and deployment != '0': query = query.filter(Company.deployment == deployment) if scenario: query = query.filter(Company.scenario == scenario) pagination = query.options(db.joinedload('screens')) \ .options(db.joinedload('display_devices')) \ .add_column(Company.subject_count).order_by(Company.id.desc()).paginate(page, size) company_list = [] for company, subject_count in pagination.items: company = company.get_json(with_status=True) company['subject_count'] = subject_count user = User.query.filter_by(company_id=company['id'], role_id=UserRole.ROLE_ADMIN).first() if user: company['username'] = user.username company_list.append(company) deployments = Company.get_deployment_count() deployments['0'] = sum(deployments.values()) data = { 'companies': company_list, 'scenarios': Company.get_scenario_count(), 'deployments': deployments } return success_result(data, page_format(pagination))
def company_change(): params = request.args page, size = get_pagination(params) query = g.user.organization.companies org = g.user.organization pagination = query.paginate(page, size) ret = [] for company in pagination.items: data = dict(name=company.name) user = User.query.filter_by(company_id=company.id, role_id=UserRole.ROLE_ADMIN).first() if user: data['username'] = user.username data['remark'] = company.remark data['company_id'] = company.id ret.append(data) return success_result(ret, page_format(pagination))
def subjects_list(): ORDER_TYPE_TIME = 'time' ORDER_TYPE_NAME = 'name' params = request.args name = params.get('name') category = params.get('category') order = params.get('order') subject_type = SubjectType.parse_param(category) if subject_type is None: 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) query = g.user.company.subjects.options(db.eagerload_all(Subject.photos)) if name: name = name.replace('\\', '\\\\') query = query.filter( Subject.real_name.contains(name) | Subject.pinyin.contains(name)) if subject_type is not None: if subject_type == SubjectType.TYPE_VISITOR: # VIP is visitor, too query = query.filter( or_(Subject.subject_type == SubjectType.TYPE_VISITOR, Subject.subject_type == SubjectType.TYPE_VIP)) else: query = query.filter_by(subject_type=subject_type) #TODO add index to start_time and pinyin ... if order == ORDER_TYPE_TIME: query = query.order_by(Subject.start_time.desc()) elif order == ORDER_TYPE_NAME: query = query.order_by(Subject.pinyin.asc()) else: query = query.order_by(Subject.start_time.desc()) current, size = get_pagination(params) pagination = query.paginate(current, size, False) page = page_format(pagination) ret = [ subject.get_json(with_photos=True, with_visitor_type=True) for subject in pagination.items ] return success_result(ret, page)
def events_list(cid): if cid not in g.user.organization.company_ids: return error_result(ErrorCode.ERROR_COMPANY_NOT_IN_ORGANIZATION) params = request.get_json() or request.form or request.args current, size = get_pagination(params) no_eager_query, query = _build_query(params, cid) pagination = fast_pagination(query, current, size, False, count_query=no_eager_query) page = page_format(pagination) result = [ event.get_json(with_subject=True, with_screen=True, with_subject_photos=True) for event in pagination.items ] return success_result(result, page)
def get_box_info(): params = request.args page, size = get_pagination(params) query = Box.query if params.get('search'): query = query.filter() query = query.outerjoin(Box.company).filter( or_(Company.name.contains(params.get('search')), Box.box_token.contains(params.get('search')))) if params.get('company_id'): query = query.join(Box.company).filter_by(id=params.get('company_id')) if params.get('version'): query = query.join(Box.box_version).filter( BoxVersion.version == params.get('version')) # 0 全部, 1 挂了, 2 正常 if params.get('status') == '1': query = query.filter(Box.heartbeat <= g.TIMESTAMP - 120) elif params.get('status') == '2': query = query.filter(or_(Box.heartbeat > g.TIMESTAMP - 120)) # 0 全部,1,已绑定,2 未绑定 if params.get('unused') == '1': query = query.filter(Box.company_id != None) elif params.get('unused') == '2': query = query.filter(Box.company_id == None) pagination = query.options(db.joinedload('box_version')) \ .options(db.joinedload('company')) \ .options(db.joinedload('all_screens')) \ .order_by(Box.id.desc()).paginate(page, size) boxes = [] for box in pagination.items: doc = box.get_json(with_box_version=True, with_company=True, with_all_screens=True) boxes.append(doc) used = Box.query.filter(Box.company_id != None).count() unused = Box.query.filter(Box.company_id == None).count() statistics = {'used': used, 'unused': unused, 'all': used + unused} data = {'boxes': boxes, 'statistics': statistics} return success_result(data, page_format(pagination))
def events_list(): params = request.get_json() or request.form or request.args category = params.get('category', 'user') if category == 'warning' and not g.user.has_permission( AccountPermission.REVIEW_STRANGER): return error_result(ErrorCode.ERROR_PERMISSION_DENIED) current, size = get_pagination(params) no_eager_query, query = _build_query(params, g.user.company_id) clear_unread_warnings(params) pagination = fast_pagination(query, current, size, False, count_query=no_eager_query) page = page_format(pagination) result = [ event.get_json(with_subject=True, with_screen=True, with_subject_photos=True) for event in pagination.items ] return success_result(result, page)