def readExcelToData(filepath): dy('进入') data = [] file = xlrd.open_workbook(filepath) dy(type(file)) table = file.sheet_by_index(0) print(type(table))
def train_check(request, qid): if request.method == 'GET': qb = QuestionInfo.objects.get(pk=qid) qs = qb.questionbank_set.all().order_by('question_type') return render(request, 'train_check.html', locals()) else: dy(request.POST) # 通过判断start_time是否存在,确定为ajax刷新还是提交 start_time = request.POST.get('start_time', '') if start_time: # 训练方式 train_type = request.POST.get('train_type', '') tt = '参加【' + train_type + '】练习' qid = request.POST.get('qid', '') qb = QuestionInfo.objects.get(pk=qid) # dy('训练情况', qid, train_type, start_time) time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') obj = models.TrainRecord.objects.create(title=qb.name, train_type=tt, starttime=start_time, user=request.user, time=time) # dy(obj) return redirect('exam:train_list') else: quesid = request.POST.get('name') val = request.POST.get('val') qs = QuestionBank.objects.get(pk=quesid) if qs.answer == val: return HttpResponse('right') else: return HttpResponse('wrong')
def get_unit_from_name(uname): units = Units.objects.filter(unit_name=uname) for u in units: if u.logo2: dy(u.logo2.logo) return u.logo2.logo return None
def post(self, request, pid): dy(pid, request.POST) # 计算得分, 存储错题 paper = models.TestPaper.objects.filter(id=pid).first() scores = self.calculate(request.POST, request, paper) # 存入结果到考试记录中 obj = models.Record.objects.create(userid=request.user, papername=paper, grade=scores) obj.save() # todo 这里需要重定向 return redirect('exam:list_result')
def listwrong(request, wid): if request.method == 'GET': trcd = models.TrainRecord.objects.get(pk=wid) trcds = trcd.wrong.select_related().all().order_by('question_type') return render(request, 'train_listwrongs.html', locals()) else: quesid = request.POST.get('name') val = request.POST.get('val') qs = QuestionBank.objects.get(pk=quesid) dy(qs.answer, val) if qs.answer == val: return JsonResponse({'status': 'right', 'ans': qs.answer}) else: return JsonResponse({'status': 'wrong', 'ans': qs.answer})
def qsbk_show(request): fname = request.FILES.get('filename').name qbname = request.POST.get('qsbk_name') applyto = json.dumps(request.POST.getlist('applyto', '')) # dy('题库预览时回传数据', fname, qbname, applyto) data = {} types = request.GET.get('type') filepath = 'tmpfiles/' + fname if types == 'word': try: data = QuestionBankClass(filepath).get_questionbank() except: pass else: data = readExcelToData(filepath) dy(data) return render(request, 'qsbk_show.html', locals())
def get(self, request): # 查看错题详情 pid = request.GET.get('pid', None) if pid: objs = models.WrongRecord.objects.filter(paper__id=pid).all() return render(request, 'start_wrongs.html', {'objs': objs}) # 试卷信息 querysets = models.TestPaper.objects.all().order_by('-add_time') # 按时间降序 papers_list = [] for paper in querysets: wrongs = paper.wrongrecord_set.filter(user=request.user, error_times__gt=0).all() dy(wrongs) if wrongs: papers_list.append(paper) if papers_list: querysets = get_pagenator(request, papers_list) return render(request, 'list_wrongs.html', {'querysets': querysets}) else: return HttpResponse('当前无错题!')
from django.test import TestCase from docx import Document import os from utils.utils import dy queryset = ['shshdlkfsjld', 'shdklfsjkdjf'] def paperSaveAsWord(queryset): doc = Document() doc.add_paragraph('段落1') doc.add_paragraph('段落2') doc.add_paragraph('段落3') # 如果文件缓存目录不存在,新建 if not os.path.exists('tmp_papers'): os.mkdir('tmp_papers') doc.save('../tmp_papers/good1.docx') import xlwt, xlrd def readExcelToData(filepath): dy('进入') data = [] file = xlrd.open_workbook(filepath) dy(type(file)) table = file.sheet_by_index(0) print(type(table)) if __name__ == '__main__': # readExcelToData('../tmpfiles/excel_template.xlsx') dy('good')
def initial_session(user_obj, request): """ 将当前登录人的所有权限列表和所有菜单权限列表注入session :param user_obj: 当前登录用户对象 :param request: 请求对象HttpRequest """ # 查询当前登录人的所有权限列表 # todo 注意这里定义的是userprofile,后台共用的密码,最好不要这么定义在一起,尽量松耦合,Role最好定义在rbac内部 ret = Role.objects.filter(userprofile=user_obj).values( 'permissions__url', 'permissions__is_menu', 'permissions__is_menu__icon', 'permissions__is_menu__title', 'permissions__is_menu__sequence', 'permissions__is_menu__layer', ).distinct() permission_list = [] menu_list = [] # dy('初始化当前用户的权限') for item in ret: # 存入权限 permission_list.append(item['permissions__url']) # 如果菜单存在,存入菜单对象 # menu = Menus.objects.filter(pk=item['permissions__is_menu']).first() if item['permissions__is_menu']: menu_list.append({ 'title': item['permissions__is_menu__title'], 'icon': item['permissions__is_menu__icon'], 'sequence': item['permissions__is_menu__sequence'], 'layer': item['permissions__is_menu__layer'], 'url': item['permissions__url'], }) # 菜单进行排序 menuLenght = len(menu_list) for i in range(menuLenght): m = menu_list[i] for j in range(i + 1, menuLenght): if m['sequence'] > menu_list[j]['sequence']: menu_list[i] = menu_list[j] menu_list[j] = m m = menu_list[i] # dy(menu_list) # todo 这里只做到二级菜单,不继续深入 menu_list1 = [] dy(menu_list) for menu in menu_list: if menu['layer'] == 0: # dy(menu.permission_set()) menu_list1.append({ 'url': menu['url'], 'title': menu['title'], 'icon': menu['icon'], 'children': [] }) elif menu['layer'] == 1: menu_list1[-1]['children'].append({ 'url': menu['url'], 'title': menu['title'], 'icon': menu['icon'] }) else: pass # dy(menu_list1) # dy('权限列表', permission_list) request.session['permission_list'] = permission_list request.session['menu_list'] = menu_list1 dy('权限列表', permission_list)