searchtopics analog Skills ohne Level #34

Merged
weeman merged 2 commits from feature-searchtopics into main 2021-07-06 21:22:20 +02:00
5 changed files with 101 additions and 5 deletions

View File

@ -6,7 +6,8 @@ import csv
import logging import logging
from app import app, db from app import app, db
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSkill, User from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSearchtopic, \
ProfileSkill, User
def seed_contacttypes(): def seed_contacttypes():
@ -92,6 +93,12 @@ def seed(dev: bool):
peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5) peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5)
db.session.add(peters_php_skill) db.session.add(peters_php_skill)
peters_python_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=3)
db.session.add(peters_python_searchtopic)
peters_php_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=1)
db.session.add(peters_php_searchtopic)
peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5) peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5)
db.session.add(peter_de) db.session.add(peter_de)

View File

@ -5,7 +5,8 @@
from flask import make_response, request from flask import make_response, request
from sqlalchemy import not_ from sqlalchemy import not_
from ki.models import Address, Contact, ContactType, Language, User, Profile, ProfileLanguage, ProfileSkill, Skill from ki.models import Address, Contact, ContactType, Language, User, Profile, ProfileLanguage, ProfileSearchtopic, \
ProfileSkill, Skill
from app import db from app import db
@ -71,6 +72,30 @@ def update_skills(profile, skills_data):
not_(ProfileSkill.skill_id.in_(profile_skill_ids))).delete() not_(ProfileSkill.skill_id.in_(profile_skill_ids))).delete()
def update_searchtopics(profile, searchtopics_data):
profile_searchtopics_ids = []
for searchtopic_data in searchtopics_data:
skill_name = searchtopic_data["skill"]["name"]
skill = Skill.query.filter(Skill.name == skill_name).first()
if (skill is None):
skill = Skill(name=skill_name)
db.session.add(skill)
profile_searchtopic = ProfileSearchtopic.query.filter(ProfileSearchtopic.profile == profile,
ProfileSearchtopic.skill == skill).first()
if (profile_searchtopic is None):
profile_searchtopic = ProfileSearchtopic(profile=profile, skill=skill)
db.session.add(profile_searchtopic)
profile_searchtopics_ids.append(skill.id)
ProfileSearchtopic.query.filter(ProfileSearchtopic.profile == profile,
not_(ProfileSearchtopic.skill_id.in_(profile_searchtopics_ids))).delete()
def update_contacts(profile, contacts_data): def update_contacts(profile, contacts_data):
contact_ids_to_be_deleted = list(map(lambda c: c.id, profile.contacts)) contact_ids_to_be_deleted = list(map(lambda c: c.id, profile.contacts))
@ -117,6 +142,7 @@ def update_profile(user_id: int):
update_address(profile, request.json.get("address", {})) update_address(profile, request.json.get("address", {}))
update_contacts(profile, request.json.get("contacts", {})) update_contacts(profile, request.json.get("contacts", {}))
update_skills(profile, request.json.get("skills", {})) update_skills(profile, request.json.get("skills", {}))
update_searchtopics(profile, request.json.get("searchtopics"))
update_languages(profile, request.json.get("languages", {})) update_languages(profile, request.json.get("languages", {}))
db.session.commit() db.session.commit()

View File

@ -41,6 +41,7 @@ class Profile(db.Model):
contacts = relationship("Contact") contacts = relationship("Contact")
address = relationship("Address", uselist=False, back_populates="profile") address = relationship("Address", uselist=False, back_populates="profile")
skills = relationship("ProfileSkill", back_populates="profile") skills = relationship("ProfileSkill", back_populates="profile")
searchtopics = relationship("ProfileSearchtopic", back_populates="profile")
languages = relationship("ProfileLanguage", back_populates="profile") languages = relationship("ProfileLanguage", back_populates="profile")
def to_dict(self): def to_dict(self):
@ -55,6 +56,7 @@ class Profile(db.Model):
"address": self.address.to_dict(), "address": self.address.to_dict(),
"contacts": list(map(lambda contact: contact.to_dict(), self.contacts)), "contacts": list(map(lambda contact: contact.to_dict(), self.contacts)),
"skills": list(map(lambda skill: skill.to_dict(), self.skills)), "skills": list(map(lambda skill: skill.to_dict(), self.skills)),
"searchtopics": list(map(lambda searchtopic: searchtopic.to_dict(), self.searchtopics)),
"languages": list(map(lambda language: language.to_dict(), self.languages)) "languages": list(map(lambda language: language.to_dict(), self.languages))
} }
@ -137,6 +139,7 @@ class Skill(db.Model):
name = Column(String(25), unique=True, nullable=False) name = Column(String(25), unique=True, nullable=False)
profiles = relationship("ProfileSkill", back_populates="skill") profiles = relationship("ProfileSkill", back_populates="skill")
searchtopics = relationship("ProfileSearchtopic", back_populates="skill")
def to_dict(self): def to_dict(self):
return {"id": self.id, "name": self.name, "icon_url": "/skills/{}/icon".format(self.id)} return {"id": self.id, "name": self.name, "icon_url": "/skills/{}/icon".format(self.id)}
@ -156,6 +159,19 @@ class ProfileSkill(db.Model):
return {"profile_id": self.profile_id, "skill": self.skill.to_dict(), "level": self.level} return {"profile_id": self.profile_id, "skill": self.skill.to_dict(), "level": self.level}
class ProfileSearchtopic(db.Model):
__tablename__ = "profile_searchtopic"
profile_id = Column(Integer, ForeignKey("profile.id"), primary_key=True)
skill_id = Column(Integer, ForeignKey("skill.id"), primary_key=True)
profile = relationship("Profile", back_populates="searchtopics")
skill = relationship("Skill", back_populates="searchtopics")
def to_dict(self):
return {"profile_id": self.profile_id, "skill": self.skill.to_dict()}
class Language(db.Model): class Language(db.Model):
__tablename__ = "language" __tablename__ = "language"

View File

@ -71,6 +71,19 @@ class TestProfileEndpoint(ApiTest):
}, },
"level": 5 "level": 5
}], }],
"searchtopics": [{
"profile_id": 1,
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
}
}, {
"profile_id": 1,
"skill": {
"name": "Assembler"
}
}],
"languages": [{ "languages": [{
"id": 1, "id": 1,
"language": { "language": {
@ -134,6 +147,17 @@ class TestProfileEndpoint(ApiTest):
self.assertEqual(second_skill.skill.name, "Tschunkproduktion") self.assertEqual(second_skill.skill.name, "Tschunkproduktion")
self.assertEqual(second_skill.level, 5) self.assertEqual(second_skill.level, 5)
searchtopics = profile.searchtopics
self.assertEqual(len(searchtopics), 2)
first_searchtopic = searchtopics[0]
self.assertEqual(first_searchtopic.skill.id, 3)
self.assertEqual(first_searchtopic.skill.name, "Python")
second_searchtopic = searchtopics[1]
self.assertEqual(second_searchtopic.skill.id, 14)
self.assertEqual(second_searchtopic.skill.name, "Assembler")
languages = profile.languages languages = profile.languages
self.assertEqual(len(languages), 2) self.assertEqual(len(languages), 2)
@ -216,6 +240,21 @@ class TestProfileEndpoint(ApiTest):
}, },
"level": 3 "level": 3
}], }],
"searchtopics": [{
"profile_id": 1,
"skill": {
"id": 1,
"name": "PHP",
"icon_url": "/skills/1/icon"
}
}, {
"profile_id": 1,
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
}
}],
"languages": [{ "languages": [{
"profile_id": 1, "profile_id": 1,
"language": { "language": {

View File

@ -1,8 +1,8 @@
"""Initial migration. """Initial migration.
Revision ID: 808fe55111df Revision ID: 44b45a772abd
Revises: Revises:
Create Date: 2021-07-05 20:13:50.560579 Create Date: 2021-07-06 21:19:44.217722
""" """
from alembic import op from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '808fe55111df' revision = '44b45a772abd'
down_revision = None down_revision = None
branch_labels = None branch_labels = None
depends_on = None depends_on = None
@ -77,6 +77,13 @@ def upgrade():
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ), sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.PrimaryKeyConstraint('profile_id', 'language_id') sa.PrimaryKeyConstraint('profile_id', 'language_id')
) )
op.create_table('profile_searchtopic',
sa.Column('profile_id', sa.Integer(), nullable=False),
sa.Column('skill_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.ForeignKeyConstraint(['skill_id'], ['skill.id'], ),
sa.PrimaryKeyConstraint('profile_id', 'skill_id')
)
op.create_table('profile_skill', op.create_table('profile_skill',
sa.Column('profile_id', sa.Integer(), nullable=False), sa.Column('profile_id', sa.Integer(), nullable=False),
sa.Column('skill_id', sa.Integer(), nullable=False), sa.Column('skill_id', sa.Integer(), nullable=False),
@ -108,6 +115,7 @@ def downgrade():
op.drop_table('token') op.drop_table('token')
op.drop_table('user') op.drop_table('user')
op.drop_table('profile_skill') op.drop_table('profile_skill')
op.drop_table('profile_searchtopic')
op.drop_table('profile_language') op.drop_table('profile_language')
op.drop_table('contact') op.drop_table('contact')
op.drop_table('address') op.drop_table('address')