implement searchtopics

This commit is contained in:
weeman 2021-07-05 20:39:49 +02:00
parent 8e087198a4
commit 2f83e206e1
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
5 changed files with 90 additions and 120 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,118 +0,0 @@
"""Initial migration.
Revision ID: 808fe55111df
Revises:
Create Date: 2021-07-05 20:13:50.560579
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '808fe55111df'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('contacttype',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=25), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('language',
sa.Column('id', sa.String(length=2), nullable=False),
sa.Column('name', sa.String(length=25), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('profile',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nickname', sa.String(length=25), nullable=False),
sa.Column('pronouns', sa.String(length=25), nullable=True),
sa.Column('volunteerwork', sa.String(length=4000), nullable=True),
sa.Column('freetext', sa.String(length=4000), nullable=True),
sa.Column('availability', sa.String(length=4000), nullable=True),
sa.Column('visible', sa.Boolean(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('nickname')
)
op.create_table('skill',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=25), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('address',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=25), nullable=True),
sa.Column('street', sa.String(length=25), nullable=True),
sa.Column('house_number', sa.String(length=10), nullable=True),
sa.Column('additional', sa.String(length=25), nullable=True),
sa.Column('postcode', sa.String(length=10), nullable=True),
sa.Column('city', sa.String(length=25), nullable=True),
sa.Column('country', sa.String(length=25), nullable=True),
sa.Column('profile_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('contact',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('profile_id', sa.Integer(), nullable=False),
sa.Column('contacttype_id', sa.Integer(), nullable=False),
sa.Column('content', sa.String(length=200), nullable=False),
sa.ForeignKeyConstraint(['contacttype_id'], ['contacttype.id'], ),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('profile_language',
sa.Column('profile_id', sa.Integer(), nullable=False),
sa.Column('language_id', sa.Integer(), nullable=False),
sa.Column('level', sa.SmallInteger(), nullable=False),
sa.ForeignKeyConstraint(['language_id'], ['language.id'], ),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.PrimaryKeyConstraint('profile_id', 'language_id')
)
op.create_table('profile_skill',
sa.Column('profile_id', sa.Integer(), nullable=False),
sa.Column('skill_id', sa.Integer(), nullable=False),
sa.Column('level', sa.SmallInteger(), nullable=False),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.ForeignKeyConstraint(['skill_id'], ['skill.id'], ),
sa.PrimaryKeyConstraint('profile_id', 'skill_id')
)
op.create_table('user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('auth_id', sa.String(length=50), nullable=False),
sa.Column('profile_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('auth_id')
)
op.create_table('token',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('token', sa.String(length=36), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('token')
op.drop_table('user')
op.drop_table('profile_skill')
op.drop_table('profile_language')
op.drop_table('contact')
op.drop_table('address')
op.drop_table('skill')
op.drop_table('profile')
op.drop_table('language')
op.drop_table('contacttype')
# ### end Alembic commands ###