Komplettes Profil zurückgeben #19
62
ki/models.py
62
ki/models.py
@ -42,10 +42,17 @@ class Profile(db.Model):
|
|||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
|
"user_id": self.user.id,
|
||||||
"nickname": self.nickname,
|
"nickname": self.nickname,
|
||||||
"pronouns": self.pronouns,
|
"pronouns": self.pronouns,
|
||||||
"volunteerwork": self.volunteerwork,
|
"volunteerwork": self.volunteerwork,
|
||||||
"freetext": self.freetext
|
"freetext": self.freetext,
|
||||||
|
"address": self.address.to_dict(),
|
||||||
|
"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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -58,6 +65,9 @@ class Token(db.Model):
|
|||||||
|
|
||||||
user = relationship("User", back_populates="tokens")
|
user = relationship("User", back_populates="tokens")
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {"user_id": self.user_id, "token": self.token}
|
||||||
|
|
||||||
|
|
||||||
class Contact(db.Model):
|
class Contact(db.Model):
|
||||||
__tablename__ = "contact"
|
__tablename__ = "contact"
|
||||||
@ -71,6 +81,14 @@ class Contact(db.Model):
|
|||||||
contacttype = relationship("ContactType")
|
contacttype = relationship("ContactType")
|
||||||
content = Column(String(200), nullable=False)
|
content = Column(String(200), nullable=False)
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"profile_id": self.profile_id,
|
||||||
|
"contacttype": self.contacttype.to_dict(),
|
||||||
|
"content": self.content
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ContactType(db.Model):
|
class ContactType(db.Model):
|
||||||
__tablename__ = "contacttype"
|
__tablename__ = "contacttype"
|
||||||
@ -78,6 +96,9 @@ class ContactType(db.Model):
|
|||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
name = Column(String(25), nullable=False)
|
name = Column(String(25), nullable=False)
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {"id": self.id, "name": self.name}
|
||||||
|
|
||||||
|
|
||||||
class Address(db.Model):
|
class Address(db.Model):
|
||||||
__tablename__ = "address"
|
__tablename__ = "address"
|
||||||
@ -94,6 +115,19 @@ class Address(db.Model):
|
|||||||
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
|
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
|
||||||
profile = relationship("Profile", back_populates="address")
|
profile = relationship("Profile", back_populates="address")
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"street": self.street,
|
||||||
|
"house_number": self.house_number,
|
||||||
|
"additional": self.additional,
|
||||||
|
"postcode": self.postcode,
|
||||||
|
"city": self.city,
|
||||||
|
"country": self.country,
|
||||||
|
"profile_id": self.profile_id
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Skill(db.Model):
|
class Skill(db.Model):
|
||||||
__tablename__ = "skill"
|
__tablename__ = "skill"
|
||||||
@ -104,7 +138,11 @@ 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 {"id": self.id, "name": self.name}
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"icon_url": "/skills/{}/icon".format(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ProfileSkill(db.Model):
|
class ProfileSkill(db.Model):
|
||||||
@ -117,6 +155,13 @@ class ProfileSkill(db.Model):
|
|||||||
profile = relationship("Profile", back_populates="skills")
|
profile = relationship("Profile", back_populates="skills")
|
||||||
skill = relationship("Skill", back_populates="profiles")
|
skill = relationship("Skill", back_populates="profiles")
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"profile_id": self.profile_id,
|
||||||
|
"skill": self.skill.to_dict(),
|
||||||
|
"level": self.level
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Language(db.Model):
|
class Language(db.Model):
|
||||||
__tablename__ = "language"
|
__tablename__ = "language"
|
||||||
@ -127,7 +172,11 @@ 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 {"id": self.id, "name": self.name}
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"icon_url": "/languages/{}/icon".format(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ProfileLanguage(db.Model):
|
class ProfileLanguage(db.Model):
|
||||||
@ -139,3 +188,10 @@ class ProfileLanguage(db.Model):
|
|||||||
|
|
||||||
profile = relationship("Profile", back_populates="languages")
|
profile = relationship("Profile", back_populates="languages")
|
||||||
language = relationship("Language", back_populates="profiles")
|
language = relationship("Language", back_populates="profiles")
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"profile_id": self.profile_id,
|
||||||
|
"language": self.language.to_dict(),
|
||||||
|
"level": self.level
|
||||||
|
}
|
||||||
|
@ -115,7 +115,9 @@ def get_user_profile(user_id):
|
|||||||
if profile is None:
|
if profile is None:
|
||||||
return make_response({}, 404)
|
return make_response({}, 404)
|
||||||
|
|
||||||
return make_response({"profile": profile.to_dict()})
|
return make_response({
|
||||||
|
"profile": profile.to_dict(),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@app.route("/users/<user_id>/profile", methods=["POST"])
|
@app.route("/users/<user_id>/profile", methods=["POST"])
|
||||||
|
@ -8,6 +8,8 @@ from ki.models import User
|
|||||||
|
|
||||||
|
|
||||||
class TestProfileEndpoint(unittest.TestCase):
|
class TestProfileEndpoint(unittest.TestCase):
|
||||||
|
maxDiff = None
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
app.debug = True
|
app.debug = True
|
||||||
app.config["TESTING"] = True
|
app.config["TESTING"] = True
|
||||||
@ -24,7 +26,7 @@ class TestProfileEndpoint(unittest.TestCase):
|
|||||||
db.drop_all()
|
db.drop_all()
|
||||||
db.engine.dispose()
|
db.engine.dispose()
|
||||||
|
|
||||||
def test_create_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),
|
data=json.dumps(login_data),
|
||||||
@ -34,7 +36,7 @@ class TestProfileEndpoint(unittest.TestCase):
|
|||||||
self.assertIn("token", login_response.json)
|
self.assertIn("token", login_response.json)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"pronouns": "Herr Dr. Dr.",
|
"pronouns": "Monsieur",
|
||||||
"volunteerwork": "ja",
|
"volunteerwork": "ja",
|
||||||
"freetext": "Hallo",
|
"freetext": "Hallo",
|
||||||
}
|
}
|
||||||
@ -42,8 +44,7 @@ class TestProfileEndpoint(unittest.TestCase):
|
|||||||
data=json.dumps(data),
|
data=json.dumps(data),
|
||||||
content_type="application/json",
|
content_type="application/json",
|
||||||
headers={
|
headers={
|
||||||
"Authorization":
|
"Authorization": "Bearer " +
|
||||||
"Bearer " +
|
|
||||||
login_response.json["token"]
|
login_response.json["token"]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ class TestProfileEndpoint(unittest.TestCase):
|
|||||||
with app.app_context():
|
with app.app_context():
|
||||||
user = User.query.filter(User.id == 1).first()
|
user = User.query.filter(User.id == 1).first()
|
||||||
profile = user.profile
|
profile = user.profile
|
||||||
self.assertEqual("Herr Dr. Dr.", profile.pronouns)
|
self.assertEqual("Monsieur", profile.pronouns)
|
||||||
self.assertEqual("ja", profile.volunteerwork)
|
self.assertEqual("ja", profile.volunteerwork)
|
||||||
self.assertEqual("Hallo", profile.freetext)
|
self.assertEqual("Hallo", profile.freetext)
|
||||||
|
|
||||||
@ -66,18 +67,65 @@ class TestProfileEndpoint(unittest.TestCase):
|
|||||||
|
|
||||||
response = self.client.get("/users/1/profile",
|
response = self.client.get("/users/1/profile",
|
||||||
headers={
|
headers={
|
||||||
"Authorization":
|
"Authorization": "Bearer " +
|
||||||
"Bearer " + login_response.json["token"]
|
login_response.json["token"]
|
||||||
})
|
})
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(
|
self.assertDictEqual(
|
||||||
response.json, {
|
response.json, {
|
||||||
"profile": {
|
"profile": {
|
||||||
|
|||||||
"freetext": "Ich mag Kaffee",
|
"user_id": 1,
|
||||||
"nickname": "peternichtlustig",
|
"nickname": "peternichtlustig",
|
||||||
"pronouns": "Herr Dr. Dr.",
|
"pronouns": "Herr Dr. Dr.",
|
||||||
"volunteerwork": "Gartenverein"
|
"freetext": "Ich mag Kaffee",
|
||||||
|
"volunteerwork": "Gartenverein",
|
||||||
|
"address": {
|
||||||
|
"additional": "Hinterhaus",
|
||||||
|
"city": "Bielefeld",
|
||||||
|
"country": "Deutschland",
|
||||||
|
"house_number": "23i",
|
||||||
|
"id": 1,
|
||||||
|
"name": "Peter Nichtlustig",
|
||||||
|
"postcode": "13337",
|
||||||
|
"profile_id": 1,
|
||||||
|
"street": "Waldweg"
|
||||||
|
},
|
||||||
|
"contacts": [{
|
||||||
|
"id": 1,
|
||||||
|
"profile_id": 1,
|
||||||
|
"contacttype": {
|
||||||
|
"id": 1,
|
||||||
|
"name": "Matrix"
|
||||||
|
},
|
||||||
|
"content": "@peter:wtf-eg.de"
|
||||||
|
}],
|
||||||
|
"skills": [{
|
||||||
|
"profile_id": 1,
|
||||||
|
"skill": {
|
||||||
|
"id": 3,
|
||||||
|
"name": "Python",
|
||||||
|
"icon_url": "/skills/3/icon"
|
||||||
|
},
|
||||||
|
"level": 5
|
||||||
|
}],
|
||||||
|
"languages": [{
|
||||||
|
"profile_id": 1,
|
||||||
|
"language": {
|
||||||
|
"id": "de",
|
||||||
|
"name": "Deutsch",
|
||||||
|
"icon_url": "/languages/de/icon"
|
||||||
|
},
|
||||||
|
"level": 5
|
||||||
|
}, {
|
||||||
|
"profile_id": 1,
|
||||||
|
"language": {
|
||||||
|
"id": "fr",
|
||||||
|
"name": "Französisch",
|
||||||
|
"icon_url": "/languages/fr/icon"
|
||||||
|
},
|
||||||
|
"level": 3
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -34,10 +34,12 @@ class TestSkillsEndpoint(unittest.TestCase):
|
|||||||
{
|
{
|
||||||
"skills": [{
|
"skills": [{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"name": "PHP"
|
"name": "PHP",
|
||||||
|
"icon_url": "/skills/1/icon"
|
||||||
}, {
|
}, {
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"name": "Python"
|
"name": "Python",
|
||||||
|
"icon_url": "/skills/3/icon"
|
||||||
}]
|
}]
|
||||||
}, response.json)
|
}, response.json)
|
||||||
self.assertIn("Access-Control-Allow-Origin", response.headers)
|
self.assertIn("Access-Control-Allow-Origin", response.headers)
|
||||||
|
Loading…
Reference in New Issue
Block a user
@scammo das ist ein Beispiel für eine vollständige Profil-Response, wie sie dann über die API rausgeht.