Skip to content

Commit

Permalink
set up db model for grade import formats and implement import format …
Browse files Browse the repository at this point in the history
…resource insertion on db init
  • Loading branch information
urvdp committed Nov 13, 2024
1 parent 3a8f973 commit 5788fa5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/spz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ class Language(db.Model):

signup_constraint = db.CheckConstraint(signup_end > signup_begin)

import_format_id = db.Column(db.Integer, db.ForeignKey('import_format.id'), nullable=True)
import_format = db.relationship("ImportFormat", back_populates="languages")


def __init__(self, name, reply_to, signup_begin, signup_rnd_window_end, signup_manual_end, signup_end,
signup_auto_end, name_english=None):
self.name = name
Expand Down Expand Up @@ -1303,7 +1307,44 @@ def get_user(self):
return User.query.get(self.user_id)

@property
def uploat_at_utc(self):
def upload_at_utc(self):
target_timezone = pytz.timezone("Europe/Berlin")
return self.upload_at.astimezone(target_timezone).strftime("%d.%m.%Y %H:%M")


class ImportFormat(db.Model):
"""Format used when importing grade course lists
:param id: unique ID
:param name: human readable name for the format
:param grade_column: defines the xls column in which the grade is stored and from which is read from
:param languages: list of associated languages for which the import format is intended (NULL for any)
"""

__tablename__ = 'import_format'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
grade_column = db.Column(db.String(10), nullable=False)

# Define a one-to-many relationship with Language
# (one import format can be used for multiple languages, but each language only has one import format)
languages = db.relationship("Language", back_populates="import_format")

def __init__(self, name, grade_column, languages=None):
if languages is None:
languages = []
self.name = name
self.grade_column = grade_column
self.languages = languages

def __repr__(self):
return '<ImportFormat "{}">'.format(self.descriptive_name)

def __lt__(self, other):
return self.name.lower() < other.name.lower()

@property
def descriptive_name(self):
return self.name

14 changes: 14 additions & 0 deletions src/spz/resource/import_formats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"formats": [
{
"name": "Spanisch Hueber",
"grade_column": "M",
"languages": ["Spanisch"]
},
{
"name": "Englisch",
"grade_column": "S",
"languages": ["Englisch", "Englisch Intensiv"]
}
]
}
38 changes: 38 additions & 0 deletions src/spz/resource/import_formats.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "",
"type": "object",
"properties": {
"formats": {
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"required": [
"name",
"grade_column"
],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"grade_column": {
"type": "string",
"minLength": 1
},
"languages": {
"type": "array",
"items": {
"type": "string",
"properties": {}
}
}
}
}
}
},
"required": [
"formats"
]
}
22 changes: 21 additions & 1 deletion src/spz/setup/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def validate_resources():
resources = ('degrees', 'origins', 'courses', 'degrees', 'graduations', 'users', 'export_formats')
resources = ('degrees', 'origins', 'courses', 'degrees', 'graduations', 'users', 'export_formats', 'import_formats')

for fname in resources:
with app.open_resource('resource/{0}.json'.format(fname)) as fd_json, \
Expand All @@ -38,6 +38,7 @@ def insert_resources():
insert_origins('resource/origins.json')
insert_courses('resource/courses.json')
insert_export_formats('resource/export_formats.json')
insert_import_formats('resource/import_formats.json')
insert_users('resource/users.json')
db.session.commit()

Expand Down Expand Up @@ -114,6 +115,25 @@ def insert_export_formats(json_file):
db.session.add(ExportFormat(**format, language=lang_ref))


def insert_import_formats(json_file):
with app.open_resource(json_file) as fd:
res = json.load(fd)

for format in res["formats"]:
lang_refs = []
if 'languages' not in format:
print(" WARNING: no languages specified for import format {}".format(format["name"]))
continue
for lang in format.pop('languages'):
lang_ref = Language.query.filter(Language.name == lang).first()
if lang_ref:
lang_refs.append(lang_ref)
else:
print(" WARNING: language {} does not exist".format(lang))

db.session.add(ImportFormat(**format, languages=lang_refs))


def insert_users(json_file):
with app.open_resource(json_file) as fd:
res = json.load(fd)
Expand Down
2 changes: 1 addition & 1 deletion src/spz/templates/internal/administration/grade.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ <h2>Hochgeladene Notenliste(n)</h2>
</a>
</td>
{% endif %}
<td>{{ file.uploat_at_utc }}</td>
<td>{{ file.upload_at_utc }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit 5788fa5

Please sign in to comment.