def test_skills_validations(self): skillset = Skillset.objects.create( name='python', description='best ever', verified=True, weight=10) skill = Skill(name='loops', verified=True, skillset_id=skillset.id) err_msg = 'Verified skills must have a weight.' with self.assertRaisesMessage(ValidationError, err_msg): skill.save()
def mutate(self, args, context, info): name = args.get('name') description = args.get('description') skillset_id = args.get('skillset_id') new_skill = SkillModel(name, description, skillset_id) new_skill.save() return CreateSkill(skill=new_skill)
def handle(self, *args, **options): dependancies = {} rubrique = '' for row in csv.DictReader(open(args[0], "r"), delimiter=",", quotechar='"'): rubrique = row['Rubrique'] if row['Rubrique'] else rubrique if Skill.objects.filter(code=row["Code"]).exists(): skill = Skill.objects.get(code=row["Code"]) skill.depends_on.clear() print "update", row["Code"] else: skill = Skill() print "create", row["Code"] skill.code = row["Code"] skill.name=row['Intitul\xc3\xa9'] skill.description=row['Commentaires'] skill.stage=row['\xc3\x89tape'] skill.level=row['Niveau'] skill.section=rubrique skill.save() for next_ in filter(lambda x: x, {row["suivant1"], row["suivant2"], row["suivant3"]}): dependancies.setdefault(row["Code"], []).append(next_) for key, value in filter(lambda (x, y): y, dependancies.iteritems()): print "on", key skill = Skill.objects.get(code=key) for dep in value: print " *", dep, "->", key Skill.objects.get(code=dep).depends_on.add(skill)
def courses_advice(skills): skills_to_learn = [] for skill in skills: # Не ищет по альтернативным именам + уйти от формата когда на каждый # скил генерируется отдельный запрос skill_query = Skill.objects.filter(name__icontains=skill) if len(skill_query) > 0: skills_to_learn = skills_to_learn + [ skill.id for skill in skill_query ] else: # Создаем скил который есть у сотрудника, но нет у нас в базе s = Skill(name=skill, learn_materials=False) s.save() skills_to_learn.append(s.id) skill_qs = Skill.objects.filter(pk__in=skills_to_learn) return skill_qs
def import_skills(self, skillfile): """Import skills from skilltable_character.dnt to django models.""" log.info('import_skills\tfile\t%s' % skillfile.filename) skillfile.read_all() log.info('import_skills\tParsed DNT File') for row in skillfile.rows: if row.id > 30000: # These are reserved and conflict with real skills break try: name = self._uistring[row.NameID] job_id = self._jobmap[row.NeedJob] icon = row.IconImageIndex ultimate = name in _ultimates except KeyError: continue skill = Skill(name=name, job_id=job_id, icon=icon, ultimate=ultimate) skill.save() self._skillmap[row.id] = skill.id log.info('Parsed %d Skills' % Skill.objects.all().count())
def index(): form = SkillForm(request.form) form.update_category_choices() # adding a new skill if request.method == 'POST' and 'skill' in request.form.keys( ) and 'new_category' not in request.form.keys(): if form.validate() and form.no_special_characters(): if Skill.insert_new_skill( form.skill.data, dict(form.category.choices).get(form.category.data)): return redirect(url_for('main.index')) else: form.add_error(ErrorMessages.SKILL_NOT_UNIQUE) # going to category form elif request.method == 'POST' and 'skill' in request.form.keys( ) and 'new_category' in request.form.keys(): return redirect(url_for('content.category')) return render_template('content/index.html', result_set=Skill.get_all_visible_skills(), form=form, admin_email=Config.ADMIN_EMAIL)
def import_skills(self, skillfile): """Import skills from skilltable_character.dnt to django models.""" log.info('import_skills\tfile\t%s' % skillfile.filename) skillfile.read_all() log.info('import_skills\tParsed DNT File') for row in skillfile.rows: if row.id > 30000: # These are reserved and conflict with real skills break try: name = self._uistring[row.NameID] job_id = self._jobmap[row.NeedJob] icon = row.IconImageIndex ultimate = name in _ultimates except KeyError: continue skill = Skill(name=name, job_id=job_id, icon=icon, ultimate=ultimate) skill.save() self._skillmap[row.id] = skill.id log.info('Parsed %d Skills' % Skill.objects.all().count())
def delete_skill_by_id(skill_id): response = User.check_token(request) if response == 200 \ and User.query.filter_by(token=request.headers.get('Authorization')).first().email == Config.ADMIN_EMAIL: skill_to_be_deleted = Skill.query.filter_by(id_skill=skill_id).first() if skill_to_be_deleted is None: return bad_request(ErrorMessages.API_DELETE_SKILL_FAILURE) if Skill.delete_skill(skill_to_be_deleted.skill_name): response = jsonify(Config.API_DELETION_SUCCESS) response.status_code = 200 return response else: return bad_request(ErrorMessages.API_DELETE_SKILL_FAILURE) return error_response(response if response != 200 else 401)
def setUp(self): self.user1 = User(username='******') self.user1.set_password('12345') self.user1.save() self.teacher = Professor(user=self.user1) self.teacher.save() self.user2 = User(username="******") self.user2.save() self.student = Student(user=self.user2) self.student.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.lesson = Lesson(id=1, name="Lesson 1", stage_id=1) self.lesson.save() self.skill1 = Skill(code=422230, name="Compter deux par deux", description="") self.skill1.save() self.skill2 = Skill(code=422231, name="Lacer ses chaussures", description="") self.skill2.save() self.c = Client() self.c.login(username='******', password='******')
def add_skills(): skillsets = Skillset.objects.all() slice_index = int(len(skillsets) / 2) skills = [] try: for skillset in skillsets[:slice_index]: skills.extend([ Skill( skillset=skillset, name=faker.sentence(nb_words=6, variable_nb_words=True), description=faker.paragraph(nb_sentences=3, variable_nb_sentences=True), verified=True, weight=i + 1, ) for i in range(10) ]) except Exception as e: print('error creating verified skills!') print(e) try: for skillset in skillsets[slice_index:]: skills.extend([ Skill(skillset=skillset, name=faker.sentence(nb_words=6, variable_nb_words=True), description=faker.paragraph(nb_sentences=3, variable_nb_sentences=True), verified=False) for i in range(10) ]) except Exception as e: print('error creating unverified skills!') print(e) try: Skill.objects.bulk_create(skills) except Exception as e: print('error with skills bulk create') print(e)
def category(): form = CategoryForm(request.form) # adding a new category if request.method == 'POST' and 'go_back' not in request.form.keys(): if form.validate() and form.no_special_characters(): if Category.insert_new_category(form.category.data): return redirect(url_for('main.index')) else: form.add_error(ErrorMessages.CATEGORY_NOT_UNIQUE) elif request.method == 'POST' and 'go_back' in request.form.keys( ): # going back to main page return redirect(url_for('main.index')) return render_template('content/index.html', result_set=Skill.get_all_visible_skills(), form=form, admin_email=Config.ADMIN_EMAIL)
def setUp(self): self.user = User(username="******") self.user.set_password('12345') self.user.save() self.teacher_user = User(username="******") self.teacher_user.set_password('12345') self.teacher_user.save() self.student = Student(user=self.user) self.student.save() self.teacher = Professor(user=self.teacher_user) self.teacher.save() res1_content = {"title": "Res1"} self.res1 = Resource(added_by=self.teacher_user, content=res1_content) self.res1.save() self.section = Section(id=1, name="Section1") self.section.save() self.section.resource.add(self.res1) self.section.save() self.skill2 = Skill(id=2, name="Skill2", code="2") self.skill2.save() res2_content = {"title": "Res2"} self.res2 = Resource(added_by=self.teacher_user, content=res2_content) self.res2.save() self.skill3 = Skill(id=3, name="Skill3", code="3") self.skill3.save() self.skill3.resource.add(self.res2) self.skill3.save() self.skill4 = Skill(id=4, name="Skill4", code="4") self.skill4.section = self.section self.skill4.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.stage.skills.add(self.skill3) self.stage.skills.add(self.skill4) self.stage.save() self.lesson = Lesson(id=1, name="English", stage_id=1) self.lesson.save() self.lesson.students.add(self.student) self.lesson.professors.add(self.teacher) self.lesson.save() self.s1 = Client() self.s1.login(username=self.user.username, password='******') self.t1 = Client() self.t1.login(username=self.teacher_user.username, password='******')
def create_skill(): response = User.check_token(request) if response == 200 \ and User.query.filter_by(token=request.headers.get('Authorization')).first().email == Config.ADMIN_EMAIL: data = request.get_json() or {} if not validate_skill_json(data): return bad_request(ErrorMessages.API_FORMAT_NOT_ALLOWED) if not validate_new_skill_name(data['skill_name']): return bad_request(ErrorMessages.API_SKILL_NAME_ERROR) if not validate_new_category_name(data['category_name']): return bad_request(ErrorMessages.API_CATEGORY_NAME_ERROR) if Skill.insert_new_skill(data['skill_name'], data['category_name'], data.get('is_shown', 1)): response = jsonify( Skill.query.filter_by( skill_name=data['skill_name']).first().to_dict('name')) response.status_code = 201 response.headers['Location'] = url_for( 'api.get_skill_by_name', skill_name=data['skill_name']) return response else: return bad_request(ErrorMessages.API_SKILL_INSERT_ERROR) return error_response(response if response != 200 else 401)
def delete(): if request.method == 'POST': Skill.delete_skill(list(request.form.keys())[0]) return redirect(url_for('content.index'))
def renew_projects_info(): # Get all users and all their data... programmers = Programmer.objects.select_related().all() # For every user get all projects and skills github_urls = [p.github_account for p in programmers] fetched_project_data = loop.run_until_complete(get_users_projects(github_urls)) # Merge all data in the single dict user_to_projects = {} for user_projects in fetched_project_data: user_to_projects.update(user_projects) user_projects_to_create = defaultdict(list) projects_to_update = [] new_skills = [] for programmer in programmers: projects = user_to_projects.get(programmer.github_account) or [] for project in projects: # Check if project should be added if not GithubProjectManager.lang_is_supported(project['language']): continue try: project_in_db = programmer.github_projects.get(url=project['url']) except ObjectDoesNotExist: user_projects_to_create[programmer.pk].append(project) new_skills.append(project['language']) else: # Check if project has been updated recently if project_in_db.last_commit < project['updated_at']: projects_to_update.append(project) # maybe new commits made a difference new_skills.append(project['language']) # Bulk create skills... Skill.objects.bulk_create([Skill(name=skill_name) for skill_name in new_skills], ignore_conflicts=True) # Bulk create projects... if user_projects_to_create: language_to_skill = {} for skill_obj in Skill.objects.filter(name__in=new_skills): language_to_skill[skill_obj.name] = skill_obj GithubProject.objects.bulk_create( [GithubProject(skill=language_to_skill.get(project['language']), url=project['url'], last_commit=project['updated_at']) for user_projects in user_projects_to_create.values() for project in user_projects ] ) # Bulk update projects... if projects_to_update: urls_to_update = {project['url']: project['updated_at'] for project in projects_to_update} projects_to_update_qs = GithubProject.objects.filter(url__in=urls_to_update) for project in projects_to_update_qs: project.last_commit = urls_to_update[project.url] GithubProject.objects.bulk_update(projects_to_update_qs, ['last_commit'])
def handle(self, *args, **options): dependancies = {} rubrique = '' to_link_to_stage = {} level_stages = {} for row in csv.DictReader(open(args[0], "r"), delimiter=",", quotechar='"'): if not filter(None, row.values()): continue rubrique = row['Rubrique'] if row['Rubrique'] else rubrique if Skill.objects.filter(code=row["Code"]).exists(): skill = Skill.objects.get(code=row["Code"]) skill.depends_on.clear() print "update", row["Code"] else: skill = Skill() print "create", row["Code"] skill.code = row["Code"] level = row['Niveau'] stage_name = row['\xc3\x89tape'].strip().decode("Utf-8") if Stage.objects.filter(level=level, name=stage_name).exists(): stage = Stage.objects.get(level=level, name=stage_name) stage.name = stage_name else: stage = Stage.objects.create(name=stage_name, level=level) if not stage.short_name: stage.short_name = stage_short_name[stage.name] stage.save() if stage not in to_link_to_stage: to_link_to_stage[stage] = [] if level not in level_stages: level_stages[level] = stage to_link_to_stage[stage].append(skill) skill.name=row['Intitul\xc3\xa9'] skill.description=row['Commentaires'] skill.section=rubrique skill.image=row["Image FontAwesome"] skill.save() for next_ in filter(lambda x: x, {row[key] for key in row.keys() if key.startswith("suivant") and key.endswith(tuple(map(str, range(10))))}): dependancies.setdefault(row["Code"], []).append(next_) for key, value in filter(lambda (x, y): y, dependancies.iteritems()): print "on", key skill = Skill.objects.get(code=key) for dep in value: print " *", dep, "->", key Skill.objects.get(code=dep).depends_on.add(skill)
def index(): if current_user.is_authenticated: return redirect(url_for('content.index')) return render_template('index.html', result_set=Skill.get_all_visible_skills(), admin_email=Config.ADMIN_EMAIL)
def setUp(self): self.user = User(username="******") self.user.set_password('12345') self.user.save() self.second_user = User(username="******") self.second_user.set_password('12345') self.second_user.save() self.teacher_user = User(username="******") self.teacher_user.save() self.second_teacher_user = User(username="******") self.second_teacher_user.save() self.student = Student(user=self.user) self.student.save() self.second_student = Student(user=self.second_user) self.second_student.save() self.teacher = Professor(user=self.teacher_user) self.teacher.save() self.second_teacher = Professor(user=self.second_teacher_user) self.second_teacher.save() self.skill1 = Skill(id=1, name="Skill1", code="1") self.skill1.save() self.skill2 = Skill(id=2, name="Skill2", code="2") self.skill2.save() self.section = Section(id=1, name="Section1") self.section.save() self.skill3 = Skill(id=3, name="Skill3", code="3") self.skill3.section = self.section self.skill3.save() self.skill4 = Skill(id=4, name="Skill4", code="4") self.skill4.section = self.section self.skill4.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.stage.skills.add(self.skill1) self.stage.skills.add(self.skill2) self.stage.save() self.second_stage = Stage(id=2, name="Stage2", level=1) self.second_stage.save() self.second_stage.skills.add(self.skill3) self.second_stage.skills.add(self.skill4) self.second_stage.save() self.lesson = Lesson(id=1, name="English", stage_id=1) self.lesson.save() self.lesson.students.add(self.student) self.lesson.students.add(self.second_student) self.lesson.professors.add(self.teacher) self.lesson.save() self.second_lesson = Lesson(id=2, name="French", stage_id=2) self.second_lesson.save() self.second_lesson.students.add(self.second_student) self.second_lesson.professors.add(self.teacher) self.second_lesson.save() self.thread = Thread(title="Help", author=self.user, recipient=self.teacher_user) self.thread.save() self.second_thread = Thread(title="Send help", author=self.second_user, lesson=self.second_lesson) self.second_thread.save() self.third_thread = Thread(title="Information regarding w/e", author=self.teacher_user, professor=self.teacher) self.third_thread.save() self.fourth_thread = Thread(title="Information regarding spam", author=self.teacher_user, professor=self.teacher) self.fourth_thread.save() self.c1 = Client() self.c1.login(username=self.user.username, password='******') self.c2 = Client() self.c2.login(username=self.second_user.username, password='******')
class TestMisc(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password('12345') self.user.save() self.second_user = User(username="******") self.second_user.set_password('12345') self.second_user.save() self.teacher_user = User(username="******") self.teacher_user.save() self.second_teacher_user = User(username="******") self.second_teacher_user.save() self.student = Student(user=self.user) self.student.save() self.second_student = Student(user=self.second_user) self.second_student.save() self.teacher = Professor(user=self.teacher_user) self.teacher.save() self.second_teacher = Professor(user=self.second_teacher_user) self.second_teacher.save() self.skill1 = Skill(id=1, name="Skill1", code="1") self.skill1.save() self.skill2 = Skill(id=2, name="Skill2", code="2") self.skill2.save() self.section = Section(id=1, name="Section1") self.section.save() self.skill3 = Skill(id=3, name="Skill3", code="3") self.skill3.section = self.section self.skill3.save() self.skill4 = Skill(id=4, name="Skill4", code="4") self.skill4.section = self.section self.skill4.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.stage.skills.add(self.skill1) self.stage.skills.add(self.skill2) self.stage.save() self.second_stage = Stage(id=2, name="Stage2", level=1) self.second_stage.save() self.second_stage.skills.add(self.skill3) self.second_stage.skills.add(self.skill4) self.second_stage.save() self.lesson = Lesson(id=1, name="English", stage_id=1) self.lesson.save() self.lesson.students.add(self.student) self.lesson.students.add(self.second_student) self.lesson.professors.add(self.teacher) self.lesson.save() self.second_lesson = Lesson(id=2, name="French", stage_id=2) self.second_lesson.save() self.second_lesson.students.add(self.second_student) self.second_lesson.professors.add(self.teacher) self.second_lesson.save() self.thread = Thread(title="Help", author=self.user, recipient=self.teacher_user) self.thread.save() self.second_thread = Thread(title="Send help", author=self.second_user, lesson=self.second_lesson) self.second_thread.save() self.third_thread = Thread(title="Information regarding w/e", author=self.teacher_user, professor=self.teacher) self.third_thread.save() self.fourth_thread = Thread(title="Information regarding spam", author=self.teacher_user, professor=self.teacher) self.fourth_thread.save() self.c1 = Client() self.c1.login(username=self.user.username, password='******') self.c2 = Client() self.c2.login(username=self.second_user.username, password='******') def test_get_skills_user(self): skills, sections = get_skills(FakeRequest(self.user)) self.assertEquals(len(skills), 2) self.assertListEqual(skills, [self.skill1, self.skill2]) self.assertEquals(len(sections), 0) def test_get_skills_second_user(self): skills, sections = get_skills(FakeRequest(self.second_user)) self.assertEquals(len(skills), 4) self.assertListEqual( skills, [self.skill1, self.skill2, self.skill3, self.skill4]) self.assertEqual(len(sections), 1) self.assertEqual(sections[0], self.skill3.section) def test_get_professors_user(self): response = self.c1.get('/forum/write/professors/') json_data = json.loads(response.content) data = json_data["data"] professor = data[0] self.assertEquals(len(data), 1) self.assertEquals( professor, { "id": self.teacher.id, "username": self.teacher.user.username, "first_name": self.teacher.user.first_name, "last_name": self.teacher.user.last_name }) def test_get_professors_second_user(self): response = self.c2.get('/forum/write/professors/') json_data = json.loads(response.content) data = json_data["data"] professor = data[0] self.assertEquals(len(data), 1) self.assertEquals( professor, { "id": self.teacher.id, "username": self.teacher.user.username, "first_name": self.teacher.user.first_name, "last_name": self.teacher.user.last_name }) def test_get_lessons_user(self): response = self.c1.get('/forum/write/lessons/') json_data = json.loads(response.content) data = json_data["data"] self.assertEquals(len(data), 1) lesson = data[0] self.assertEqual(lesson["id"], self.lesson.id) self.assertEqual(lesson["name"], self.lesson.name) def test_get_lessons_second_user(self): response = self.c2.get('/forum/write/lessons/') json_data = json.loads(response.content) data = json_data["data"] self.assertEquals(len(data), 2) lesson = data[0] self.assertEqual(lesson["id"], self.lesson.id) self.assertEqual(lesson["name"], self.lesson.name) lesson2 = data[1] self.assertEqual(lesson2["id"], self.second_lesson.id) self.assertEqual(lesson2["name"], self.second_lesson.name) def test_get_users(self): response = self.c1.get('/forum/write/users/') json_data = json.loads(response.content) data = json_data["data"] users = User.objects.all() for i, user in enumerate(data): self.assertEquals( user, { 'id': users[i].id, 'username': users[i].username, 'first_name': users[i].first_name, 'last_name': users[i].last_name })
def __init__(self, *args, **kwargs): super(SkillForm, self).__init__(*args, **kwargs) # If the form.instance.pk it's None means that the form came from CreateView, so generate the Max value Order if self.instance.pk is None: self.initial['order'] = Skill.GetOrderMaxValue(Skill)
class TestPostThread(TestCase): def setUp(self): self.user1 = User(username='******') self.user1.set_password('12345') self.user1.save() self.teacher = Professor(user=self.user1) self.teacher.save() self.user2 = User(username="******") self.user2.save() self.student = Student(user=self.user2) self.student.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.lesson = Lesson(id=1, name="Lesson 1", stage_id=1) self.lesson.save() self.skill1 = Skill(code=422230, name="Compter deux par deux", description="") self.skill1.save() self.skill2 = Skill(code=422231, name="Lacer ses chaussures", description="") self.skill2.save() self.c = Client() self.c.login(username='******', password='******') def test_post_valid_new_public_thread(self): new_thread = { "title": "titre_1", "visibdata": str(self.teacher.id), "skills": [422230, 422231], "content": "message_1", "visibility": "public" } response = self.c.post('/forum/write/', data=new_thread) new_thread = Thread.objects.order_by('-pk')[0] last_msg = Message.objects.order_by('-pk')[0] self.assertEquals(response.status_code, 302) self.assertEquals(new_thread.title, "titre_1") self.assertEquals(last_msg.content, "message_1") def test_post_valid_new_private_thread(self): new_thread = { "title": "titre_2", "visibdata": str(self.user2.id), "skills": [422230, 422231], "content": "message_2", "visibility": "private" } response = self.c.post('/forum/write/', data=new_thread) new_thread = Thread.objects.order_by('-pk')[0] last_msg = Message.objects.order_by('-pk')[0] self.assertEquals(response.status_code, 302) self.assertEquals(new_thread.title, "titre_2") self.assertEquals(last_msg.content, "message_2") def test_post_valid_new_class_thread(self): new_thread = { "title": "titre_3", "visibdata": str(self.lesson.id), "skills": [422230, 422231], "content": "message_3", "visibility": "class" } response = self.c.post('/forum/write/', data=new_thread) new_thread = Thread.objects.order_by('-pk')[0] last_msg = Message.objects.order_by('-pk')[0] self.assertEquals(response.status_code, 302) self.assertEquals(new_thread.title, "titre_3") self.assertEquals(last_msg.content, "message_3") def test_post_invalid_new_thread_blank_req_fields(self): thread_cnt_before = Thread.objects.all().count() msg_cnt_before = Message.objects.all().count() new_thread = { "title": "", "visibdata": "", "skills": "", "content": "", "visibility": "" } response = self.c.post('/forum/write/', data=new_thread) self.assertEquals(response.status_code, 200) self.assertEquals(thread_cnt_before, Thread.objects.all().count()) self.assertEquals(msg_cnt_before, Message.objects.all().count()) def test_post_invalid_new_thread_unknown_skills(self): thread_cnt_before = Thread.objects.all().count() msg_cnt_before = Message.objects.all().count() new_thread = { "title": "titre_5", "visibdata": str(self.lesson.id), "skills": ["l", "m"], "content": "message_5", "visibility": "class" } response = self.c.post('/forum/write/', data=new_thread) self.assertEquals(response.status_code, 200) self.assertEquals(thread_cnt_before, Thread.objects.all().count()) self.assertEquals(msg_cnt_before, Message.objects.all().count()) def test_post_invalid_new_private_thread_unknown_recipient(self): thread_cnt_before = Thread.objects.all().count() msg_cnt_before = Message.objects.all().count() new_thread = { "title": "titre_6", "visibdata": "unknown", "skills": "422230 422231", "content": "message_6", "visibility": "private" } response = self.c.post('/forum/write/', data=new_thread) self.assertEquals(response.status_code, 200) self.assertEquals(thread_cnt_before, Thread.objects.all().count()) self.assertEquals(msg_cnt_before, Message.objects.all().count()) def test_post_invalid_new_class_thread_unknown_class(self): thread_cnt_before = Thread.objects.all().count() msg_cnt_before = Message.objects.all().count() new_thread = { "title": "titre_7", "visibdata": "unknown", "skills": [422230, 422231], "content": "message_7", "visibility": "class" } response = self.c.post('/forum/write/', data=new_thread) self.assertEquals(response.status_code, 200) self.assertEquals(thread_cnt_before, Thread.objects.all().count()) self.assertEquals(msg_cnt_before, Message.objects.all().count()) def test_post_invalid_new_public_thread_unknown_professor(self): thread_cnt_before = Thread.objects.all().count() msg_cnt_before = Message.objects.all().count() new_thread = { "title": "titre_8", "visibdata": "unknown", "skills": [422230, 422231], "content": "message_7", "visibility": "public" } response = self.c.post('/forum/write/', data=new_thread) self.assertEquals(response.status_code, 200) self.assertEquals(thread_cnt_before, Thread.objects.all().count()) self.assertEquals(msg_cnt_before, Message.objects.all().count())
class TestResources(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password('12345') self.user.save() self.teacher_user = User(username="******") self.teacher_user.set_password('12345') self.teacher_user.save() self.student = Student(user=self.user) self.student.save() self.teacher = Professor(user=self.teacher_user) self.teacher.save() res1_content = {"title": "Res1"} self.res1 = Resource(added_by=self.teacher_user, content=res1_content) self.res1.save() self.section = Section(id=1, name="Section1") self.section.save() self.section.resource.add(self.res1) self.section.save() self.skill2 = Skill(id=2, name="Skill2", code="2") self.skill2.save() res2_content = {"title": "Res2"} self.res2 = Resource(added_by=self.teacher_user, content=res2_content) self.res2.save() self.skill3 = Skill(id=3, name="Skill3", code="3") self.skill3.save() self.skill3.resource.add(self.res2) self.skill3.save() self.skill4 = Skill(id=4, name="Skill4", code="4") self.skill4.section = self.section self.skill4.save() self.stage = Stage(id=1, name="Stage1", level=1) self.stage.save() self.stage.skills.add(self.skill3) self.stage.skills.add(self.skill4) self.stage.save() self.lesson = Lesson(id=1, name="English", stage_id=1) self.lesson.save() self.lesson.students.add(self.student) self.lesson.professors.add(self.teacher) self.lesson.save() self.s1 = Client() self.s1.login(username=self.user.username, password='******') self.t1 = Client() self.t1.login(username=self.teacher_user.username, password='******') def test_get_all_resources(self): response = self.s1.get('/forum/write/resources/') json_data = json.loads(response.content) data = json_data["data"] self.assertEquals(len(data), 2) correct_ids = [self.res1.id, self.res2.id] self.assertTrue(data[0]["id"] in correct_ids) self.assertTrue(data[1]["id"] in correct_ids) def test_get_section_resources(self): response = self.s1.get( '/forum/write/resources/?section={0}&skills[]={1}'.format( self.section.id, 0)) json_data = json.loads(response.content) data = json_data["data"] self.assertEquals(len(data), 1) self.assertEquals(data[0]["title"], self.res1.content['title']) def test_get_skills_resources(self): response = self.s1.get( '/forum/write/resources/?skills[]={0}§ion={1}'.format( self.skill3.id, 0)) json_data = json.loads(response.content) data = json_data["data"] self.assertEquals(len(data), 1) self.assertEquals(data[0]["title"], self.res2.content['title'])
def handle(self, *args, **options): dependancies = {} rubrique = '' to_link_to_stage = {} level_stages = {} for row in csv.DictReader(open(args[0], "r"), delimiter=",", quotechar='"'): if not filter(None, row.values()): continue rubrique = row['Rubrique'] if row['Rubrique'] else rubrique if Skill.objects.filter(code=row["Code"]).exists(): skill = Skill.objects.get(code=row["Code"]) skill.depends_on.clear() print "update", row["Code"] else: skill = Skill() print "create", row["Code"] skill.code = row["Code"] level = row['Niveau'] stage_name = row['\xc3\x89tape'].strip().decode("Utf-8") if Stage.objects.filter(level=level, name=stage_name).exists(): stage = Stage.objects.get(level=level, name=stage_name) stage.name = stage_name else: stage = Stage.objects.create(name=stage_name, level=level) if not stage.short_name: stage.short_name = stage_short_name[stage.name] stage.save() if stage not in to_link_to_stage: to_link_to_stage[stage] = [] if level not in level_stages: level_stages[level] = stage to_link_to_stage[stage].append(skill) skill.name = row['Intitul\xc3\xa9'] skill.description = row['Commentaires'] skill.section = rubrique skill.image = row["Image FontAwesome"] skill.save() for next_ in filter( lambda x: x, { row[key] for key in row.keys() if key.startswith("suivant") and key.endswith(tuple(map(str, range(10)))) }): dependancies.setdefault(row["Code"], []).append(next_) for key, value in filter(lambda (x, y): y, dependancies.iteritems()): print "on", key skill = Skill.objects.get(code=key) for dep in value: print " *", dep, "->", key Skill.objects.get(code=dep).depends_on.add(skill)