def handle(self, *args, **options): if settings.DEBUG and settings.ACTIVATE_DIRECTLY: now = datetime.datetime.now() Setting.setval( 'activation', { 'quota': random.randint(100, 300), 'update_time': now.strftime('%Y-%m-%d %H:%M:%S'), 'start_date': str((now - datetime.timedelta(1)).date()), 'end_date': str((now + datetime.timedelta(15)).date()), }) print 'activate success'
def resource_catalog_add(request): ''' 教学点对应的卫星资源接收目录添加 ''' if request.method == 'POST': if Setting.getvalue('server_type') != 'country': return create_failure_dict(msg='教学点仅支持县级服务器') f = EduPointResourceCatalogForm(request.POST) if f.is_valid(): catalog = f.cleaned_data['catalog'] edupoint_id = request.POST.get('edupoint_id') try: edupoint = EduPoint.objects.get(id=edupoint_id) except Exception, e: logger.exception(e) return create_failure_dict(msg='无该教学点ID') if edupoint.edupointresourcecatalog_set.all().count() >= 10: return create_failure_dict(msg='教学点只能拥有10个资源接收目录') obj, c = EduPointResourceCatalog.objects.get_or_create(edupoint=edupoint, catalog=catalog) if not c: return create_failure_dict(msg='教学点不能保存相同的资源接收目录') return create_success_dict(msg='成功添加教学点卫星资源接收目录')
def delete(request, *args, **kwargs): """ 教学点管理>删除节点 """ if request.method == 'POST': if not cache.get('sudo'): return create_failure_dict(msg='请输入正确的超级管理员admin密码!') if Setting.getvalue('server_type') != 'country': return create_failure_dict(msg='教学点仅支持县级服务器') id = request.POST.get('uuid') try: obj = EduPoint.objects.get(id=id) except Exception, e: logger.exception(e) return create_failure_dict(msg='无该教学点ID, 不能删除') # 如果该教学点下的教室有已申报的情况,需要提示先取消申报才可删除教学点 if not obj.has_delete(): return create_failure_dict(msg='先解除终端绑定,才能删除教学点!') obj.delete() return create_success_dict(msg='成功删除教学点')
def edit(request): ''' 教学点管理>编辑 ''' if request.method == 'POST': if Setting.getvalue('server_type') != 'country': return create_failure_dict(msg='教学点仅支持县级服务器') id = request.POST.get('uuid') obj = EduPoint.objects.get(id=id) f = EduPointEditForm(request.POST, instance=obj) if f.is_valid(): number = f.cleaned_data['number'] remark = f.cleaned_data['remark'] if number < obj.number: return create_failure_dict(msg='教学点修改终端数量不能小于原始数量') obj.number = number obj.remark = remark obj.save() return create_success_dict(msg='成功编辑教学点') return create_failure_dict(msg='教学点添加失败', errors=f.errors)
def _query(request, node_type): lst = ('province', 'city', 'country', 'town', 'school', 'unit', 'room') f = lambda x, y: lst.index(x) <= lst.index(y) and Setting.getvalue( x) or None d = { # 前端将后台返回的 conditions 解析了,这里重组一下 'province_name': request.GET.get('province_name', f('province', node_type)), 'city_name': request.GET.get('city_name', f('city', node_type)), 'country_name': request.GET.get('country_name', f('country', node_type)), 'town_name': request.GET.get('town_name', None), 'point_name': request.GET.get('point_name', None), 'room_name': request.GET.get('room_name', None), 'parent_key': request.GET.get('parent_key', '_init') } school_year = request.GET.get('school_year', None) term_type = request.GET.get('term_type', None) parent = request.GET.get('parent', None) d[d['parent_key']] = parent if d['parent_key'] != '_init' and d.has_key('_init'): del d['_init'] o = models.EduPoint.objects.all() pic = models.EduPointDetailDesktopViewLog.objects.all() if d['province_name'] and lst.index(node_type) > 0: o = o.filter(province_name=d['province_name']) pic = pic.filter(province_name=d['province_name']) if d['city_name'] and lst.index(node_type) > 1: o = o.filter(city_name=d['city_name']) pic = pic.filter(city_name=d['city_name']) if d['country_name'] and lst.index(node_type) > 2: o = o.filter(country_name=d['country_name']) pic = pic.filter(country_name=d['country_name']) if d['town_name'] and lst.index(node_type) > 3: o = o.filter(town_name=d['town_name']) pic = pic.filter(town_name=d['town_name']) if d['point_name'] and lst.index(node_type) > 4: o = o.filter(point_name=d['point_name']) pic = pic.filter(point_name=d['point_name']) if d['room_name'] and lst.index(node_type) > 5: o = o.filter(number=d['room_name']) pic = pic.filter(number=d['room_name']) if school_year or term_type: if school_year: o = o.filter(school_year=school_year) pic = pic.filter(school_year=school_year) if term_type: o = o.filter(term_type=term_type) pic = pic.filter(term_type=term_type) else: # if not school_year and not term_type: t = NewTerm.get_current_term() if not t: return create_failure_dict(msg='学年学期获取失败') o = o.filter(school_year=t.school_year, term_type=t.term_type) pic = pic.filter(school_year=t.school_year, term_type=t.term_type) o = o.order_by('province_name', 'city_name', 'town_name', 'point_name') return o, pic, d
def handle(self, *args, **options): server_type = Setting.getvalue('server_type') if server_type != 'country': print 'Wrong server_type' raise Exception(u'错误的服务器级别') term = NewTerm.get_current_or_next_term() if not term: print 'There is no term available in the database' raise Exception(u'无可用学年学期') print 'current term', term.school_year, term.term_type pre_term = NewTerm.get_previous_term(term=term) if not pre_term: print 'There is no pre_term' raise Exception(u'无可用学年学期') print 'pre_term', pre_term.school_year, pre_term.term_type objs1 = models.EduPoint.objects.filter( school_year=pre_term.school_year, term_type=pre_term.term_type) for o in objs1: point, is_new = models.EduPoint.objects.get_or_create( school_year=term.school_year, term_type=term.term_type, province_name=o.province_name, city_name=o.city_name, country_name=o.country_name, town_name=o.town_name, point_name=o.point_name, number=o.number, remark=o.remark, communicate_key=o.communicate_key) if is_new: print 'create one edupoint item:', point.point_name objs = list(o.edupointdetail_set.all()) if is_new: print 'child_set count:', objs.count() for oo in objs: models.EduPointDetail.objects.create( edupoint=point, type=oo.type, name=oo.name, communicate_key=oo.communicate_key, grade_name=oo.grade_name, class_name=oo.class_name, cpuID=oo.cpuID, need_confirm=True)
def _get_second_part(): ''' 去上级服务器取得NODE ID 并放到缓存里 ''' cursor = connection.cursor() cursor.execute('Lock TABLES setting WRITE;') node_id = Setting.getvalue('sync_node_id') cursor.execute('UNLOCK TABLES;') cursor.close() return int(node_id) # old version school_uuid = Group.objects.get(group_type='school').uuid cache_key = 'school-%s-node-id' % (school_uuid) node_id = cache.get(cache_key) if not node_id: # 获取上级服务器的HOST,PORT,KEY host = Setting.getvalue('sync_server_host') port = Setting.getvalue('sync_server_port') key = Setting.getvalue('sync_server_key') data = {'key': key} try: url = u'http://%s:%s/terminal/api/get_node_id/' % (host, port) ret = requests.post(url, data=data, timeout=60) ret = ret.json() node_id = ret['data']['node_id'] if node_id: cache.set(cache_key, node_id, 60 * 60 * 24) except: node_id = None return node_id
def unbind(request, *args, **kwargs): """系统设置>服务器管理>教学点教室终端管理>清除绑定""" server_type = Setting.getvalue('server_type') if server_type != 'country': return create_failure_dict(msg=u'错误的服务器级别') uuid = request.POST.get('uuid', None) if not uuid: return create_failure_dict(msg=u'获取节点失败') if not cache.get('sudo'): return create_failure_dict(msg=u'请输入正确的超级管理员admin密码!') try: o = models.EduPointDetail.objects.get(id=uuid) o.cpuID = '' o.communicate_key = _generate_key() o.save() return create_success_dict(msg=u'清除绑定成功') except: return create_failure_dict(msg=u'错误的UUID')
def resource_catalog_add_new(request): ''' 教学点对应的卫星资源接收目录添加 ''' if Setting.getvalue('server_type') != 'country': return create_failure_dict(msg=u'资源接收目录仅支持县级服务器') path = request.REQUEST.get('catalog', None) if not path: return create_failure_dict(msg=u'未获取到资源接收目录') if Setting.objects.filter(name='resource_catalog').count() > 9: return create_failure_dict(msg=u'最多添加10个资源接收目录') o, is_new = Setting.objects.get_or_create( name='resource_catalog', value=path ) o.save() return create_success_dict(msg=u'成功设定资源接收目录')
def leest(request, *args, **kwargs): """系统设置>服务器管理>教学点教室终端管理>列表显示""" server_type = Setting.getvalue('server_type') if server_type != 'country': return create_failure_dict(msg='错误的服务器级别') o = list(_query(request)) ret = map( lambda i: { 'uuid': i['pk'], 'school_year': i['edupoint__school_year'], 'term_type': i['edupoint__term_type'], 'town_name': i['edupoint__town_name'], 'point_name': i['edupoint__point_name'], 'key': i['communicate_key'], 'status': i['cpuID'] and i['cpuID'] and '已申报' or '未申报', 'number': i['name'], 'last_active_time': i['last_active_time'], 'last_upload_time': i['last_upload_time'], }, o) return paginatoooor(request, ret)
def resource_catalog_delete(request): ''' 教学点对应的卫星资源接收目录删除 ''' if request.method == 'POST': if not cache.get('sudo'): return create_failure_dict(msg='请输入正确的超级管理员admin密码!') if Setting.getvalue('server_type') != 'country': return create_failure_dict(msg='教学点仅支持县级服务器') id = request.POST.get('uuid') try: obj = EduPointResourceCatalog.objects.get(id=id) except Exception, e: logger.exception(e) return create_failure_dict(msg='教学点没有该卫星资源接收目录') obj.delete() return create_success_dict(msg='成功删除教学点卫星资源接收目录')
def handle(self, *args, **options): server_type = Setting.getvalue('server_type') if server_type != 'school': current_term = NewTerm.get_current_term() terms = NewTerm.objects.all() if current_term: terms = terms.exclude(pk=current_term.pk) else: current_term = Term.get_current_term_list() terms = Term.objects.all() if current_term: uuids = [o.pk for o in current_term] terms = terms.exclude(pk__in=uuids) uuids = Setting.objects.filter(name='task-init-statictic-term') uuids = list(uuids.values_list('value', flat=True)) if uuids: terms = terms.exclude(pk__in=uuids) for term in terms: logger.debug('task-init-statictic-term: %s %s' % (term.school_year, term.term_type)) Statistic.init_all(term) Statistic.update_all(term)
def _getvalue(name): return Setting.getvalue(name)
def screenshot_node_info(request, *args, **kwargs): """教师授课>教学点桌面使用日志>节点信息获取""" f = lambda x: x == 'country' and 'town' or x server_type = Setting.getvalue('server_type') root_name = Setting.getvalue(server_type) if server_type == 'school': return create_failure_dict(msg=u'错误的服务器级别.') node_type = request.GET.get('node_type', f(server_type)) o, pic, conditions = _query(request, node_type) q = models.EduPointDetail.objects.none() for i in o: q |= i.edupointdetail_set.filter(type='room') for i in ('province', 'city', 'country', 'town', 'unit', 'room'): if node_type == i: break name = '%s_name' % (i != 'unit' and i or 'point') if not conditions[name]: return create_failure_dict(msg=u'父级参数不完整(%s)' % name) for i in conditions.keys(): if not conditions[i]: del conditions[i] ret = [] if node_type == 'province': provinces = list(set(o.values_list('province_name', flat=True))) for province_name in provinces: oo = o.filter(province_name=province_name) # qq = q.filter(edupoint__province_name=province_name) pp = pic.filter(edupoint__province_name=province_name) ret.append({ 'parent_name': province_name, 'node_type': 'province', 'child_type': 'city', 'unit_count': oo.count(), 'room_count': oo.aggregate(n=Sum('number'))['n'], 'pic_count': pp.count(), }) conditions['parent_key'] = 'province_name' elif node_type == 'city': citys = list(set(o.values_list('city_name', flat=True))) for city_name in citys: oo = o.filter(city_name=city_name) # qq = q.filter(edupoint__city_name=city_name) pp = pic.filter(edupoint__city_name=city_name) ret.append({ 'parent_name': city_name, 'node_type': 'city', 'child_type': 'country', 'unit_count': oo.count(), 'room_count': oo.aggregate(n=Sum('number'))['n'], 'pic_count': pp.count(), }) conditions['parent_key'] = 'city_name' elif node_type == 'country': countrys = list(set(o.values_list('country_name', flat=True))) for country_name in countrys: oo = o.filter(country_name=country_name) # qq = q.filter(edupoint__country_name=country_name) pp = pic.filter(edupoint__country_name=country_name) ret.append({ 'parent_name': country_name, 'node_type': 'country', 'child_type': 'town', 'unit_count': oo.count(), 'room_count': oo.aggregate(n=Sum('number'))['n'], 'pic_count': pp.count(), }) conditions['parent_key'] = 'country_name' elif node_type == 'town': towns = list(set(o.values_list('town_name', flat=True))) for town_name in towns: oo = o.filter(town_name=town_name) # qq = q.filter(edupoint__town_name=town_name) pp = pic.filter(edupoint__town_name=town_name) ret.append({ 'parent_name': town_name, 'node_type': 'town', 'child_type': 'unit', 'unit_count': oo.count(), 'room_count': oo.aggregate(n=Sum('number'))['n'], 'pic_count': pp.count(), }) conditions['parent_key'] = 'town_name' elif node_type == 'unit': points = list(set(o.values_list('point_name', flat=True))) for point_name in points: oo = o.filter(point_name=point_name) # qq = q.filter(edupoint__point_name=point_name) pp = pic.filter(edupoint__point_name=point_name) ret.append({ 'parent_name': point_name, 'node_type': 'unit', 'child_type': 'room', 'unit_count': oo.count(), 'room_count': oo.aggregate(n=Sum('number'))['n'], 'pic_count': pp.count(), }) conditions['parent_key'] = 'point_name' elif node_type == 'room': ret = map( lambda i: { 'parent_name': i.name, 'node_type': 'room', 'child_type': 'none', 'unit_count': 1, 'room_count': 1, 'point_id': i.edupoint.pk, 'room_id': i.pk, 'pic_count': pic.filter(edupointdetail=i).count(), }, q) conditions['parent_key'] = 'room_name' return paginatoooor(request, ret, conditions=conditions, root_name=root_name)
def get_node_trees(request, *args, **kwargs): """系统设置>教学点>节点树,用于条件联动""" __f = lambda name: Setting.getvalue(name) node_type = request.GET.get('node_type', None) province_name = request.GET.get('province_name', __f('province')) city_name = request.GET.get('city_name', __f('city')) country_name = request.GET.get('country_name', __f('country')) town_name = request.GET.get('town_name', None) point_name = request.GET.get('point_name', None) school_year = request.GET.get('school_year', None) term_type = request.GET.get('term_type', None) status = request.GET.get('status', None) ori = request.GET.get('ori', None) # 数据源: GroupTB or 实际存在的数据 type_index = ('province', 'city', 'country', 'town', 'unit', 'room') # 下面的用于限定父级参数不完整的时候不返回子级的信息 for i in type_index[3:]: if node_type == i: break name = request.GET.get('%s_name' % (i != 'unit' and i or 'point'), None) if not name: return create_success_dict(data={'records': []}) o = models.EduPoint.objects.all() if province_name and type_index.index(node_type) > 0: o = o.filter(province_name=province_name) if city_name and type_index.index(node_type) > 1: o = o.filter(city_name=city_name) if country_name and type_index.index(node_type) > 2: o = o.filter(country_name=country_name) if town_name and type_index.index(node_type) > 3: o = o.filter(town_name=town_name) if point_name and type_index.index(node_type) > 4: o = o.filter(point_name=point_name) if status == u'已申报': o = o.exclude(edupointdetail__cpuID='') elif status == u'未申报': o = o.filter(edupointdetail__cpuID='') if school_year or term_type: if school_year: o = o.filter(school_year=school_year) if term_type: o = o.filter(term_type=term_type) else: # if not school_year and not term_type: # by default term = NewTerm.get_current_or_next_term() if not term: return create_failure_dict(msg=u'无可用学年学期') o = o.filter(school_year=term.school_year) o = o.filter(term_type=term.term_type) o = o.order_by('province_name', 'city_name', 'country_name', 'town_name', 'point_name', 'create_time') lst = [] # We have 7 level: # province, city, country, town, school/unit, grade, class/room # Part I: province city country town -- form GroupTB # Part II: school grade class -- from Group Grade and Class # Part III: unit room -- from EduPoint and EduPointDetail # node_type will be asigned to server_type if it is null if not node_type: server_type = Setting.getvalue('server_type') if server_type == 'country': node_type = 'town' elif server_type == 'school': node_type = 'school' else: return create_failure_dict() index_lst_1 = ('province', 'city', 'country', 'town') # Part I if node_type in index_lst_1: if ori == 'unit': lst = list(set(o.values_list('%s_name' % node_type, flat=True))) else: g = GroupTB.objects.all() if index_lst_1.index(node_type) > 0: # city g = g.get(name=province_name) if index_lst_1.index(node_type) > 1: # country g = g.child_set.get(name=city_name) if index_lst_1.index(node_type) > 2: # town g = g.child_set.get(name=country_name) # if index_lst_1.index(node_type) > 3: # school # g = g.child_set.get(name=town_name) # if index_lst_1.index(node_type) > 4: # grade # g = g.child_set.get(name=school_name) # if index_lst_1.index(node_type) > 5: # class # g = g.child_set.get(name=grade_name) if node_type == 'province': # province lst = g.filter(parent__isnull=True).values_list('name', flat=True) else: lst = g.child_set.values_list('name', flat=True) # Part II elif node_type == 'school': g2 = Group.objects.all().filter(group_type=node_type) lst = list(set(g2.values_list('name', flat=True))) # Part III elif node_type == 'unit': # if not (school_year and term_type): # return create_failure_dict(msg='#both school_year and term_type \ # needed in this node_type') lst = o.values_list('point_name', flat=True) elif node_type == 'room': # if not (school_year and term_type and town_name and point_name): # return create_failure_dict(msg='#school_year and term_type and \ # town_name and point_name are needed in this node_type') if o.exists(): room = o[0].edupointdetail_set.filter(type='room') lst = list(set(room.values_list('name', flat=True))) else: lst = [] else: lst = [ 'node_type should in province city country \ town school unit and room' ] return create_success_dict( data={'records': map(lambda i: {'key': i}, lst)})
def resource_global(request): ''' 资源使用综合分析 1. 选择区县,乡镇街道,其结点单位为学校 2. 选择学校,其结点为年级 3. 选择年级,其结点为班级 4. 右侧上左饼图为资源来源的百分比图,右侧上右饼图为资源类型的百分比图 5. 右侧下方柱状图为TOP10的学校或是年级或是班级的上课次数 ''' # TODO 2015-03-23 # 添加查询条件 # 查询条件:学年:(当前学年(默认)、动态获取学校所有的学年信息), # 学期:(当前学期(默认)、动态获取学校所选学年的所有学期); # 各分类数值统计将按序排列;当统计类型超过10种时,第10种及以上合并为其他; # 其他需求(前端修改?) # 右侧使用饼状图显示当前学校所查询学期的相关数据:资源来源分类统计, # 各分类总数与百分比;资源类型分类统计,各分类总数与百分比;右侧使用柱状图 # 显示当前学校所查询学期的相关数据:各班级资源使用次数前十名。 node_uuid = request.GET.get('uuid', '') node_type = request.GET.get('type', 'school') school_year = request.GET.get('school_year', '') term_type = request.GET.get('term_type', '') if not school_year and not term_type: terms = Term.get_current_term_list() if terms: school_year = terms[0].school_year term_type = terms[0].term_type if not node_type: node_type = 'school' if not node_uuid: # 只有type没有uuid,是默认界面的url if Setting.getvalue('server_type') != node_type: return create_failure_dict(msg=u'错误的node_type %s' % node_type) try: obj = Group.objects.get(group_type=node_type) except: return create_failure_dict(msg=u'查找服务器节点时出错') node_uuid = obj.uuid else: # 既有type又有uuid,需要验证数据合法性 if node_type in ('province', 'city', 'country', 'town', 'school'): try: obj = Group.objects.get(group_type=node_type, uuid=node_uuid) except: return create_failure_dict(msg=u'错误的node_type和uuid') elif node_type == 'grade': try: obj = Grade.objects.get(uuid=node_uuid) except: return create_failure_dict(msg=u'错误的年级uuid') else: return create_failure_dict(msg=u'错误的node_type') k = u'resource-analysis-for-node-%s-%s-%s' % (node_uuid, school_year, term_type) v = cache.get(k) if v: data = pickle.loads(v) else: data = _get_data(node_uuid, node_type, school_year, term_type) cache.set(k, pickle.dumps(data), get_timeout(k, 60 * 10)) return create_success_dict(data=data)
def inherit_items(request, *args, **kwargs): school_year = request.GET.get('school_year') term_type = request.GET.get('term_type') pre_school_year = request.GET.get('pre_school_year') pre_term_type = request.GET.get('pre_term_type') server_type = Setting.getvalue('server_type') if server_type != 'country': # print 'Wrong server_type' return create_failure_dict(msg=u'错误的服务器级别', debug=server_type) if school_year and term_type: try: term = NewTerm.objects.get(school_year=school_year, term_type=term_type) except Exception as e: return create_failure_dict(msg=u'无可用学年学期', debug=str(e)) else: term = NewTerm.get_current_or_next_term() if not term: print 'There is no term available in the database' return create_failure_dict(msg=u'当前时间/未来时间不存在可用学年学期') print 'current term', term.school_year, term.term_type if school_year and term_type: try: pre_term = NewTerm.objects.get(school_year=pre_school_year, term_type=pre_term_type) except Exception as e: return create_failure_dict(msg=u'无可用历史学年学期', debug=str(e)) else: pre_term = NewTerm.get_previous_term(term=term) if not pre_term: print 'There is no pre_term' return create_failure_dict(msg='无可用历史学年学期') print 'pre_term', pre_term.school_year, pre_term.term_type objs1 = models.EduPoint.objects.filter(school_year=pre_term.school_year, term_type=pre_term.term_type) for o in objs1: point, is_new = models.EduPoint.objects.get_or_create( school_year=term.school_year, term_type=term.term_type, province_name=o.province_name, city_name=o.city_name, country_name=o.country_name, town_name=o.town_name, point_name=o.point_name, number=o.number, remark=o.remark) if is_new: print 'create one edupoint item:', point.point_name objs = o.edupointdetail_set.all() if is_new: print 'child_set count:', objs.count() for oo in objs: models.EduPointDetail.objects.get_or_create( edupoint=point, type=oo.type, name=oo.name, communicate_key=_generate_key()) return create_success_dict(msg=u'操作成功')