def subjects_list(request): if request.method == 'GET': subjects = Subject.objects.all() serializer = SubjectSerializer(subjects, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': query_string = request.POST.get('query', '') subject = get_subject_by_query_string(query_string=query_string) if subject and subject.requested_recently(): subject.request_time = timezone.now() subject.save(update_fields=['request_time']) serializer = SubjectSerializer(data=subject.to_dict()) if serializer.is_valid(): return JsonResponse(serializer.data, status=201) else: data = fetch_data( url='https://rmsp.nalog.ru/search-proc.json', query_string=query_string, ) if data: subject = Subject(**data) subject.save() subject.query_set.create(query_string=query_string) serializer = SubjectSerializer(data=subject.to_dict()) if serializer.is_valid(): return JsonResponse(serializer.data, status=201) return JsonResponse({'error': 'Query string is invalid'}, status=400)
def test_scoring_service(): article = Article(id='a-id', url='a-url', title='Apple’s Social Network', body=read_article_body(), keywords=[], date=datetime.utcnow()) subjects = [ Subject(id='s-0', symbol='AAPL', name='Apple inc.', score=0.0, article_id='a-id'), Subject(id='s-1', symbol='GOOG', name='Alphabet inc.', score=0.0, article_id='a-id') ] expected_scores = [('s-0', 0.37470489668959894), ('s-1', 0.004385360758615228)] scorer = ScoringService() scored_subjects = scorer.score(article, subjects) assert len(scored_subjects) == len(expected_scores) for i, ssub in enumerate(scored_subjects): expected_id, expected_score = expected_scores[i] assert ssub.id == expected_id assert ssub.score == expected_score
def post(self): parser = reqparse.RequestParser() parser.add_argument( "name", required=True, help="Please enter a name.") parser.add_argument( "description", required=True, help="Please enter a description.") parser.add_argument( "teacher_id", required=True, help="Please enter a valid teacher's ID.") args = parser.parse_args() name, description, teacher_id = args["name"], args["description"], \ args["teacher_id"] subject = Subject(name=name, description=description, subject_id="SB" + str(random.randint(1, 999))) if teacher_id: teacher = Teacher.query.filter_by(staff_id=teacher_id).first() if not teacher: return {"error": "The teacher ID you entered is invalid."}, 400 else: subject.teacher = teacher return create_or_update_resource( resource=subject, resource_type="subject", serializer=subject_serializer, create=True)
def test_scrape_target_to_article_conversion(): target = ScrapeTarget( url='target-url', subjects=[ Subject(id='s-id', symbol='symbol', name='name', score=0.0, article_id='a-id') ], referer=Referer(id='r-id', external_id='e-id', follower_count=100, article_id='a-id'), title='a-title', body='a-body', article_id='a-id' ) article = target.article() assert article.id == target.article_id assert article.url == target.url invalid_target = ScrapeTarget( url='target-url', subjects=[ Subject(id='s-id', symbol='symbol', name='name', score=0.0, article_id='a-id') ], referer=Referer(id='r-id', external_id='e-id', follower_count=100, article_id='a-id'), title=None, body=None, article_id='a-id' ) with pytest.raises(ValueError): invalid_target.article()
def test_create_subject_with_same_name(self): db.session.add(Subject('Test')) db.session.commit() s = Subject('Test') db.session.add(s) with self.app.app_context(): self.assertRaises(IntegrityError, db.session.commit)
def add(self, subject_name, checkpoints_json): self.add_base_fields(checkpoints_json) self.add_base_checkpoint(checkpoints_json) subject = Subject.query.filter_by(name=subject_name).first() if subject is None: subject = Subject(name=subject_name) subject.add_to_db() checkpoints = subject.checkpoints for checkpoint_json in checkpoints_json: checkpoint_name = checkpoint_json['name'] checkpoint = checkpoints.filter_by(name=checkpoint_name).first() if checkpoint is None: checkpoint = Checkpoint(name=checkpoint_name) checkpoints.append(checkpoint) fields = checkpoint.fields for field_json in checkpoint_json['fields']: field_name = field_json['name'] field = fields.filter_by(name=field_name).first() if field is None: field = CheckpointField(name=field_name) fields.append(field) field.type = field_json.get('type', None) field.is_hidden = field_json.get('is_hidden', False) db.session.commit() checkpoints = subject.checkpoints.all() return Checkpoint.json_list(checkpoints)
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 setUp(self): """建立测试环境""" self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() UserType.insert_usertypes() Subject.insert_subjects()
def setUp(self): self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles() StateMetaData.insert_states() Subject.insert_subjects()
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 test_get_subjects_list(self): subject_list_empty = models_conf_classes.SubjectConf.get_objects_dict_list_or_404(Subject)[0].json subject = Subject('Test') db.session.add(subject) db.session.commit() subject_list = models_conf_classes.SubjectConf.get_objects_dict_list_or_404(Subject)[0].json self.assertTrue(subject.to_dict() in subject_list['items']) self.assertEqual(subject_list_empty['msg'], 'objects is not found') self.assertEqual(subject_list_empty.get('items', False), False)
def subject_init(row): subject = Subject() num = Subject.query.filter_by(name=row['Nom']).count() if num > 0: Subject.query.delete() study = Study.query.filter_by(name=row['Etude']).first() subject.name = row['Nom'] subject.description = row['Description'] subject.study = study return subject
def test_to_dict_method(self): teacher = User(email='*****@*****.**') subject = Subject("Test") teacher.is_teacher = True db.session.add(teacher) db.session.add(subject) db.session.commit() db.session.commit() tch_dict = teacher.to_dict() sub_dict = subject.to_dict() self.assertIsInstance(tch_dict, dict) self.assertIsInstance(sub_dict, dict)
def setUp(self): db.create_all() db.session.add(Subject(codename='math', name='Математика', _aliases='матеша,матан,алгебра')) db.session.add(Subject(codename='history', name='История', _aliases='истор')) db.session.add(Task(subject_codename='math', date=date(2020, 3, 4), text='тестовое задание с математики')) for i in range(14): db.session.add(Timetable(subjects='math', is_work_day=True)) db.session.add(User(id=45345234, username='******', name='Админ', status='super_admin')) db.session.add(User(id=64564562, username='******', name='Человек', status='user')) db.session.commit() self.login()
def updatetable(): semester_obj = Semester.query.filter_by(semester_no=request.form['semester']).first() if semester_obj: subject = Subject(subjectname=request.form['subject'], teachername=request.form['teacher'], day=request.form['day'], room=request.form['room'], timing=request.form['timing'], semester_name=semester_obj) db.session.add(subject) db.session.commit() else: semester_obj = Semester(semester_no=request.form['semester']) subject = Subject(subjectname=request.form['subject'], teachername=request.form['teacher'], day=request.form['day'], room=request.form['room'], timing=request.form['timing'], semester_name=semester_obj) db.session.add(subject) db.session.commit() return ""
def add_subject(): if not current_user.is_authenticated or current_user.role != 'admin': return redirect(url_for('login')) form1 = AddSubject(prefix="form1") if form1.validate_on_submit() and form1.submit.data: old_subject = Subject.query.filter(Subject.name == form1.name.data) \ .first() if old_subject is not None: flash('Subject already exists') return redirect(url_for('add_subject')) subject = Subject(name=form1.name.data) db.session.add(subject) db.session.commit() flash('Subject is added') return redirect(url_for('add_subject')) db_subject_list = Subject.query.all() subject_list = [(i.sub_id, i.name) for i in db_subject_list] form2 = DeleteSubject(prefix="form2") form2.subject.choices = subject_list if form2.validate_on_submit() or form2.submit.data: subject = Subject.query.filter(Subject.sub_id == form2.subject.data) \ .all()[0] db.session.delete(subject) db.session.commit() flash('Subject is deleted') return redirect(url_for('add_subject')) return render_template('add_subject.html', form1=form1, form2=form2)
def subject_import_visitor(): try: file_ = request.files['file'] except: return error_result(ErrorCode.ERROR_INVALID_PARAM) if not g.user.has_permission(AccountPermission.ADD_VISITOR): return error_result(ErrorCode.ERROR_PERMISSION_DENIED) workbook = xlrd.open_workbook(file_contents=file_.read()) sheet = workbook.sheet_by_index(0) success = 0 state_mapping = VisitorPurpose.state_mapping_reverse for i in xrange(1, sheet.nrows): try: row = sheet.row(i) name = row[0].value db.session.add( Subject(name=name, purpose=state_mapping[row[1].value], come_from=row[2].value, phone=row[3].value, interviewee=row[4].value, remark=row[5].value, subject_type=SubjectType.TYPE_VISITOR, company_id=g.user.company_id)) db.session.commit() success += 1 except: import traceback; print traceback.format_exc() pass return success_result({'success': success, 'total': sheet.nrows - 1, 'failed': []})
def setUp(self): """Set up test client and populate test database with test data """ self.app = app.test_client() db.create_all() user = User(username="******", password="******") student = Student(student_id="ST001", first_name="Hermione", last_name="Granger", email_address="*****@*****.**") teacher = Teacher(staff_id="TC001", first_name="Minerva", last_name="McGonagall", email_address="*****@*****.**") subject = Subject(subject_id="SB001", name="Transfiguration", description="Teaches the art of changing the form " "and appearance of an object or a person.") subject.major_students.append(student) teacher.subjects_taught.append(subject) db.session.add(user) db.session.add(student) db.session.add(teacher) db.session.add(subject) self.token = self.get_token()
def new_class(): teacher = Teacher.query.filter_by(id=current_user.id).first() try: blocks = teacher.blocks except exc.SQLAlchemyError: pass manual_block_form = ManualBlockForm() if manual_block_form.validate_on_submit(): subject = Subject(title=manual_block_form.class_subject.data) db.session.add(subject) db.session.commit() subject = Subject.query.filter_by( title=manual_block_form.class_subject.data, ).first() block = Block( subject_id=subject.id, teacher_id=teacher.id, title=manual_block_form.class_block.data, ) db.session.add(block) db.session.commit() return redirect(url_for('classes')) return render_template( 'classes/new_class.html', blocks=blocks, manual_block_form=manual_block_form, )
def test_scraped_article_serialization(): scraped_article = ScrapedArticle( article=Article( id='a-id', url='target-url', title='a-title', body='a-body', keywords=[ 'k-0', 'k-1' ], date=datetime.utcnow()), subjects=[ Subject( id='s-id', symbol='symbol', name='name', score=0.0, article_id='a-id') ], referer=Referer( id='r-id', external_id='e-id', follower_count=100, article_id='a-id') ) sa_dict = scraped_article.asdict() sa_dict['article']['id'] == 'a-id'
def add_subject(): """添加主题.""" req_dict = request.get_json() name = req_dict.get('name') seq = req_dict.get("seq") area = req_dict.get('area') print("++++添加主题++++", req_dict) # 校验数据 param_keys = ["name", "seq", "area"] param_dict = dict() for i in param_keys: param_dict.__setitem__(i, req_dict.get(i, '')) for key, value in param_dict.items(): if not value: return jsonify(errno=RET.PARAMERR, errmsg="数据不完整,缺少%s" % key) try: exist = Subject.query.filter( or_(Subject.name == name, Subject.seq == seq)).all() if exist: return jsonify(errno=RET.DBERR, errmsg="数据有重复") subject = Subject(name=name, seq=seq, area=area) db.session.add(subject) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.UNKOWNERR, errmsg="添加失败") return jsonify(errno=RET.OK, errmsg="成功")
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_import(): try: file_ = request.files['file'] except: return error_result(ErrorCode.ERROR_INVALID_PARAM) if not g.user.has_permission(AccountPermission.ADD_EMPLOYEE): return error_result(ErrorCode.ERROR_PERMISSION_DENIED) workbook = xlrd.open_workbook(file_contents=file_.read()) sheet = workbook.sheet_by_index(0) success = 0 failed = [] for i in xrange(1, sheet.nrows): try: row = sheet.row(i) name = row[0].value email = row[5].value if email and Subject.query.filter_by(email=email).first(): failed.append(dict(name=name, email=email)) continue if not name.strip(): continue db.session.add(Subject(name=name, job_number=row[1].value, department=row[2].value, password='******', title=row[3].value, phone=row[4].value, email=email, description=row[6].value, remark=row[7].value, subject_type=SubjectType.TYPE_EMPLOYEE, company_id=g.user.company_id)) db.session.commit() success += 1 except: import traceback; print traceback.format_exc() return success_result({'success': success, 'total': sheet.nrows - 1, 'failed': failed})
def test_subject_delete(self): subject = Subject('Test') db.session.add(subject) db.session.commit() self.assertEqual(Subject.query.get(subject.id), subject) deleted = models_conf_classes.SubjectConf.delete_object(subject, db) self.assertEqual(deleted[1], 201) self.assertEqual(Subject.query.get(subject.id), None)
def test_subject_update(self): subject = Subject('Test') db.session.add(subject) db.session.commit() subject_data = {'description': 'Test description'} update = models_conf_classes.SubjectConf.update_obj(Subject, subject.id, db, subject_data) self.assertEqual(update[1], 201) self.assertEqual(subject.description, subject_data['description'])
def newSubject(): newSubject = Subject(data=request.data) db.session.add(newSubject) db.session.commit() obj1 = {} obj1['id'] = json.dumps(newSubject.id) obj1['data'] = json.loads(newSubject.data) return obj1
def add_association(self, username, subject_name, group_id): tutor = self.find_tutor_by_username(username) if tutor.tgs.filter_by(subject_name=subject_name, group_id=group_id).first(): raise AssociationExist(subject_name=subject_name, group_id=group_id) subject = Subject.find_by_name(subject_name) if subject is None: subject = Subject(name=subject_name) subject.add_to_db() group = Group.find_by_id(group_id) if group is None: group = Group(id=group_id) group.add_to_db() association = AssociationTGS() association.subject = subject association.group = group tutor.tgs.append(association) db.session.commit()
def _process_subjects(self, subjects: dict): """ Processes all WaniKani subjects and stores it in the database to be easily accessible. This only needs to happen once since the subject info is the same for everyone. Parameters ---------- subjects : dict The JSON containing the subject info. Returns ------- None """ for subject in subjects['data']: character_image = None # Get an easily usable image (PNG) instead of a vector. if subject['object'] == 'radical' and subject['data'][ 'characters'] is None: for image in subject['data']['character_images']: if image['content_type'] == 'image/png': character_image = image['url'] break sbjt = Subject() sbjt.id = subject['id'] sbjt.level = subject['data']['level'] sbjt.type = subject['object'] sbjt.image_url = character_image sbjt.characters = subject['data']['characters'] or '[Radical]' database.session.add(sbjt)
def query(request): query_string = request.POST.get('query', '') subject = get_subject_by_query_string(query_string=query_string) if subject and subject.requested_recently(): subject.request_time = timezone.now() subject.save(update_fields=['request_time']) else: data = fetch_data( url='https://rmsp.nalog.ru/search-proc.json', query_string=query_string, ) if data: subject = Subject(**data) subject.save() subject.query_set.create(query_string=query_string) return HttpResponseRedirect(reverse('app:index'))
def addSubject(): form = SubjectForm() message = '' if form.validate_on_submit(): name = form.name.data db.session.add(Subject(name=name)) db.session.commit() message = '添加成功' return render_template('ques/addSubject.html', form=form, message=message)