Commit 08fd406b authored by Daniel Schreiber's avatar Daniel Schreiber

move csv handling to seperate module

so we can add a view to upload csv via http
parent b4656dfd
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import date, timedelta, datetime
from django.conf import settings
from django.core.management import BaseCommand
from django.db import transaction
from django.utils import timezone
from api.models import Resource, UserType
from api.utils.queue_utils import dispatch_deletion_sync
from api.utils.csv_handling import sync_from_csv_data
class Command(BaseCommand):
......@@ -22,37 +17,5 @@ class Command(BaseCommand):
def handle(self, *args, **options):
path = options["csv"]
date_format = options["date"]
eppn_dict = {}
with open(path, "r") as csv:
for line in csv.readlines():
line.strip()
eppn, d = line.strip().split(",")
exp_date = timezone.make_aware(datetime.strptime(d, date_format))
eppn_dict[eppn] = exp_date
# iterate over all local resources
for res in Resource.objects.filter(realm_idp=settings.SERVER_REALM)\
.select_related("sp", "sp__localresourcepolicy"):
try:
exp_date = eppn_dict[res.eppn]
except KeyError:
# eppn does no longer exist, we need to make sure that
# the resource will be locked immediately
# deletion and purging happens as usual
exp_date = min(res.expiry_date, timezone.now() - timedelta(days=1))
delete_date = exp_date + timedelta(days=res.sp.delete_after_days)
purge_date = delete_date + timedelta(days=res.sp.purge_api_data_after_days)
if purge_date < timezone.now():
uuid = res.uuid
is_local_sp = res.sp.is_local
res.delete()
# if this resource belongs to a remote sp, we need to notify
# the sp that we deleted the resource record
if not is_local_sp:
dispatch_deletion_sync(res, uuid)
else:
if res.expiry_date != exp_date or res.deletion_date != delete_date:
res.update_attribute([
{"name": "expiry_date", "value": exp_date.isoformat()},
{"name": "deletion_date", "value": delete_date.isoformat()},
], user_type=UserType.TYPE_IDP)
sync_from_csv_data(csv, date_format)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
from datetime import timedelta, datetime
from django.conf import settings
from django.db import transaction
from django.utils import timezone
from api.models import Resource, UserType
from api.utils.queue_utils import dispatch_deletion_sync
@transaction.atomic()
def sync_from_csv_data(file_obj, date_format):
eppn_dict = {}
for line in file_obj:
line.strip()
eppn, d = line.strip().split(",")
exp_date = timezone.make_aware(datetime.strptime(d, date_format))
eppn_dict[eppn] = exp_date
# iterate over all local resources
for res in Resource.objects.filter(realm_idp=settings.SERVER_REALM)\
.select_related("sp", "sp__localresourcepolicy"):
try:
exp_date = eppn_dict[res.eppn]
except KeyError:
# eppn does no longer exist, we need to make sure that
# the resource will be locked immediately
# deletion and purging happens as usual
exp_date = min(res.expiry_date, timezone.now() - timedelta(days=1))
delete_date = exp_date + timedelta(days=res.sp.delete_after_days)
purge_date = delete_date + timedelta(days=res.sp.purge_api_data_after_days)
if purge_date < timezone.now():
uuid = res.uuid
is_local_sp = res.sp.is_local
res.delete()
# if this resource belongs to a remote sp, we need to notify
# the sp that we deleted the resource record
if not is_local_sp:
dispatch_deletion_sync(res, uuid)
else:
if res.expiry_date != exp_date or res.deletion_date != delete_date:
res.update_attribute([
{"name": "expiry_date", "value": exp_date.isoformat()},
{"name": "deletion_date", "value": delete_date.isoformat()},
], user_type=UserType.TYPE_IDP)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment