Compare commits

..

5 Commits

Author SHA1 Message Date
5113f6995e
implement languages update
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-27 13:51:40 +02:00
15b71459ee
implement updating skills 2021-06-27 13:38:16 +02:00
68b84f50ca
implement contacts update 2021-06-27 13:07:54 +02:00
54a6686474
implement address update 2021-06-27 12:20:36 +02:00
101bc20923
extract update profile handler handler 2021-06-27 11:55:08 +02:00
9 changed files with 272 additions and 78 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,11 +50,15 @@ 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")
db.session.add(email_contact)
peters_address = Address(name="Peter Nichtlustig",
street="Waldweg",
house_number="23i",
@ -65,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()

1
ki/handlers/__init__.py Normal file
View File

@ -0,0 +1 @@
from ki.handlers.update_profile import update_profile # noqa

View File

@ -0,0 +1,118 @@
from flask import make_response, request
from sqlalchemy import not_
from ki.models import Address, Contact, ContactType, Language, User, Profile, ProfileLanguage, ProfileSkill, Skill
from app import db
def update_address(profile, address_data):
address = profile.address
if (address is None):
address = Address(profile=profile)
db.session.add(address)
address.name = address_data.get("name", "")
address.street = address_data.get("street", "")
address.house_number = address_data.get("house_number", "")
address.additional = address_data.get("additional", "")
address.postcode = address_data.get("postcode", "")
address.city = address_data.get("city", "")
address.country = address_data.get("country", "")
def update_languages(profile, languages_data):
profile_language_ids = []
for language_data in languages_data:
language_id = language_data["language"]["id"]
language = Language.query.get(language_id)
profile_language = ProfileLanguage.query.filter(ProfileLanguage.profile == profile,
ProfileLanguage.language_id == language_id).first()
if profile_language is None:
profile_language = ProfileLanguage(profile=profile, language=language)
db.session.add(profile_language)
profile_language.level = language_data["level"]
profile_language_ids.append(language_id)
ProfileLanguage.query.filter(ProfileLanguage.profile == profile,
not_(ProfileLanguage.language_id.in_(profile_language_ids))).delete()
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_name = contact_data["contacttype"]["name"]
contacttype = ContactType.query.filter(ContactType.name == contacttype_name).first()
if (contacttype is None):
contacttype = ContactType(name=contacttype_name)
db.session.add(contacttype)
if "id" in contact_data:
contact_id = int(contact_data["id"])
contact_ids_to_be_deleted.remove(contact_id)
contact = Contact.query.get(contact_id)
else:
contact = Contact(profile=profile, contacttype=contacttype)
db.session.add(contact)
contact.contacttype_id = contacttype.id
contact.content = contact_data["content"]
Contact.query.filter(Contact.id.in_(contact_ids_to_be_deleted)).delete()
def update_profile(user_id: int):
user = User.query.get(user_id)
if user is None:
return make_response({}, 404)
profile = user.profile
if (profile is None):
profile = Profile(user=user, nickname=user.auth_id)
db.session.add(profile)
profile.pronouns = request.json.get("pronouns", "")
profile.volunteerwork = request.json.get("volunteerwork", "")
profile.freetext = request.json.get("freetext", "")
update_address(profile, request.json.get("address", {}))
update_contacts(profile, request.json.get("contacts", {}))
update_skills(profile, request.json.get("skills", {}))
update_languages(profile, request.json.get("languages", {}))
db.session.commit()
return make_response({"profile": profile.to_dict()})

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

@ -3,8 +3,9 @@ from flask import g, make_response, request, send_file
from functools import wraps
from ki.auth import auth
from ki.models import Language, Skill, Token, User, Profile
from app import app, db
from ki.handlers import update_profile as update_profile_handler
from ki.models import Language, Skill, Token, User
from app import app
def token_auth(func):
@ -121,25 +122,9 @@ def get_user_profile(user_id):
@app.route("/users/<user_id>/profile", methods=["POST"])
@token_auth
def update_profile(user_id):
user = User.query.filter(User.id == int(user_id)).first()
if user is None:
return make_response({}, 404)
profile = user.profile
if (profile is None):
profile = Profile(user=user, nickname=user.auth_id)
db.session.add(profile)
profile.pronouns = request.json.get("pronouns", "")
profile.volunteerwork = request.json.get("volunteerwork", "")
profile.freetext = request.json.get("freetext", "")
db.session.commit()
return make_response(profile.to_dict(), 200)
return update_profile_handler(int(user_id))
@app.route("/skills")

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)
@ -39,14 +37,60 @@ class TestProfileEndpoint(unittest.TestCase):
"pronouns": "Monsieur",
"volunteerwork": "ja",
"freetext": "Hallo",
"address": {
"name": "Peeeda",
"street": "Bachstraße",
"house_number": "42x",
"additional": "oben",
"postcode": "23232",
"city": "Travemünde",
"country": "Deutschland"
},
"contacts": [{
"id": 1,
"contacttype": {
"id": 1,
"name": "Matrix"
},
"content": "@peeda:wtf-eg.de"
}, {
"contacttype": {
"name": "Rohrpost"
},
"content": "Ausgang 2"
}],
"skills": [{
"id": 1,
"skill": {
"id": 3,
"name": "Python"
},
"level": 4
}, {
"skill": {
"name": "Tschunkproduktion"
},
"level": 5
}],
"languages": [{
"id": 1,
"language": {
"id": "de",
"name": "Deutsch"
},
"level": 4
}, {
"language": {
"id": "es",
"name": "Spanisch"
},
"level": 2
}]
}
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():
@ -56,20 +100,59 @@ class TestProfileEndpoint(unittest.TestCase):
self.assertEqual("ja", profile.volunteerwork)
self.assertEqual("Hallo", profile.freetext)
address = profile.address
self.assertEqual(address.name, "Peeeda")
self.assertEqual(address.street, "Bachstraße")
self.assertEqual(address.house_number, "42x")
self.assertEqual(address.additional, "oben")
self.assertEqual(address.postcode, "23232")
self.assertEqual(address.city, "Travemünde")
self.assertEqual(address.country, "Deutschland")
contacts = profile.contacts
self.assertEqual(len(contacts), 2)
first_contact = contacts[0]
self.assertEqual(first_contact.contacttype.name, "Matrix")
self.assertEqual(first_contact.content, "@peeda:wtf-eg.de")
second_contact = contacts[1]
self.assertEqual(second_contact.contacttype.name, "Rohrpost")
self.assertEqual(second_contact.content, "Ausgang 2")
skills = profile.skills
self.assertEqual(len(skills), 2)
first_skill = skills[0]
self.assertEqual(first_skill.skill.id, 3)
self.assertEqual(first_skill.skill.name, "Python")
self.assertEqual(first_skill.level, 4)
second_skill = skills[1]
self.assertEqual(second_skill.skill.id, 13)
self.assertEqual(second_skill.skill.name, "Tschunkproduktion")
self.assertEqual(second_skill.level, 5)
languages = profile.languages
self.assertEqual(len(languages), 2)
first_language = languages[0]
self.assertEqual(first_language.language_id, "de")
self.assertEqual(first_language.level, 4)
second_language = languages[1]
self.assertEqual(second_language.language_id, "es")
self.assertEqual(second_language.level, 2)
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(
@ -99,15 +182,31 @@ class TestProfileEndpoint(unittest.TestCase):
"name": "Matrix"
},
"content": "@peter:wtf-eg.de"
}, {
"id": 2,
"profile_id": 1,
"contacttype": {
"id": 2,
"name": "E-Mail"
},
"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",