add contacttypes seed and endpoint

This commit is contained in:
weeman 2021-07-02 16:49:14 +02:00
parent 4ca8660b1d
commit f589ed063e
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
5 changed files with 80 additions and 12 deletions

View File

@ -0,0 +1,10 @@
id,name
1,E-Mail
2,Mobiltelefon
3,Festnetz
4,Matrix
5,Mastodon
6,Telegram
7,Jabber/XMPP
8,Web
9,Twitter
1 id name
2 1 E-Mail
3 2 Mobiltelefon
4 3 Festnetz
5 4 Matrix
6 5 Mastodon
7 6 Telegram
8 7 Jabber/XMPP
9 8 Web
10 9 Twitter

View File

@ -5,7 +5,25 @@ from app import app, db
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSkill, User
def seed_contacttypes():
contacttypes_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/contacttypes.csv"
logging.info("importing contacttypes")
with open(contacttypes_seed_file_path) as file:
csv_reader = csv.DictReader(file)
for contacttype in csv_reader:
id = int(contacttype["id"])
db_contacttype = ContactType.query.get(id)
if db_contacttype is None:
db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"]))
def seed(dev: bool):
seed_contacttypes()
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
logging.info("importing skills")
@ -47,16 +65,10 @@ def seed(dev: bool):
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")
matrix_contact = Contact(profile=peters_profile, contacttype_id=4, 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_id=1, content="peter@wtf-eg.de")
db.session.add(email_contact)
peters_address = Address(name="Peter Nichtlustig",

View File

@ -4,7 +4,7 @@ from functools import wraps
from ki.auth import auth
from ki.handlers import update_profile as update_profile_handler
from ki.models import Language, Skill, Token, User
from ki.models import ContactType, Language, Skill, Token, User
from app import app
@ -133,6 +133,12 @@ def update_profile(user_id):
return update_profile_handler(int(user_id))
@app.route("/contacttypes")
@token_auth
def get_contacttypes():
return handle_completion_request(ContactType, "contacttypes")
@app.route("/skills")
@token_auth
def get_skills():

View File

@ -0,0 +1,40 @@
import unittest
from ki.test.ApiTest import ApiTest
class TestContactTypesEndpoint(ApiTest):
def test_skills_options(self):
response = self.client.options("/contacttypes")
self.assertEqual(response.status_code, 200)
self.assertIn("Access-Control-Allow-Origin", response.headers)
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*")
def test_get_contacttypes_unauthorised(self):
response = self.client.get("/contacttypes?search=m")
self.assertEqual(response.status_code, 401)
def test_get_contacttypes(self):
token = self.login("peter", "geheim")["token"]
response = self.client.get("/contacttypes?search=m", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertEqual(
{
"contacttypes": [{
"id": 5,
"name": "Mastodon"
}, {
"id": 4,
"name": "Matrix"
}, {
"id": 2,
"name": "Mobiltelefon"
}]
}, response.json)
self.assertIn("Access-Control-Allow-Origin", response.headers)
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*")
if __name__ == "main":
unittest.main()

View File

@ -43,7 +43,7 @@ class TestProfileEndpoint(ApiTest):
"contacts": [{
"id": 1,
"contacttype": {
"id": 1,
"id": 4,
"name": "Matrix"
},
"content": "@peeda:wtf-eg.de"
@ -179,7 +179,7 @@ class TestProfileEndpoint(ApiTest):
"id": 1,
"profile_id": 1,
"contacttype": {
"id": 1,
"id": 4,
"name": "Matrix"
},
"content": "@peter:wtf-eg.de"
@ -187,7 +187,7 @@ class TestProfileEndpoint(ApiTest):
"id": 2,
"profile_id": 1,
"contacttype": {
"id": 2,
"id": 1,
"name": "E-Mail"
},
"content": "peter@wtf-eg.de"