implement updating skills

This commit is contained in:
weeman 2021-06-27 13:38:16 +02:00
parent 68b84f50ca
commit 15b71459ee
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
7 changed files with 85 additions and 65 deletions

View File

@ -1,3 +1,4 @@
[style] [style]
based_on_style = pep8 based_on_style = pep8
allow_split_before_dict_value = 0 allow_split_before_dict_value = 0
column_limit = 120

View File

@ -3,3 +3,11 @@ id,name
2,Vue.js 2,Vue.js
3,Python 3,Python
4,JavaScript 4,JavaScript
5,Angular
6,Flask
7,SQLAlchemy
8,Rust
9,MySQL
10,PostgreSQL
11,SQLite
12,Node.js

1 id name
3 2 Vue.js
4 3 Python
5 4 JavaScript
6 5 Angular
7 6 Flask
8 7 SQLAlchemy
9 8 Rust
10 9 MySQL
11 10 PostgreSQL
12 11 SQLite
13 12 Node.js

View File

@ -50,17 +50,13 @@ def seed(dev: bool):
matrix_type = ContactType(name="Matrix") matrix_type = ContactType(name="Matrix")
db.session.add(matrix_type) db.session.add(matrix_type)
matrix_contact = Contact(profile=peters_profile, matrix_contact = Contact(profile=peters_profile, contacttype=matrix_type, content="@peter:wtf-eg.de")
contacttype=matrix_type,
content="@peter:wtf-eg.de")
db.session.add(matrix_contact) db.session.add(matrix_contact)
email_type = ContactType(name="E-Mail") email_type = ContactType(name="E-Mail")
db.session.add(email_type) db.session.add(email_type)
email_contact = Contact(profile=peters_profile, email_contact = Contact(profile=peters_profile, contacttype=email_type, content="peter@wtf-eg.de")
contacttype=email_type,
content="peter@wtf-eg.de")
db.session.add(email_contact) db.session.add(email_contact)
peters_address = Address(name="Peter Nichtlustig", peters_address = Address(name="Peter Nichtlustig",
@ -73,19 +69,16 @@ def seed(dev: bool):
profile=peters_profile) profile=peters_profile)
db.session.add(peters_address) db.session.add(peters_address)
peters_python_skill = ProfileSkill(profile=peters_profile, peters_python_skill = ProfileSkill(profile=peters_profile, skill_id=3, level=3)
skill_id=3,
level=5)
db.session.add(peters_python_skill) db.session.add(peters_python_skill)
peter_de = ProfileLanguage(profile=peters_profile, peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5)
language_id="de", db.session.add(peters_php_skill)
level=5)
peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5)
db.session.add(peter_de) db.session.add(peter_de)
peter_fr = ProfileLanguage(profile=peters_profile, peter_fr = ProfileLanguage(profile=peters_profile, language_id="fr", level=3)
language_id="fr",
level=3)
db.session.add(peter_fr) db.session.add(peter_fr)
db.session.commit() db.session.commit()

View File

@ -1,6 +1,7 @@
from flask import make_response, request from flask import make_response, request
from sqlalchemy import not_
from ki.models import Address, Contact, ContactType, User, Profile from ki.models import Address, Contact, ContactType, User, Profile, ProfileSkill, Skill
from app import db from app import db
@ -20,15 +21,39 @@ def update_address(profile, address_data):
address.country = address_data.get("country", "") address.country = address_data.get("country", "")
def update_skills(profile, skills_data):
profile_skill_ids = []
for skill_data in skills_data:
skill_name = skill_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_skill = ProfileSkill.query.filter(ProfileSkill.profile == profile, ProfileSkill.skill == skill).first()
if (profile_skill is None):
profile_skill = ProfileSkill(profile=profile, skill=skill)
db.session.add(profile_skill)
profile_skill.level = skill_data["level"]
profile_skill_ids.append(skill.id)
ProfileSkill.query.filter(ProfileSkill.profile == profile,
not_(ProfileSkill.skill_id.in_(profile_skill_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))
for contact_data in contacts_data: for contact_data in contacts_data:
contacttype = ContactType.query.filter( contacttype_name = contact_data["contacttype"]["name"]
ContactType.name == contact_data["contacttype"]["name"]).first() contacttype = ContactType.query.filter(ContactType.name == contacttype_name).first()
if (contacttype is None): if (contacttype is None):
contacttype = ContactType(name=contact_data["contacttype"]["name"]) contacttype = ContactType(name=contacttype_name)
db.session.add(contacttype) db.session.add(contacttype)
if "id" in contact_data: if "id" in contact_data:
@ -63,6 +88,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", {}))
db.session.commit() db.session.commit()

View File

@ -29,10 +29,7 @@ class Profile(db.Model):
volunteerwork = Column(String(4000), default="") volunteerwork = Column(String(4000), default="")
freetext = Column(String(4000), default="") freetext = Column(String(4000), default="")
created = Column(DateTime, nullable=False, default=datetime.now) created = Column(DateTime, nullable=False, default=datetime.now)
updated = Column(DateTime, updated = Column(DateTime, onupdate=datetime.now, nullable=False, default=datetime.now)
onupdate=datetime.now,
nullable=False,
default=datetime.now)
user = relationship("User", back_populates="profile", uselist=False) user = relationship("User", back_populates="profile", uselist=False)
contacts = relationship("Contact") contacts = relationship("Contact")
@ -48,11 +45,9 @@ class Profile(db.Model):
"volunteerwork": self.volunteerwork, "volunteerwork": self.volunteerwork,
"freetext": self.freetext, "freetext": self.freetext,
"address": self.address.to_dict(), "address": self.address.to_dict(),
"contacts": list( "contacts": list(map(lambda contact: contact.to_dict(), self.contacts)),
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)),
"languages": list( "languages": list(map(lambda language: language.to_dict(), self.languages))
map(lambda language: language.to_dict(), self.languages))
} }
@ -75,9 +70,7 @@ class Contact(db.Model):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False) profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
profile = relationship("Profile", back_populates="contacts") profile = relationship("Profile", back_populates="contacts")
contacttype_id = Column(Integer, contacttype_id = Column(Integer, ForeignKey("contacttype.id"), nullable=False)
ForeignKey("contacttype.id"),
nullable=False)
contacttype = relationship("ContactType") contacttype = relationship("ContactType")
content = Column(String(200), nullable=False) content = Column(String(200), nullable=False)
@ -138,11 +131,7 @@ class Skill(db.Model):
profiles = relationship("ProfileSkill", back_populates="skill") profiles = relationship("ProfileSkill", back_populates="skill")
def to_dict(self): def to_dict(self):
return { return {"id": self.id, "name": self.name, "icon_url": "/skills/{}/icon".format(self.id)}
"id": self.id,
"name": self.name,
"icon_url": "/skills/{}/icon".format(self.id)
}
class ProfileSkill(db.Model): class ProfileSkill(db.Model):
@ -156,11 +145,7 @@ class ProfileSkill(db.Model):
skill = relationship("Skill", back_populates="profiles") skill = relationship("Skill", back_populates="profiles")
def to_dict(self): def to_dict(self):
return { return {"profile_id": self.profile_id, "skill": self.skill.to_dict(), "level": self.level}
"profile_id": self.profile_id,
"skill": self.skill.to_dict(),
"level": self.level
}
class Language(db.Model): class Language(db.Model):
@ -172,11 +157,7 @@ class Language(db.Model):
profiles = relationship("ProfileLanguage", back_populates="language") profiles = relationship("ProfileLanguage", back_populates="language")
def to_dict(self): def to_dict(self):
return { return {"id": self.id, "name": self.name, "icon_url": "/languages/{}/icon".format(self.id)}
"id": self.id,
"name": self.name,
"icon_url": "/languages/{}/icon".format(self.id)
}
class ProfileLanguage(db.Model): class ProfileLanguage(db.Model):
@ -190,8 +171,4 @@ class ProfileLanguage(db.Model):
language = relationship("Language", back_populates="profiles") language = relationship("Language", back_populates="profiles")
def to_dict(self): def to_dict(self):
return { return {"profile_id": self.profile_id, "language": self.language.to_dict(), "level": self.level}
"profile_id": self.profile_id,
"language": self.language.to_dict(),
"level": self.level
}

View File

@ -28,9 +28,7 @@ class TestProfileEndpoint(unittest.TestCase):
def test_update_profile(self): def test_update_profile(self):
login_data = {"username": "peter", "password": "geheim"} login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login", login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
data=json.dumps(login_data),
content_type="application/json")
self.assertEqual(login_response.status_code, 200) self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json) self.assertIn("token", login_response.json)
@ -60,15 +58,25 @@ class TestProfileEndpoint(unittest.TestCase):
"name": "Rohrpost" "name": "Rohrpost"
}, },
"content": "Ausgang 2" "content": "Ausgang 2"
}],
"skills": [{
"id": 1,
"skill": {
"id": 3,
"name": "Python"
},
"level": 4
}, {
"skill": {
"name": "Tschunkproduktion"
},
"level": 5
}] }]
} }
response = self.client.post("/users/1/profile", response = self.client.post("/users/1/profile",
data=json.dumps(data), data=json.dumps(data),
content_type="application/json", content_type="application/json",
headers={ headers={"Authorization": "Bearer " + login_response.json["token"]})
"Authorization": "Bearer " +
login_response.json["token"]
})
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
with app.app_context(): with app.app_context():
@ -100,18 +108,13 @@ class TestProfileEndpoint(unittest.TestCase):
def test_get_profile(self): def test_get_profile(self):
login_data = {"username": "peter", "password": "geheim"} login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login", login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
data=json.dumps(login_data),
content_type="application/json")
self.assertEqual(login_response.status_code, 200) self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json) self.assertIn("token", login_response.json)
response = self.client.get("/users/1/profile", response = self.client.get("/users/1/profile",
headers={ headers={"Authorization": "Bearer " + login_response.json["token"]})
"Authorization": "Bearer " +
login_response.json["token"]
})
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertDictEqual( self.assertDictEqual(
@ -151,13 +154,21 @@ class TestProfileEndpoint(unittest.TestCase):
"content": "peter@wtf-eg.de" "content": "peter@wtf-eg.de"
}], }],
"skills": [{ "skills": [{
"profile_id": 1,
"skill": {
"id": 1,
"name": "PHP",
"icon_url": "/skills/1/icon"
},
"level": 5
}, {
"profile_id": 1, "profile_id": 1,
"skill": { "skill": {
"id": 3, "id": 3,
"name": "Python", "name": "Python",
"icon_url": "/skills/3/icon" "icon_url": "/skills/3/icon"
}, },
"level": 5 "level": 3
}], }],
"languages": [{ "languages": [{
"profile_id": 1, "profile_id": 1,

View File

@ -36,6 +36,10 @@ class TestSkillsEndpoint(unittest.TestCase):
"id": 1, "id": 1,
"name": "PHP", "name": "PHP",
"icon_url": "/skills/1/icon" "icon_url": "/skills/1/icon"
}, {
"id": 10,
"name": "PostgreSQL",
"icon_url": "/skills/10/icon"
}, { }, {
"id": 3, "id": 3,
"name": "Python", "name": "Python",