Ejemplo n.º 1
0
def screen_detail(sid):
    company = g.user.company
    screen = Screen.query.filter(Screen.company_id == company.id,
                                 Screen.id == sid).first()
    if screen is None:
        return error_result(ErrorCode.ERROR_SCREEN_NOT_EXIST)

    if request.method == 'GET':
        return success_result(screen.get_json())
    elif request.method == 'PUT':
        params = request.get_json()
        fields = ('box_id', 'name', 'camera_name', 'camera_address',
                  'camera_position', 'network_switcher',
                  'network_switcher_token'
                  'description', 'allow_all_subjects', 'allow_visitor',
                  'allowed_subject_ids')
        screen.update(fields, params)
        screen.display_devices.update({'reload_timestamp': g.TIMESTAMP})
        db.session.add(screen)
        update_company_data_version(company)
        return success_result(screen.get_json())
    elif request.method == 'DELETE':
        content = generate_delete_screen_task_content(company.id, sid)
        task = Task()
        task.name = u'删除相机'
        task.content = content
        db.session.add(task)
        db.session.commit()
        delete_screen.delay(task.id, company.id, sid)
        return success_result()
Ejemplo n.º 2
0
def add_task(request):
    context = RequestContext(request)

    edit_task = request.user.staff.role.editTask
    global g_parent_add
    if request.method == 'POST':

        my_dict = dict(request.POST.iterlists())

        name = my_dict['name'][0].strip()
        create_date = datetime.now()
        begin_date = request.POST.get('begin_date')
        end_date = request.POST.get('end_date')
        state = my_dict['state'][0].strip()
        importance = my_dict['importance'][0].strip()
        content = my_dict['content'][0].strip()
        finish = my_dict['finish'][0].strip()

        principal = None
        if my_dict.get('principal'):
            did = my_dict['principal'][0].strip()
            if did != '' and did != 0 and did != 'undefined':
                principal = Staff.objects.get(id=did)

        t_parent = None
        parent_v = []
        if my_dict.get('parent'):
            pid = my_dict['parent'][0].strip()
            if pid != 'None' and pid != '-1' and pid != 0:
                t_parent = Task.objects.get(id=pid)
                tmp_t = t_parent
                while tmp_t is not None:
                    parent_v.append(tmp_t.creator)
                    parent_v.append(tmp_t.principal)
                    tmp_t = tmp_t.parent
        else:
            pid = 0

        t_group = []
        if my_dict.get('group'):
            tmp_group = my_dict['group']
            for tg in tmp_group:
                t_group.append(Group.objects.get(id=tg))

        executor = []
        if my_dict.get("executor"):
            tmp_exe = my_dict["executor"]
            for e in tmp_exe:
                executor.append(Staff.objects.get(id=e))

        visible = []
        if my_dict.get("visible"):
            tmp_visible = my_dict["visible"]
            for v in tmp_visible:
                visible.append(Staff.objects.get(id=v))
        if name == '':
            messages.error(request, '名称不能为空!')
        elif re.search(r'/|\\|:|\*|<|>|\?|:|\"|\|', name):
            messages.error(request, '名称不能包含下列任何字符“ \ / : * ? " < > | ”')
        elif t_parent == None and Task.objects.filter(name=name, mptt_level=0, istemp=0).count() > 0:
            messages.error(request, '名称已存在!')
        elif begin_date == '' or end_date == '':
            messages.error(request, '开始日期和结束日期不能为空!')
        elif content == '':
            messages.error(request, '任务内容不能为空!')
        elif principal is None:
            messages.error(request, '负责人不能为空!')
        elif (not edit_task and pid == 0) or pid == '-1':
            messages.error(request, '您没有权限创建该任务!')
        elif not valid_time(begin_date):
            messages.error(request, '开始日期格式错误!')
        elif not valid_time(end_date):
            messages.error(request, '结束日期格式错误!')
        else:
            tsk = Task()
            tsk.name = name
            tsk.create_date = create_date
            tsk.begin_date = begin_date
            tsk.end_date = end_date
            tsk.state = state
            tsk.importance = importance
            tsk.content = content
            ff = finish
            if len(ff) > 50:
                finish = ff[0:49]
            tsk.finish = finish
            tsk.parent = t_parent
            tsk.creator = request.user.staff
            tsk.principal = principal

            tsk.save()

            tsk.group.clear()
            for g in t_group:
                tsk.group.add(g)

            tsk.executor.clear()
            for e in executor:
                tsk.executor.add(e)

            tsk.viewing.clear()
            tsk.viewing.add(request.user.staff)
            tsk.viewing.add(principal)

            for v in visible:
                if not tsk.viewing.filter(id=v.id):
                    tsk.viewing.add(v)

            for e in executor:
                if not tsk.viewing.filter(id=e.id):
                    tsk.viewing.add(e)
            for tp in parent_v:
                if not tsk.viewing.filter(id=tp.id):
                    tsk.viewing.add(tp)
            # cache.clear()
            messages.success(request, "创建任务成功!")
            if my_dict.get('temp'):
                temp_id = my_dict['temp'][0].strip()
                if temp_id != '-1':
                    createTaskBy(request, temp_id, tsk.id)

                    #parent = Task.objects.all()
        staff = Staff.objects.filter(visible=True)
        group = Group.objects.all()
        ngroup = Staff.objects.filter(group=None)
        temps = Task.objects.filter(Q(istemp=1) & Q(parent=None))
        return render_to_response('task/add_task.html',
                                  {'parent': g_parent_add, 'staff': request.user.staff, 'group': group,
                                   'ngroup': ngroup,
                                   't_name': name, 't_begin_date': begin_date, 't_end_data': end_date, 't_state': state,
                                   't_importance': importance, 't_content': content, 't_finish': finish,
                                   't_principal': principal, 't_parent': t_parent, 't_group': t_group, 'temps': temps,
                                   't_executor': executor, 't_visible': visible, 'edit_task': edit_task}, context)
    else:

        p_parent = []
        task_execute = request.user.staff.task
        task_cp = Task.objects.filter(
            Q(istemp=False) & (Q(creator=request.user.staff) | Q(principal=request.user.staff)))
        task_viewing = request.user.staff.viewing.all()
        for t1 in task_execute.all():
            has = False
            for p in p_parent:
                if p == t1:
                    has = True
                    break
            if not has:
                p_parent.append(t1)

        for t2 in task_cp:
            has = False
            for p in p_parent:
                if p == t2:
                    has = True
                    break
            if not has:
                p_parent.append(t2)
        for t3 in task_viewing:
            p_parent.append(t3)
        g_parent_add = Task.objects.filter(istemp=0)
        for p in g_parent_add:
            has = False
            for t1 in p_parent:
                if p == t1:
                    has = True
                    break
            if not has:
                p.name = "没有权限的项目"
                p.id = -1

        staff = Staff.objects.filter(visible=True)
        group = Group.objects.all()
        ngroup = Staff.objects.filter(group=None)
        temps = Task.objects.filter(Q(istemp=1) & Q(parent=None))
        return render_to_response('task/add_task.html',
                                  {'parent': g_parent_add, 'staff': request.user.staff, 'group': group,
                                   'edit_task': edit_task, 'ngroup': ngroup, 'temps': temps}, context)
Ejemplo n.º 3
0
def company_info(cid):
    company = Company.query.filter_by(id=cid).first()
    if company is None:
        return error_result(ErrorCode.ERROR_COMPANY_NOT_EXIST)
    if request.method == 'GET':
        user = User.query.filter_by(company_id=cid,
                                    role_id=UserRole.ROLE_ADMIN).first()
        data = company.get_json(with_status=True)
        if user:
            data['username'] = user.username
        return success_result(data)

    elif request.method == 'PUT':
        params = request.get_json()
        username = params.get('username')
        password = params.get('password')
        fields = ('name', 'deployment', 'consigner', 'scenario', 'remark',
                  'feature_version')
        old_feature_version = company.feature_version
        company.update(fields, params)
        new_feature_version = int(company.feature_version)

        functions = params.get('functions')
        if functions:
            company.set_function_status(functions)

        if company.deployment == DeploymentVersion.OFFLINE:
            meta = params.get('meta')
            company.save_meta_data(meta)

        if username:
            user = User.query.filter_by(company_id=cid,
                                        role_id=UserRole.ROLE_ADMIN).first()
            if user is None:
                if not password:
                    password = '******'
                user = User(company_id=company.id,
                            role_id=UserRole.ROLE_ADMIN,
                            username=username,
                            password_reseted=True)
            else:
                if user.username != username and User.query.filter_by(
                        username=username).first() is not None:
                    return error_result(ErrorCode.ERROR_USERNAME_EXISTED)
                user.username = username
            if password:
                user.password = password
            db.session.add(user)

            #feature版本的设置在task里进行,这里先给改成原来的版本,因为版本切换有可能失败
            if new_feature_version:
                company.feature_version = old_feature_version
        db.session.commit()

        if old_feature_version != new_feature_version:
            task = Task()
            task.name = u'模型版本切换'
            task.content = u'%s: %s -> %s' % (
                company.name, old_feature_version, new_feature_version)
            db.session.add(task)
            db.session.commit()
            switch_feature_version.delay(task.id, company.id,
                                         new_feature_version)

        ret = company.get_json(with_status=True)
        ret['username'] = username
        return success_result(ret)

    elif request.method == 'DELETE':
        if len(company.boxes) > 0:
            return error_result(ErrorCode.ERROR_NOT_ALLOWED)
        db.session.delete(company)
        db.session.commit()
        return success_result({})