donar first model
This commit is contained in:
parent
5332a4ac4d
commit
81db0b2381
0
ofu_app/apps/donar/management/commands/__init__.py
Normal file
0
ofu_app/apps/donar/management/commands/__init__.py
Normal file
14
ofu_app/apps/donar/management/commands/import_rooms.py
Normal file
14
ofu_app/apps/donar/management/commands/import_rooms.py
Normal file
@ -0,0 +1,14 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from apps.donar.models import Room
|
||||
from apps.donar.utils import migrate_data
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Imports Rooms from Univis PRG"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
pass
|
||||
|
||||
def handle(self, *args, **options):
|
||||
migrate_data.main()
|
||||
self.stdout.write(self.style.SUCCESS('Successfully migrate data'))
|
||||
29
ofu_app/apps/donar/migrations/0001_initial.py
Normal file
29
ofu_app/apps/donar/migrations/0001_initial.py
Normal file
@ -0,0 +1,29 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.2 on 2017-10-03 22:01
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Room',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('key', models.CharField(max_length=60)),
|
||||
('address', models.CharField(max_length=60)),
|
||||
('building_key', models.CharField(max_length=60)),
|
||||
('floor', models.CharField(max_length=60)),
|
||||
('name', models.CharField(max_length=60)),
|
||||
('orgname', models.CharField(max_length=60)),
|
||||
('short', models.CharField(max_length=60)),
|
||||
],
|
||||
),
|
||||
]
|
||||
20
ofu_app/apps/donar/migrations/0002_auto_20171004_0016.py
Normal file
20
ofu_app/apps/donar/migrations/0002_auto_20171004_0016.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.2 on 2017-10-03 22:16
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('donar', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='room',
|
||||
name='short',
|
||||
field=models.CharField(max_length=60, unique=True),
|
||||
),
|
||||
]
|
||||
@ -12,4 +12,4 @@ class Room(models.Model):
|
||||
floor = models.CharField(max_length=MAX_LENGTH)
|
||||
name = models.CharField(max_length=MAX_LENGTH)
|
||||
orgname = models.CharField(max_length=MAX_LENGTH)
|
||||
short = models.CharField(max_length=MAX_LENGTH)
|
||||
short = models.CharField(unique=True, max_length=MAX_LENGTH)
|
||||
|
||||
22
ofu_app/apps/donar/urls.py
Normal file
22
ofu_app/apps/donar/urls.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""ofu_app URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/1.11/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.conf.urls import url, include
|
||||
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf.urls import url
|
||||
|
||||
from apps.donar import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', views.home, name='donar-main'),
|
||||
]
|
||||
63
ofu_app/apps/donar/utils/migrate_data.py
Normal file
63
ofu_app/apps/donar/utils/migrate_data.py
Normal file
@ -0,0 +1,63 @@
|
||||
import json
|
||||
from pprint import pprint
|
||||
from django.db.utils import IntegrityError
|
||||
|
||||
from apps.donar.models import Room
|
||||
from apps.donar.utils.parser import univis_rooms_parser
|
||||
|
||||
# CONFIG
|
||||
UNIVIS_RPG_GuK = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Geistes-%20und%20Kulturwissenschaften&show=xml"
|
||||
UNIVIS_RPG_SoWi = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Sozial-%20und%20Wirtschaftswissenschaften&show=xml"
|
||||
UNIVIS_RPG_HuWi = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Humanwissenschaften&show=xml"
|
||||
UNIVIS_RPG_WIAI = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Wirtschaftsinformatik&show=xml"
|
||||
|
||||
|
||||
def getJsonFromFile(path):
|
||||
with open(path, "r") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
def writeFekideDataInDB(data):
|
||||
|
||||
for room in data:
|
||||
try:
|
||||
key = ""
|
||||
address = ""
|
||||
building_key = ""
|
||||
floor = ""
|
||||
name = ""
|
||||
orgname = ""
|
||||
short = ""
|
||||
if '@key' in room:
|
||||
key = room['@key']
|
||||
|
||||
if 'address' in room:
|
||||
address = room['address']
|
||||
|
||||
if 'buildingkey' in room:
|
||||
building_key = room['buildingkey']
|
||||
if 'floor' in room:
|
||||
floor = room['floor']
|
||||
if 'name' in room:
|
||||
name = room['name']
|
||||
if 'short' in room:
|
||||
short = room['short']
|
||||
|
||||
Room.objects.create(key=key, address=address, building_key=building_key, floor=floor, name=name,
|
||||
orgname=orgname, short=short)
|
||||
except IntegrityError:
|
||||
# ignored
|
||||
break
|
||||
|
||||
|
||||
def main():
|
||||
# get food jsons
|
||||
writeFekideDataInDB(univis_rooms_parser.parsePage(UNIVIS_RPG_GuK))
|
||||
writeFekideDataInDB(univis_rooms_parser.parsePage(UNIVIS_RPG_SoWi))
|
||||
writeFekideDataInDB(univis_rooms_parser.parsePage(UNIVIS_RPG_HuWi))
|
||||
writeFekideDataInDB(univis_rooms_parser.parsePage(UNIVIS_RPG_WIAI))
|
||||
pprint("Room: " + str(Room.objects.count()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
35
ofu_app/apps/donar/utils/parser/univis_rooms_parser.py
Normal file
35
ofu_app/apps/donar/utils/parser/univis_rooms_parser.py
Normal file
@ -0,0 +1,35 @@
|
||||
import requests
|
||||
import datetime
|
||||
import xmltodict
|
||||
import json
|
||||
from pprint import pprint
|
||||
|
||||
# CONFIG
|
||||
UNIVIS_RPG_GuK = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Geistes-%20und%20Kulturwissenschaften&show=xml"
|
||||
UNIVIS_RPG_SoWi = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Sozial-%20und%20Wirtschaftswissenschaften&show=xml"
|
||||
UNIVIS_RPG_HuWi = "http://www.config.de/cgi-bin/prg-wizard.pl"
|
||||
UNIVIS_RPG_WIAI = "http://univis.uni-bamberg.de/prg?search=rooms&department=Fakult%E4t%20Wirtschaftsinformatik&show=xml"
|
||||
|
||||
|
||||
def loadPage(url: str):
|
||||
return requests.get(url).content
|
||||
|
||||
|
||||
def getDay():
|
||||
return datetime.datetime.today().strftime("%A, %d.%m.%Y")
|
||||
|
||||
|
||||
def getRoom(dict: dict):
|
||||
rooms = []
|
||||
for room in dict['UnivIS']['Room']:
|
||||
rooms.append(room)
|
||||
return rooms
|
||||
|
||||
|
||||
def parsePage(url):
|
||||
page = loadPage(url)
|
||||
dict = xmltodict.parse(page)
|
||||
rooms = getRoom(dict)
|
||||
return rooms
|
||||
|
||||
# parsePage()
|
||||
@ -1,3 +1,9 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
from apps.donar.models import Room
|
||||
|
||||
|
||||
# Create your views here.
|
||||
def home(request):
|
||||
rooms = Room.objects.all()
|
||||
return render(request, 'donar/home.jinja', {'rooms': rooms})
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
||||
'django_jinja',
|
||||
'apps.food',
|
||||
'apps.events',
|
||||
'apps.donar',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
||||
Binary file not shown.
@ -24,4 +24,5 @@ urlpatterns = [
|
||||
# -- Apps --
|
||||
url(r'^food/', include('apps.food.urls')),
|
||||
url(r'^events/', include('apps.events.urls')),
|
||||
url(r'^donar/', include('apps.donar.urls')),
|
||||
]
|
||||
|
||||
18
ofu_app/templates/donar/home.jinja
Normal file
18
ofu_app/templates/donar/home.jinja
Normal file
@ -0,0 +1,18 @@
|
||||
{% extends 'base.jinja' %}
|
||||
{% block headline %}
|
||||
<h1 class="text-center">Alle Räume an der OFU </h1>
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
{% for room in rooms %}
|
||||
<div class="col-6 p-3">
|
||||
<div class="p-3 border border-dark roundep-3 border border-dark rounded bg-light text-dark">
|
||||
<h3>{{ room.name }}</h3>
|
||||
<p>Short: {{ room.short }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user