Compare commits
No commits in common. "d10706e3017fd6664076ec34a16765e2a4442d9d" and "09669cf3694ef6c077e8b4c53ae1a39962aec62e" have entirely different histories.
d10706e301
...
09669cf369
@ -1,3 +0,0 @@
|
||||
[style]
|
||||
based_on_style = pep8
|
||||
allow_split_before_dict_value = 0
|
@ -23,7 +23,7 @@ cp env.dev .env
|
||||
pipenv install --dev
|
||||
pipenv shell
|
||||
flask db upgrade
|
||||
flask seed --dev
|
||||
flask seed
|
||||
flask run
|
||||
```
|
||||
|
||||
|
@ -1 +0,0 @@
|
||||
from ki.actions.seed import seed # noqa
|
@ -1,83 +0,0 @@
|
||||
import csv
|
||||
import logging
|
||||
|
||||
from app import app, db
|
||||
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSkill, User
|
||||
|
||||
|
||||
def seed(dev: bool):
|
||||
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
|
||||
|
||||
logging.info("importing skills")
|
||||
|
||||
with open(skill_seed_file_path) as skills_file:
|
||||
skills_csv_reader = csv.DictReader(skills_file)
|
||||
|
||||
for skill in skills_csv_reader:
|
||||
id = int(skill["id"])
|
||||
db_skill = Skill.query.get(id)
|
||||
|
||||
if db_skill is None:
|
||||
db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))
|
||||
|
||||
logging.info("importing languages")
|
||||
|
||||
iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"
|
||||
|
||||
with open(iso_seed_file_path) as iso_file:
|
||||
iso_csv_reader = csv.DictReader(iso_file)
|
||||
|
||||
for iso in iso_csv_reader:
|
||||
id = iso["639-1"]
|
||||
db_language = Language.query.get(id)
|
||||
|
||||
if db_language is None:
|
||||
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
|
||||
|
||||
if dev:
|
||||
logging.info("seeding peter :)")
|
||||
|
||||
peter = User(auth_id="peter")
|
||||
db.session.add(peter)
|
||||
|
||||
peters_profile = Profile(nickname="peternichtlustig",
|
||||
pronouns="Herr Dr. Dr.",
|
||||
volunteerwork="Gartenverein",
|
||||
freetext="Ich mag Kaffee",
|
||||
user=peter)
|
||||
db.session.add(peters_profile)
|
||||
|
||||
matrix_type = ContactType(name="Matrix")
|
||||
db.session.add(matrix_type)
|
||||
|
||||
matrix_contact = Contact(profile=peters_profile,
|
||||
contacttype=matrix_type,
|
||||
content="@peter:wtf-eg.de")
|
||||
db.session.add(matrix_contact)
|
||||
|
||||
peters_address = Address(name="Peter Nichtlustig",
|
||||
street="Waldweg",
|
||||
house_number="23i",
|
||||
additional="Hinterhaus",
|
||||
postcode="13337",
|
||||
city="Bielefeld",
|
||||
country="Deutschland",
|
||||
profile=peters_profile)
|
||||
db.session.add(peters_address)
|
||||
|
||||
peters_python_skill = ProfileSkill(profile=peters_profile,
|
||||
skill_id=3,
|
||||
level=5)
|
||||
db.session.add(peters_python_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)
|
||||
db.session.add(peter_fr)
|
||||
|
||||
db.session.commit()
|
@ -1,10 +1,39 @@
|
||||
import click
|
||||
import csv
|
||||
|
||||
from app import app
|
||||
from ki.actions import seed
|
||||
from ki.models import Language, Skill
|
||||
from app import app, db
|
||||
|
||||
|
||||
def seed():
|
||||
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
|
||||
|
||||
print("importing skills")
|
||||
|
||||
with open(skill_seed_file_path) as skills_file:
|
||||
skills_csv_reader = csv.DictReader(skills_file)
|
||||
|
||||
for skill in skills_csv_reader:
|
||||
id = int(skill["id"])
|
||||
db_skill = Skill.query.get(id)
|
||||
|
||||
if db_skill is None:
|
||||
db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))
|
||||
|
||||
iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"
|
||||
|
||||
with open(iso_seed_file_path) as iso_file:
|
||||
iso_csv_reader = csv.DictReader(iso_file)
|
||||
|
||||
for iso in iso_csv_reader:
|
||||
id = iso["639-1"]
|
||||
db_language = Language.query.get(id)
|
||||
|
||||
if db_language is None:
|
||||
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@app.cli.command("seed")
|
||||
@click.option("--dev", is_flag=True)
|
||||
def seed_command(dev):
|
||||
seed(dev)
|
||||
def seed_command():
|
||||
seed()
|
||||
|
62
ki/models.py
62
ki/models.py
@ -42,17 +42,10 @@ class Profile(db.Model):
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"user_id": self.user.id,
|
||||
"nickname": self.nickname,
|
||||
"pronouns": self.pronouns,
|
||||
"volunteerwork": self.volunteerwork,
|
||||
"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))
|
||||
"freetext": self.freetext
|
||||
}
|
||||
|
||||
|
||||
@ -65,9 +58,6 @@ class Token(db.Model):
|
||||
|
||||
user = relationship("User", back_populates="tokens")
|
||||
|
||||
def to_dict(self):
|
||||
return {"user_id": self.user_id, "token": self.token}
|
||||
|
||||
|
||||
class Contact(db.Model):
|
||||
__tablename__ = "contact"
|
||||
@ -81,14 +71,6 @@ class Contact(db.Model):
|
||||
contacttype = relationship("ContactType")
|
||||
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):
|
||||
__tablename__ = "contacttype"
|
||||
@ -96,9 +78,6 @@ class ContactType(db.Model):
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String(25), nullable=False)
|
||||
|
||||
def to_dict(self):
|
||||
return {"id": self.id, "name": self.name}
|
||||
|
||||
|
||||
class Address(db.Model):
|
||||
__tablename__ = "address"
|
||||
@ -115,19 +94,6 @@ class Address(db.Model):
|
||||
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
|
||||
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):
|
||||
__tablename__ = "skill"
|
||||
@ -138,11 +104,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}
|
||||
|
||||
|
||||
class ProfileSkill(db.Model):
|
||||
@ -155,13 +117,6 @@ class ProfileSkill(db.Model):
|
||||
profile = relationship("Profile", back_populates="skills")
|
||||
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):
|
||||
__tablename__ = "language"
|
||||
@ -172,11 +127,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}
|
||||
|
||||
|
||||
class ProfileLanguage(db.Model):
|
||||
@ -188,10 +139,3 @@ class ProfileLanguage(db.Model):
|
||||
|
||||
profile = relationship("Profile", back_populates="languages")
|
||||
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,9 +115,7 @@ def get_user_profile(user_id):
|
||||
if profile is None:
|
||||
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"])
|
||||
|
@ -2,8 +2,8 @@ from alembic import command
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from app import app, db, migrate
|
||||
from ki.actions import seed
|
||||
from app import app, migrate
|
||||
from ki.commands import seed
|
||||
|
||||
|
||||
class TestLoginEndpoint(unittest.TestCase):
|
||||
@ -16,11 +16,7 @@ class TestLoginEndpoint(unittest.TestCase):
|
||||
config = migrate.get_config()
|
||||
command.upgrade(config, "head")
|
||||
|
||||
seed(True)
|
||||
|
||||
def tearDown(self):
|
||||
db.drop_all()
|
||||
db.engine.dispose()
|
||||
seed()
|
||||
|
||||
def test_login(self):
|
||||
response1_data = self.login()
|
||||
|
@ -3,13 +3,11 @@ import unittest
|
||||
import json
|
||||
|
||||
from app import app, db, migrate
|
||||
from ki.actions import seed
|
||||
from ki.models import User
|
||||
from ki.commands import seed
|
||||
from ki.models import Profile, User
|
||||
|
||||
|
||||
class TestProfileEndpoint(unittest.TestCase):
|
||||
maxDiff = None
|
||||
|
||||
def setUp(self):
|
||||
app.debug = True
|
||||
app.config["TESTING"] = True
|
||||
@ -17,16 +15,22 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
self.client = app.test_client()
|
||||
|
||||
with app.app_context():
|
||||
db.drop_all()
|
||||
db.engine.dispose()
|
||||
config = migrate.get_config()
|
||||
command.upgrade(config, "head")
|
||||
|
||||
seed(True)
|
||||
seed()
|
||||
|
||||
def tearDown(self):
|
||||
db.drop_all()
|
||||
db.engine.dispose()
|
||||
|
||||
def test_update_profile(self):
|
||||
def test_create_profile(self):
|
||||
user = User(auth_id="peter")
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
login_data = {"username": "peter", "password": "geheim"}
|
||||
login_response = self.client.post("/users/login",
|
||||
data=json.dumps(login_data),
|
||||
@ -36,7 +40,7 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
self.assertIn("token", login_response.json)
|
||||
|
||||
data = {
|
||||
"pronouns": "Monsieur",
|
||||
"pronouns": "Herr Dr. Dr.",
|
||||
"volunteerwork": "ja",
|
||||
"freetext": "Hallo",
|
||||
}
|
||||
@ -44,7 +48,8 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
data=json.dumps(data),
|
||||
content_type="application/json",
|
||||
headers={
|
||||
"Authorization": "Bearer " +
|
||||
"Authorization":
|
||||
"Bearer " +
|
||||
login_response.json["token"]
|
||||
})
|
||||
|
||||
@ -52,11 +57,20 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
with app.app_context():
|
||||
user = User.query.filter(User.id == 1).first()
|
||||
profile = user.profile
|
||||
self.assertEqual("Monsieur", profile.pronouns)
|
||||
self.assertEqual("Herr Dr. Dr.", profile.pronouns)
|
||||
self.assertEqual("ja", profile.volunteerwork)
|
||||
self.assertEqual("Hallo", profile.freetext)
|
||||
|
||||
def test_get_profile(self):
|
||||
user = User(auth_id="peter")
|
||||
db.session.add(user)
|
||||
|
||||
profile = Profile(user=user)
|
||||
profile.nickname = "Popeter"
|
||||
db.session.add(profile)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
login_data = {"username": "peter", "password": "geheim"}
|
||||
login_response = self.client.post("/users/login",
|
||||
data=json.dumps(login_data),
|
||||
@ -67,65 +81,18 @@ class TestProfileEndpoint(unittest.TestCase):
|
||||
|
||||
response = self.client.get("/users/1/profile",
|
||||
headers={
|
||||
"Authorization": "Bearer " +
|
||||
login_response.json["token"]
|
||||
"Authorization":
|
||||
"Bearer " + login_response.json["token"]
|
||||
})
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertDictEqual(
|
||||
self.assertEqual(
|
||||
response.json, {
|
||||
"profile": {
|
||||
"user_id": 1,
|
||||
"nickname": "peternichtlustig",
|
||||
"pronouns": "Herr Dr. Dr.",
|
||||
"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
|
||||
}]
|
||||
"freetext": "",
|
||||
"nickname": "Popeter",
|
||||
"pronouns": "",
|
||||
"volunteerwork": ""
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
from alembic import command
|
||||
import unittest
|
||||
|
||||
from app import app, db, migrate
|
||||
from ki.actions import seed
|
||||
from app import app, migrate
|
||||
from ki.commands import seed
|
||||
|
||||
|
||||
class TestSkillsEndpoint(unittest.TestCase):
|
||||
@ -15,11 +15,7 @@ class TestSkillsEndpoint(unittest.TestCase):
|
||||
config = migrate.get_config()
|
||||
command.upgrade(config, "head")
|
||||
|
||||
seed(True)
|
||||
|
||||
def tearDown(self):
|
||||
db.drop_all()
|
||||
db.engine.dispose()
|
||||
seed()
|
||||
|
||||
def test_skills_options(self):
|
||||
response = self.client.options("/skills")
|
||||
@ -34,12 +30,10 @@ class TestSkillsEndpoint(unittest.TestCase):
|
||||
{
|
||||
"skills": [{
|
||||
"id": 1,
|
||||
"name": "PHP",
|
||||
"icon_url": "/skills/1/icon"
|
||||
"name": "PHP"
|
||||
}, {
|
||||
"id": 3,
|
||||
"name": "Python",
|
||||
"icon_url": "/skills/3/icon"
|
||||
"name": "Python"
|
||||
}]
|
||||
}, response.json)
|
||||
self.assertIn("Access-Control-Allow-Origin", response.headers)
|
||||
|
Loading…
Reference in New Issue
Block a user