implement updating skills

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

View File

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

View File

@ -3,3 +3,11 @@ id,name
2,Vue.js
3,Python
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")
db.session.add(matrix_type)
matrix_contact = Contact(profile=peters_profile,
contacttype=matrix_type,
content="@peter:wtf-eg.de")
matrix_contact = Contact(profile=peters_profile, contacttype=matrix_type, content="@peter:wtf-eg.de")
db.session.add(matrix_contact)
email_type = ContactType(name="E-Mail")
db.session.add(email_type)
email_contact = Contact(profile=peters_profile,
contacttype=email_type,
content="peter@wtf-eg.de")
email_contact = Contact(profile=peters_profile, contacttype=email_type, content="peter@wtf-eg.de")
db.session.add(email_contact)
peters_address = Address(name="Peter Nichtlustig",
@ -73,19 +69,16 @@ def seed(dev: bool):
profile=peters_profile)
db.session.add(peters_address)
peters_python_skill = ProfileSkill(profile=peters_profile,
skill_id=3,
level=5)
peters_python_skill = ProfileSkill(profile=peters_profile, skill_id=3, level=3)
db.session.add(peters_python_skill)
peter_de = ProfileLanguage(profile=peters_profile,
language_id="de",
level=5)
peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5)
db.session.add(peters_php_skill)
peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5)
db.session.add(peter_de)
peter_fr = ProfileLanguage(profile=peters_profile,
language_id="fr",
level=3)
peter_fr = ProfileLanguage(profile=peters_profile, language_id="fr", level=3)
db.session.add(peter_fr)
db.session.commit()

View File

@ -1,6 +1,7 @@
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
@ -20,15 +21,39 @@ def update_address(profile, address_data):
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):
contact_ids_to_be_deleted = list(map(lambda c: c.id, profile.contacts))
for contact_data in contacts_data:
contacttype = ContactType.query.filter(
ContactType.name == contact_data["contacttype"]["name"]).first()
contacttype_name = contact_data["contacttype"]["name"]
contacttype = ContactType.query.filter(ContactType.name == contacttype_name).first()
if (contacttype is None):
contacttype = ContactType(name=contact_data["contacttype"]["name"])
contacttype = ContactType(name=contacttype_name)
db.session.add(contacttype)
if "id" in contact_data:
@ -63,6 +88,7 @@ def update_profile(user_id: int):
update_address(profile, request.json.get("address", {}))
update_contacts(profile, request.json.get("contacts", {}))
update_skills(profile, request.json.get("skills", {}))
db.session.commit()

View File

@ -29,10 +29,7 @@ class Profile(db.Model):
volunteerwork = Column(String(4000), default="")
freetext = Column(String(4000), default="")
created = Column(DateTime, nullable=False, default=datetime.now)
updated = Column(DateTime,
onupdate=datetime.now,
nullable=False,
default=datetime.now)
updated = Column(DateTime, onupdate=datetime.now, nullable=False, default=datetime.now)
user = relationship("User", back_populates="profile", uselist=False)
contacts = relationship("Contact")
@ -48,11 +45,9 @@ class Profile(db.Model):
"volunteerwork": self.volunteerwork,
"freetext": self.freetext,
"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)),
"languages": list(
map(lambda language: language.to_dict(), self.languages))
"languages": list(map(lambda language: language.to_dict(), self.languages))
}
@ -75,9 +70,7 @@ class Contact(db.Model):
id = Column(Integer, primary_key=True)
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
profile = relationship("Profile", back_populates="contacts")
contacttype_id = Column(Integer,
ForeignKey("contacttype.id"),
nullable=False)
contacttype_id = Column(Integer, ForeignKey("contacttype.id"), nullable=False)
contacttype = relationship("ContactType")
content = Column(String(200), nullable=False)
@ -138,11 +131,7 @@ class Skill(db.Model):
profiles = relationship("ProfileSkill", back_populates="skill")
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)}
class ProfileSkill(db.Model):
@ -156,11 +145,7 @@ class ProfileSkill(db.Model):
skill = relationship("Skill", back_populates="profiles")
def to_dict(self):
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 Language(db.Model):
@ -172,11 +157,7 @@ class Language(db.Model):
profiles = relationship("ProfileLanguage", back_populates="language")
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"icon_url": "/languages/{}/icon".format(self.id)
}
return {"id": self.id, "name": self.name, "icon_url": "/languages/{}/icon".format(self.id)}
class ProfileLanguage(db.Model):
@ -190,8 +171,4 @@ class ProfileLanguage(db.Model):
language = relationship("Language", back_populates="profiles")
def to_dict(self):
return {
"profile_id": self.profile_id,
"language": self.language.to_dict(),
"level": self.level
}
return {"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):
login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login",
data=json.dumps(login_data),
content_type="application/json")
login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json)
@ -60,15 +58,25 @@ class TestProfileEndpoint(unittest.TestCase):
"name": "Rohrpost"
},
"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",
data=json.dumps(data),
content_type="application/json",
headers={
"Authorization": "Bearer " +
login_response.json["token"]
})
headers={"Authorization": "Bearer " + login_response.json["token"]})
self.assertEqual(response.status_code, 200)
with app.app_context():
@ -100,18 +108,13 @@ class TestProfileEndpoint(unittest.TestCase):
def test_get_profile(self):
login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login",
data=json.dumps(login_data),
content_type="application/json")
login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json)
response = self.client.get("/users/1/profile",
headers={
"Authorization": "Bearer " +
login_response.json["token"]
})
headers={"Authorization": "Bearer " + login_response.json["token"]})
self.assertEqual(response.status_code, 200)
self.assertDictEqual(
@ -151,13 +154,21 @@ class TestProfileEndpoint(unittest.TestCase):
"content": "peter@wtf-eg.de"
}],
"skills": [{
"profile_id": 1,
"skill": {
"id": 1,
"name": "PHP",
"icon_url": "/skills/1/icon"
},
"level": 5
}, {
"profile_id": 1,
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
},
"level": 5
"level": 3
}],
"languages": [{
"profile_id": 1,

View File

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