Add RDF models export classes — untested

Also add a dependency to https://github.com/tobast/RDFSerializer/
This commit is contained in:
Théophile Bastian 2018-02-23 13:32:32 +01:00
parent 0e02f22d08
commit 2732e4115f
3 changed files with 129 additions and 0 deletions

1
.gitignore vendored
View file

@ -65,3 +65,4 @@ venv/
# Django stuff
db.sqlite3
_vimrc_local.vim

126
profiles/models_rdf.py Normal file
View file

@ -0,0 +1,126 @@
""" RDF serialization class for profile models """
import rdfserializer as rdf
from rdfserializer import RDFModelSerialiser as RDFModelSerializer
# ^ This was hurting my eyes way too much
from rdfserializer import SCHEMA as schema
import models as profile_models
class RdfWebpage(RDFModelSerializer):
""" RDF serializer for Webpage """
_type = schema.WebPage
model = profile_models.Webpage
entries = [
rdf.RDFSimpleField(schema.url, 'url'),
]
class RdfWebsite(RDFModelSerializer):
""" RDF serializer for Website """
_type = schema.WebSite
model = profile_models.Website
entries = [
rdf.RDFSimpleField(schema.name, 'name'),
rdf.RDFSimpleField(schema.url, 'url'),
rdf.RDFManyField(schema.keywords, 'keywords',
lambda keyword: keyword.text),
rdf.RDFManyLinker(schema.hasPart, 'notable_pages', RdfWebpage),
]
class RdfPlace(RDFModelSerializer):
""" RDF serializer for Place """
_type = schema.Place
model = profile_models.Place
entries = [
rdf.RDFSimpleField(schema.name, 'name'),
rdf.RDFSimpleField(schema.address, 'address'),
rdf.RDFSimpleField(schema.latitude, 'lat'),
rdf.RDFSimpleField(schema.longitude, 'lon'),
]
class RdfEvent(RDFModelSerializer):
""" RDF serializer for Event """
_type = schema.Event
model = profile_models.Event
entries = [
rdf.RDFSimpleField(schema.name, 'name'),
rdf.RDFSimpleField(schema.startDate, 'date'),
rdf.RDFLeftBinder(schema.location, 'place', RdfPlace),
]
class RdfBrowserFingerprint(RDFModelSerializer):
""" RDF serializer for BrowserFingerprint """
_type = schema.Intangible
model = profile_models.BrowserFingerprint
entries = [
rdf.RDFSimpleField(schema.description, 'description'),
rdf.RDFSimpleField('useragent', 'useragent'),
rdf.RDFSimpleField('appname', 'appname'),
rdf.RDFSimpleField('appversion', 'appversion'),
rdf.RDFSimpleField('platform', 'platform'),
rdf.RDFSimpleField('vendor', 'vendor'),
rdf.RDFSimpleField('vendorsub', 'vendorsub'),
rdf.RDFSimpleField('buildID', 'buildID'),
rdf.RDFSimpleField('oscpu', 'oscpu'),
rdf.RDFSimpleField('accept_encoding', 'accept_encoding'),
rdf.RDFSimpleField('accept_default', 'accept_default'),
rdf.RDFSimpleField('accept_lang', 'accept_lang'),
rdf.RDFSimpleField('pixeldepth', 'pixeldepth'),
rdf.RDFSimpleField('colordepth', 'colordepth'),
rdf.RDFSimpleField('screens', 'screens'),
]
class RdfSearchEngine(RDFModelSerializer):
""" RDF serializer for SearchEngine """
_type = schema.WebSite
model = profile_models.SearchEngine
entries = [
rdf.RDFSimpleField(schema.url, 'url'),
rdf.RDFSimpleField(schema.name, 'name'),
rdf.RDFSimpleField('query_pattern', 'query_pattern'),
]
class RdfInterest(RDFModelSerializer):
""" RDF serializer for Interest """
Interesttype = 'interest'
model = profile_models.Interest
entries = [
rdf.RDFSimpleField(schema.name, 'name'),
rdf.RDFManyField(schema.keywords, 'keywords',
lambda keyword: keyword.text),
rdf.RDFManyLinker(schema.location, 'places', RdfPlace),
rdf.RDFManyLinker(schema.website, 'websites', RdfWebsite),
rdf.RDFManyLinker(schema.event, 'events', RdfEvent),
]
class RdfProfile(RDFModelSerializer):
""" RDF serializer for Profile """
_type = schema.Person
model = profile_models.Profile
entries = [
rdf.RDFSimpleField('nickname', 'nick'),
rdf.RDFSimpleField(schema.given_name, 'first_name'),
rdf.RDFSimpleField(schema.family_name, 'last_name'),
rdf.RDFSimpleField(schema.email, 'email'),
rdf.RDFSimpleField('uses_urls', 'uses_urls'),
rdf.RDFManyLinker('interest', 'interests', RdfInterest),
rdf.RDFLeftBinder('search_engine', 'search_engine', RdfSearchEngine),
rdf.RDFLeftBinder('browser_fingerprint', 'browser_fingerprint',
RdfBrowserFingerprint)
]

View file

@ -12,3 +12,5 @@ pycares==2.3.0
pytz==2017.3
yarl==1.1.1
beautifulsoup4==4.6.0
rdflib==4.2.2
git+https://github.com/tobast/RDFSerializer.git