#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# This is a Python dump created using dump2py.
# DJANGO_SETTINGS_MODULE was 'lino_amici.projects.amici1.settings.demo', TIME_ZONE was 'UTC'.


from __future__ import unicode_literals

import logging
logger = logging.getLogger('lino.management.commands.dump2py')

SOURCE_VERSION = '22.5.0'

import os
import six
from decimal import Decimal
from datetime import datetime
from datetime import time, date
from django.conf import settings
from django.utils.timezone import make_aware, utc
from django.core.management import call_command
# from django.contrib.contenttypes.models import ContentType
from lino.utils.dpy import create_mti_child
from lino.utils.dpy import DpyLoader
from lino.core.utils import resolve_model

if settings.USE_TZ:
    def dt(*args):
        return make_aware(datetime(*args), timezone=utc)
else:
    def dt(*args):
        return datetime(*args)

def new_content_type_id(m):
    if m is None: return m
    ct = settings.SITE.models.contenttypes.ContentType.objects.get_for_model(m)
    if ct is None: return None
    return ct.pk

def pmem():
    # Thanks to https://stackoverflow.com/questions/938733/total-memory-used-by-python-process
    process = psutil.Process(os.getpid())
    print(process.memory_info().rss)

def execfile(fn, *args):
    logger.info("Execute file %s ...", fn)
    six.exec_(compile(open(fn, "rb").read(), fn, 'exec'), *args)
    # pmem()  # requires pip install psutil


def bv2kw(fieldname, values):
    """
    Needed if `Site.languages` changed between dumpdata and loaddata
    """
    return settings.SITE.babelkw(fieldname, en=values[0],de=values[1],fr=values[2])

addresses_Address = resolve_model("addresses.Address")
blogs_Entry = resolve_model("blogs.Entry")
blogs_EntryType = resolve_model("blogs.EntryType")
cal_Calendar = resolve_model("cal.Calendar")
cal_EntryRepeater = resolve_model("cal.EntryRepeater")
cal_Event = resolve_model("cal.Event")
cal_EventPolicy = resolve_model("cal.EventPolicy")
cal_EventType = resolve_model("cal.EventType")
cal_Guest = resolve_model("cal.Guest")
cal_GuestRole = resolve_model("cal.GuestRole")
cal_RecurrentEvent = resolve_model("cal.RecurrentEvent")
cal_RemoteCalendar = resolve_model("cal.RemoteCalendar")
cal_Room = resolve_model("cal.Room")
cal_Subscription = resolve_model("cal.Subscription")
cal_Task = resolve_model("cal.Task")
calview_DailyPlannerRow = resolve_model("calview.DailyPlannerRow")
checkdata_Problem = resolve_model("checkdata.Problem")
comments_Comment = resolve_model("comments.Comment")
comments_CommentType = resolve_model("comments.CommentType")
comments_Mention = resolve_model("comments.Mention")
comments_Reaction = resolve_model("comments.Reaction")
contacts_Company = resolve_model("contacts.Company")
contacts_CompanyType = resolve_model("contacts.CompanyType")
contacts_Partner = resolve_model("contacts.Partner")
contacts_Person = resolve_model("contacts.Person")
contacts_Role = resolve_model("contacts.Role")
contacts_RoleType = resolve_model("contacts.RoleType")
contacts_UploadVCardFile = resolve_model("contacts.UploadVCardFile")
countries_Country = resolve_model("countries.Country")
countries_Place = resolve_model("countries.Place")
courses_Course = resolve_model("courses.Course")
courses_Enrolment = resolve_model("courses.Enrolment")
courses_Line = resolve_model("courses.Line")
courses_Slot = resolve_model("courses.Slot")
courses_Topic = resolve_model("courses.Topic")
dashboard_Widget = resolve_model("dashboard.Widget")
excerpts_Excerpt = resolve_model("excerpts.Excerpt")
excerpts_ExcerptType = resolve_model("excerpts.ExcerptType")
gfks_HelpText = resolve_model("gfks.HelpText")
households_Household = resolve_model("households.Household")
households_Member = resolve_model("households.Member")
households_Type = resolve_model("households.Type")
humanlinks_Link = resolve_model("humanlinks.Link")
ledger_Account = resolve_model("ledger.Account")
ledger_AccountingPeriod = resolve_model("ledger.AccountingPeriod")
ledger_FiscalYear = resolve_model("ledger.FiscalYear")
ledger_Journal = resolve_model("ledger.Journal")
ledger_LedgerInfo = resolve_model("ledger.LedgerInfo")
ledger_MatchRule = resolve_model("ledger.MatchRule")
ledger_Movement = resolve_model("ledger.Movement")
ledger_PaymentTerm = resolve_model("ledger.PaymentTerm")
ledger_Voucher = resolve_model("ledger.Voucher")
lists_List = resolve_model("lists.List")
lists_ListType = resolve_model("lists.ListType")
lists_Member = resolve_model("lists.Member")
phones_ContactDetail = resolve_model("phones.ContactDetail")
sepa_Account = resolve_model("sepa.Account")
system_SiteConfig = resolve_model("system.SiteConfig")
topics_Interest = resolve_model("topics.Interest")
topics_Topic = resolve_model("topics.Topic")
uploads_Upload = resolve_model("uploads.Upload")
uploads_UploadType = resolve_model("uploads.UploadType")
uploads_Volume = resolve_model("uploads.Volume")
users_Authority = resolve_model("users.Authority")
users_User = resolve_model("users.User")


def create_blogs_entrytype(id, name, important, remark):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(important=important)
    kw.update(remark=remark)
    return blogs_EntryType(**kw)

def create_cal_calendar(id, name, description, color):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(description=description)
    kw.update(color=color)
    return cal_Calendar(**kw)

def create_cal_eventtype(id, ref, seqno, name, attach_to_email, email_template, description, is_appointment, all_rooms, locks_user, force_guest_states, fill_presences, start_date, event_label, max_conflicting, max_days, transparent, planner_column, default_duration):
#    if planner_column: planner_column = settings.SITE.models.cal.PlannerColumns.get_by_value(planner_column)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(seqno=seqno)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(attach_to_email=attach_to_email)
    kw.update(email_template=email_template)
    kw.update(description=description)
    kw.update(is_appointment=is_appointment)
    kw.update(all_rooms=all_rooms)
    kw.update(locks_user=locks_user)
    kw.update(force_guest_states=force_guest_states)
    kw.update(fill_presences=fill_presences)
    kw.update(start_date=start_date)
    if event_label is not None: kw.update(bv2kw('event_label',event_label))
    kw.update(max_conflicting=max_conflicting)
    kw.update(max_days=max_days)
    kw.update(transparent=transparent)
    kw.update(planner_column=planner_column)
    kw.update(default_duration=default_duration)
    return cal_EventType(**kw)

def create_cal_eventpolicy(id, start_date, start_time, end_date, end_time, name, every_unit, every, positions, monday, tuesday, wednesday, thursday, friday, saturday, sunday, max_events, event_type_id):
#    if every_unit: every_unit = settings.SITE.models.cal.Recurrencies.get_by_value(every_unit)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(end_date=end_date)
    kw.update(end_time=end_time)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(every_unit=every_unit)
    kw.update(every=every)
    kw.update(positions=positions)
    kw.update(monday=monday)
    kw.update(tuesday=tuesday)
    kw.update(wednesday=wednesday)
    kw.update(thursday=thursday)
    kw.update(friday=friday)
    kw.update(saturday=saturday)
    kw.update(sunday=sunday)
    kw.update(max_events=max_events)
    kw.update(event_type_id=event_type_id)
    return cal_EventPolicy(**kw)

def create_cal_guestrole(id, ref, name):
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    if name is not None: kw.update(bv2kw('name',name))
    return cal_GuestRole(**kw)

def create_cal_remotecalendar(id, seqno, type, url_template, username, password, readonly):
    kw = dict()
    kw.update(id=id)
    kw.update(seqno=seqno)
    kw.update(type=type)
    kw.update(url_template=url_template)
    kw.update(username=username)
    kw.update(password=password)
    kw.update(readonly=readonly)
    return cal_RemoteCalendar(**kw)

def create_calview_dailyplannerrow(id, seqno, designation, start_time, end_time):
    kw = dict()
    kw.update(id=id)
    kw.update(seqno=seqno)
    if designation is not None: kw.update(bv2kw('designation',designation))
    kw.update(start_time=start_time)
    kw.update(end_time=end_time)
    return calview_DailyPlannerRow(**kw)

def create_comments_commenttype(id, name):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    return comments_CommentType(**kw)

def create_contacts_companytype(id, name, abbr):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    if abbr is not None: kw.update(bv2kw('abbr',abbr))
    return contacts_CompanyType(**kw)

def create_contacts_roletype(id, name, can_sign):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(can_sign=can_sign)
    return contacts_RoleType(**kw)

def create_contacts_uploadvcardfile(id, file, mimetype, file_size, description):
    kw = dict()
    kw.update(id=id)
    kw.update(file=file)
    kw.update(mimetype=mimetype)
    kw.update(file_size=file_size)
    kw.update(description=description)
    return contacts_UploadVCardFile(**kw)

def create_countries_country(name, isocode, short_code, iso3):
    kw = dict()
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(isocode=isocode)
    kw.update(short_code=short_code)
    kw.update(iso3=iso3)
    return countries_Country(**kw)

def create_countries_place(id, parent_id, name, country_id, zip_code, type, show_type):
#    if type: type = settings.SITE.models.countries.PlaceTypes.get_by_value(type)
    kw = dict()
    kw.update(id=id)
    kw.update(parent_id=parent_id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(country_id=country_id)
    kw.update(zip_code=zip_code)
    kw.update(type=type)
    kw.update(show_type=show_type)
    return countries_Place(**kw)

def create_courses_slot(id, seqno, start_time, end_time, name):
    kw = dict()
    kw.update(id=id)
    kw.update(seqno=seqno)
    kw.update(start_time=start_time)
    kw.update(end_time=end_time)
    kw.update(name=name)
    return courses_Slot(**kw)

def create_courses_topic(id, name):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    return courses_Topic(**kw)

def create_excerpts_excerpttype(id, name, build_method, template, attach_to_email, email_template, certifying, remark, body_template, content_type_id, primary, backward_compat, print_recipient, print_directly, shortcut):
#    if build_method: build_method = settings.SITE.models.printing.BuildMethods.get_by_value(build_method)
    content_type_id = new_content_type_id(content_type_id)
#    if shortcut: shortcut = settings.SITE.models.excerpts.Shortcuts.get_by_value(shortcut)
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(build_method=build_method)
    kw.update(template=template)
    kw.update(attach_to_email=attach_to_email)
    kw.update(email_template=email_template)
    kw.update(certifying=certifying)
    kw.update(remark=remark)
    kw.update(body_template=body_template)
    kw.update(content_type_id=content_type_id)
    kw.update(primary=primary)
    kw.update(backward_compat=backward_compat)
    kw.update(print_recipient=print_recipient)
    kw.update(print_directly=print_directly)
    kw.update(shortcut=shortcut)
    return excerpts_ExcerptType(**kw)

def create_gfks_helptext(id, content_type_id, field, help_text):
    content_type_id = new_content_type_id(content_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(content_type_id=content_type_id)
    kw.update(field=field)
    kw.update(help_text=help_text)
    return gfks_HelpText(**kw)

def create_households_type(id, name):
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    return households_Type(**kw)

def create_ledger_account(id, ref, seqno, name, common_account, needs_partner, clearable, default_amount, sales_allowed, purchases_allowed, wages_allowed, taxes_allowed, clearings_allowed, bank_po_allowed):
#    if common_account: common_account = settings.SITE.models.ledger.CommonAccounts.get_by_value(common_account)
    if default_amount is not None: default_amount = Decimal(default_amount)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(seqno=seqno)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(common_account=common_account)
    kw.update(needs_partner=needs_partner)
    kw.update(clearable=clearable)
    kw.update(default_amount=default_amount)
    kw.update(sales_allowed=sales_allowed)
    kw.update(purchases_allowed=purchases_allowed)
    kw.update(wages_allowed=wages_allowed)
    kw.update(taxes_allowed=taxes_allowed)
    kw.update(clearings_allowed=clearings_allowed)
    kw.update(bank_po_allowed=bank_po_allowed)
    return ledger_Account(**kw)

def create_ledger_fiscalyear(id, ref, start_date, end_date, state):
#    if state: state = settings.SITE.models.ledger.PeriodStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(state=state)
    return ledger_FiscalYear(**kw)

def create_ledger_accountingperiod(id, ref, start_date, end_date, state, year_id, remark):
#    if state: state = settings.SITE.models.ledger.PeriodStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(state=state)
    kw.update(year_id=year_id)
    kw.update(remark=remark)
    return ledger_AccountingPeriod(**kw)

def create_ledger_paymentterm(id, ref, name, days, months, end_of_month, printed_text):
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(days=days)
    kw.update(months=months)
    kw.update(end_of_month=end_of_month)
    if printed_text is not None: kw.update(bv2kw('printed_text',printed_text))
    return ledger_PaymentTerm(**kw)

def create_contacts_partner(id, email, language, url, phone, gsm, fax, country_id, city_id, zip_code, region_id, addr1, street_prefix, street, street_no, street_box, addr2, prefix, name, remarks, payment_term_id, purchase_account_id):
    kw = dict()
    kw.update(id=id)
    kw.update(email=email)
    kw.update(language=language)
    kw.update(url=url)
    kw.update(phone=phone)
    kw.update(gsm=gsm)
    kw.update(fax=fax)
    kw.update(country_id=country_id)
    kw.update(city_id=city_id)
    kw.update(zip_code=zip_code)
    kw.update(region_id=region_id)
    kw.update(addr1=addr1)
    kw.update(street_prefix=street_prefix)
    kw.update(street=street)
    kw.update(street_no=street_no)
    kw.update(street_box=street_box)
    kw.update(addr2=addr2)
    kw.update(prefix=prefix)
    kw.update(name=name)
    kw.update(remarks=remarks)
    kw.update(payment_term_id=payment_term_id)
    kw.update(purchase_account_id=purchase_account_id)
    return contacts_Partner(**kw)

def create_addresses_address(id, country_id, city_id, zip_code, region_id, addr1, street_prefix, street, street_no, street_box, addr2, data_source, address_type, partner_id, remark, primary):
#    if data_source: data_source = settings.SITE.models.addresses.DataSources.get_by_value(data_source)
#    if address_type: address_type = settings.SITE.models.addresses.AddressTypes.get_by_value(address_type)
    kw = dict()
    kw.update(id=id)
    kw.update(country_id=country_id)
    kw.update(city_id=city_id)
    kw.update(zip_code=zip_code)
    kw.update(region_id=region_id)
    kw.update(addr1=addr1)
    kw.update(street_prefix=street_prefix)
    kw.update(street=street)
    kw.update(street_no=street_no)
    kw.update(street_box=street_box)
    kw.update(addr2=addr2)
    kw.update(data_source=data_source)
    kw.update(address_type=address_type)
    kw.update(partner_id=partner_id)
    kw.update(remark=remark)
    kw.update(primary=primary)
    return addresses_Address(**kw)

def create_contacts_company(partner_ptr_id, parent_id, type_id):
    kw = dict()
    kw.update(parent_id=parent_id)
    kw.update(type_id=type_id)
    return create_mti_child(contacts_Partner, partner_ptr_id, contacts_Company, **kw)

def create_contacts_person(partner_ptr_id, title, first_name, middle_name, last_name, gender, birth_date):
#    if gender: gender = settings.SITE.models.system.Genders.get_by_value(gender)
    kw = dict()
    kw.update(title=title)
    kw.update(first_name=first_name)
    kw.update(middle_name=middle_name)
    kw.update(last_name=last_name)
    kw.update(gender=gender)
    kw.update(birth_date=birth_date)
    return create_mti_child(contacts_Partner, partner_ptr_id, contacts_Person, **kw)

def create_cal_room(id, name, company_id, contact_person_id, contact_role_id, display_color, description):
#    if display_color: display_color = settings.SITE.models.cal.DisplayColors.get_by_value(display_color)
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(company_id=company_id)
    kw.update(contact_person_id=contact_person_id)
    kw.update(contact_role_id=contact_role_id)
    kw.update(display_color=display_color)
    kw.update(description=description)
    return cal_Room(**kw)

def create_contacts_role(id, type_id, person_id, company_id, start_date, end_date):
    kw = dict()
    kw.update(id=id)
    kw.update(type_id=type_id)
    kw.update(person_id=person_id)
    kw.update(company_id=company_id)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    return contacts_Role(**kw)

def create_courses_line(id, ref, name, company_id, contact_person_id, contact_role_id, excerpt_title, course_area, topic_id, description, every_unit, every, event_type_id, guest_role_id, body_template):
#    if course_area: course_area = settings.SITE.models.courses.ActivityLayouts.get_by_value(course_area)
#    if every_unit: every_unit = settings.SITE.models.cal.Recurrencies.get_by_value(every_unit)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(company_id=company_id)
    kw.update(contact_person_id=contact_person_id)
    kw.update(contact_role_id=contact_role_id)
    if excerpt_title is not None: kw.update(bv2kw('excerpt_title',excerpt_title))
    kw.update(course_area=course_area)
    kw.update(topic_id=topic_id)
    if description is not None: kw.update(bv2kw('description',description))
    kw.update(every_unit=every_unit)
    kw.update(every=every)
    kw.update(event_type_id=event_type_id)
    kw.update(guest_role_id=guest_role_id)
    kw.update(body_template=body_template)
    return courses_Line(**kw)

def create_households_household(partner_ptr_id, type_id):
    kw = dict()
    kw.update(type_id=type_id)
    return create_mti_child(contacts_Partner, partner_ptr_id, households_Household, **kw)

def create_households_member(id, start_date, end_date, title, first_name, middle_name, last_name, gender, birth_date, role, person_id, household_id, dependency, primary):
#    if gender: gender = settings.SITE.models.system.Genders.get_by_value(gender)
#    if role: role = settings.SITE.models.households.MemberRoles.get_by_value(role)
#    if dependency: dependency = settings.SITE.models.households.MemberDependencies.get_by_value(dependency)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(title=title)
    kw.update(first_name=first_name)
    kw.update(middle_name=middle_name)
    kw.update(last_name=last_name)
    kw.update(gender=gender)
    kw.update(birth_date=birth_date)
    kw.update(role=role)
    kw.update(person_id=person_id)
    kw.update(household_id=household_id)
    kw.update(dependency=dependency)
    kw.update(primary=primary)
    return households_Member(**kw)

def create_humanlinks_link(id, type, parent_id, child_id):
#    if type: type = settings.SITE.models.humanlinks.LinkTypes.get_by_value(type)
    kw = dict()
    kw.update(id=id)
    kw.update(type=type)
    kw.update(parent_id=parent_id)
    kw.update(child_id=child_id)
    return humanlinks_Link(**kw)

def create_lists_listtype(id, designation):
    kw = dict()
    kw.update(id=id)
    if designation is not None: kw.update(bv2kw('designation',designation))
    return lists_ListType(**kw)

def create_lists_list(id, ref, designation, list_type_id, remarks):
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    if designation is not None: kw.update(bv2kw('designation',designation))
    kw.update(list_type_id=list_type_id)
    kw.update(remarks=remarks)
    return lists_List(**kw)

def create_lists_member(id, seqno, list_id, partner_id, remark):
    kw = dict()
    kw.update(id=id)
    kw.update(seqno=seqno)
    kw.update(list_id=list_id)
    kw.update(partner_id=partner_id)
    kw.update(remark=remark)
    return lists_Member(**kw)

def create_phones_contactdetail(id, start_date, end_date, detail_type, partner_id, value, remark, primary):
#    if detail_type: detail_type = settings.SITE.models.phones.ContactDetailTypes.get_by_value(detail_type)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(detail_type=detail_type)
    kw.update(partner_id=partner_id)
    kw.update(value=value)
    kw.update(remark=remark)
    kw.update(primary=primary)
    return phones_ContactDetail(**kw)

def create_sepa_account(id, partner_id, iban, bic, remark, primary):
    kw = dict()
    kw.update(id=id)
    kw.update(partner_id=partner_id)
    kw.update(iban=iban)
    kw.update(bic=bic)
    kw.update(remark=remark)
    kw.update(primary=primary)
    return sepa_Account(**kw)

def create_system_siteconfig(id, default_build_method, simulate_today, site_company_id, next_partner_id, default_event_type_id, site_calendar_id, max_auto_events, hide_events_before, default_color, pupil_guestrole_id):
#    if default_build_method: default_build_method = settings.SITE.models.printing.BuildMethods.get_by_value(default_build_method)
#    if default_color: default_color = settings.SITE.models.cal.DisplayColors.get_by_value(default_color)
    kw = dict()
    kw.update(id=id)
    kw.update(default_build_method=default_build_method)
    kw.update(simulate_today=simulate_today)
    kw.update(site_company_id=site_company_id)
    kw.update(next_partner_id=next_partner_id)
    kw.update(default_event_type_id=default_event_type_id)
    kw.update(site_calendar_id=site_calendar_id)
    kw.update(max_auto_events=max_auto_events)
    kw.update(hide_events_before=hide_events_before)
    kw.update(default_color=default_color)
    kw.update(pupil_guestrole_id=pupil_guestrole_id)
    return system_SiteConfig(**kw)

def create_topics_topic(id, ref, name, description_text):
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    if name is not None: kw.update(bv2kw('name',name))
    if description_text is not None: kw.update(bv2kw('description_text',description_text))
    return topics_Topic(**kw)

def create_topics_interest(id, owner_type_id, owner_id, topic_id, remark, partner_id):
    owner_type_id = new_content_type_id(owner_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(topic_id=topic_id)
    kw.update(remark=remark)
    kw.update(partner_id=partner_id)
    return topics_Interest(**kw)

def create_uploads_uploadtype(id, name, upload_area, max_number, wanted, shortcut):
#    if upload_area: upload_area = settings.SITE.models.uploads.UploadAreas.get_by_value(upload_area)
#    if shortcut: shortcut = settings.SITE.models.uploads.Shortcuts.get_by_value(shortcut)
    kw = dict()
    kw.update(id=id)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(upload_area=upload_area)
    kw.update(max_number=max_number)
    kw.update(wanted=wanted)
    kw.update(shortcut=shortcut)
    return uploads_UploadType(**kw)

def create_uploads_volume(id, ref, root_dir, base_url, description):
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(root_dir=root_dir)
    kw.update(base_url=base_url)
    kw.update(description=description)
    return uploads_Volume(**kw)

def create_ledger_journal(id, ref, seqno, name, build_method, template, trade_type, voucher_type, journal_group, auto_check_clearings, auto_fill_suggestions, force_sequence, preliminary, make_ledger_movements, account_id, partner_id, printed_name, dc, yearly_numbering, must_declare, uploads_volume_id, sepa_account_id):
#    if build_method: build_method = settings.SITE.models.printing.BuildMethods.get_by_value(build_method)
#    if trade_type: trade_type = settings.SITE.models.ledger.TradeTypes.get_by_value(trade_type)
#    if voucher_type: voucher_type = settings.SITE.models.ledger.VoucherTypes.get_by_value(voucher_type)
#    if journal_group: journal_group = settings.SITE.models.ledger.JournalGroups.get_by_value(journal_group)
#    if dc: dc = settings.SITE.models.ledger.DC.get_by_value(dc)
    kw = dict()
    kw.update(id=id)
    kw.update(ref=ref)
    kw.update(seqno=seqno)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(build_method=build_method)
    kw.update(template=template)
    kw.update(trade_type=trade_type)
    kw.update(voucher_type=voucher_type)
    kw.update(journal_group=journal_group)
    kw.update(auto_check_clearings=auto_check_clearings)
    kw.update(auto_fill_suggestions=auto_fill_suggestions)
    kw.update(force_sequence=force_sequence)
    kw.update(preliminary=preliminary)
    kw.update(make_ledger_movements=make_ledger_movements)
    kw.update(account_id=account_id)
    kw.update(partner_id=partner_id)
    if printed_name is not None: kw.update(bv2kw('printed_name',printed_name))
    kw.update(dc=dc)
    kw.update(yearly_numbering=yearly_numbering)
    kw.update(must_declare=must_declare)
    kw.update(uploads_volume_id=uploads_volume_id)
    kw.update(sepa_account_id=sepa_account_id)
    return ledger_Journal(**kw)

def create_ledger_matchrule(id, account_id, journal_id):
    kw = dict()
    kw.update(id=id)
    kw.update(account_id=account_id)
    kw.update(journal_id=journal_id)
    return ledger_MatchRule(**kw)

def create_users_user(id, email, language, modified, created, start_date, end_date, password, last_login, username, user_type, initials, first_name, last_name, remarks, partner_id, time_zone, date_format, dashboard_layout, access_class, event_type_id):
#    if user_type: user_type = settings.SITE.models.users.UserTypes.get_by_value(user_type)
#    if time_zone: time_zone = settings.SITE.models.about.TimeZones.get_by_value(time_zone)
#    if date_format: date_format = settings.SITE.models.about.DateFormats.get_by_value(date_format)
#    if dashboard_layout: dashboard_layout = settings.SITE.models.system.DashboardLayouts.get_by_value(dashboard_layout)
#    if access_class: access_class = settings.SITE.models.cal.AccessClasses.get_by_value(access_class)
    kw = dict()
    kw.update(id=id)
    kw.update(email=email)
    kw.update(language=language)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(password=password)
    kw.update(last_login=last_login)
    kw.update(username=username)
    kw.update(user_type=user_type)
    kw.update(initials=initials)
    kw.update(first_name=first_name)
    kw.update(last_name=last_name)
    kw.update(remarks=remarks)
    kw.update(partner_id=partner_id)
    kw.update(time_zone=time_zone)
    kw.update(date_format=date_format)
    kw.update(dashboard_layout=dashboard_layout)
    kw.update(access_class=access_class)
    kw.update(event_type_id=event_type_id)
    return users_User(**kw)

def create_blogs_entry(id, user_id, owner_type_id, owner_id, body, body_short_preview, body_full_preview, title, pub_date, pub_time, entry_type_id, language):
    owner_type_id = new_content_type_id(owner_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(body=body)
    kw.update(body_short_preview=body_short_preview)
    kw.update(body_full_preview=body_full_preview)
    kw.update(title=title)
    kw.update(pub_date=pub_date)
    kw.update(pub_time=pub_time)
    kw.update(entry_type_id=entry_type_id)
    kw.update(language=language)
    return blogs_Entry(**kw)

def create_cal_event(id, modified, created, start_date, start_time, end_date, end_time, build_time, build_method, user_id, assigned_to_id, owner_type_id, owner_id, summary, description, access_class, sequence, auto_type, priority, event_type_id, transparent, guests_edited, room_id, state, notify_before, notify_unit, notified):
#    if build_method: build_method = settings.SITE.models.printing.BuildMethods.get_by_value(build_method)
    owner_type_id = new_content_type_id(owner_type_id)
#    if access_class: access_class = settings.SITE.models.cal.AccessClasses.get_by_value(access_class)
#    if priority: priority = settings.SITE.models.xl.Priorities.get_by_value(priority)
#    if state: state = settings.SITE.models.cal.EntryStates.get_by_value(state)
#    if notify_unit: notify_unit = settings.SITE.models.cal.NotifyBeforeUnits.get_by_value(notify_unit)
    kw = dict()
    kw.update(id=id)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(end_date=end_date)
    kw.update(end_time=end_time)
    kw.update(build_time=build_time)
    kw.update(build_method=build_method)
    kw.update(user_id=user_id)
    kw.update(assigned_to_id=assigned_to_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(summary=summary)
    kw.update(description=description)
    kw.update(access_class=access_class)
    kw.update(sequence=sequence)
    kw.update(auto_type=auto_type)
    kw.update(priority=priority)
    kw.update(event_type_id=event_type_id)
    kw.update(transparent=transparent)
    kw.update(guests_edited=guests_edited)
    kw.update(room_id=room_id)
    kw.update(state=state)
    kw.update(notify_before=notify_before)
    kw.update(notify_unit=notify_unit)
    kw.update(notified=notified)
    return cal_Event(**kw)

def create_cal_entryrepeater(id, start_date, start_time, end_date, end_time, every_unit, every, positions, monday, tuesday, wednesday, thursday, friday, saturday, sunday, max_events, cal_entry_id):
#    if every_unit: every_unit = settings.SITE.models.cal.Recurrencies.get_by_value(every_unit)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(end_date=end_date)
    kw.update(end_time=end_time)
    kw.update(every_unit=every_unit)
    kw.update(every=every)
    kw.update(positions=positions)
    kw.update(monday=monday)
    kw.update(tuesday=tuesday)
    kw.update(wednesday=wednesday)
    kw.update(thursday=thursday)
    kw.update(friday=friday)
    kw.update(saturday=saturday)
    kw.update(sunday=sunday)
    kw.update(max_events=max_events)
    kw.update(cal_entry_id=cal_entry_id)
    return cal_EntryRepeater(**kw)

def create_cal_guest(id, event_id, partner_id, role_id, state, remark):
#    if state: state = settings.SITE.models.cal.GuestStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(event_id=event_id)
    kw.update(partner_id=partner_id)
    kw.update(role_id=role_id)
    kw.update(state=state)
    kw.update(remark=remark)
    return cal_Guest(**kw)

def create_cal_recurrentevent(id, start_date, start_time, end_date, end_time, name, user_id, every_unit, every, positions, monday, tuesday, wednesday, thursday, friday, saturday, sunday, max_events, event_type_id, description):
#    if every_unit: every_unit = settings.SITE.models.cal.Recurrencies.get_by_value(every_unit)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(end_date=end_date)
    kw.update(end_time=end_time)
    if name is not None: kw.update(bv2kw('name',name))
    kw.update(user_id=user_id)
    kw.update(every_unit=every_unit)
    kw.update(every=every)
    kw.update(positions=positions)
    kw.update(monday=monday)
    kw.update(tuesday=tuesday)
    kw.update(wednesday=wednesday)
    kw.update(thursday=thursday)
    kw.update(friday=friday)
    kw.update(saturday=saturday)
    kw.update(sunday=sunday)
    kw.update(max_events=max_events)
    kw.update(event_type_id=event_type_id)
    kw.update(description=description)
    return cal_RecurrentEvent(**kw)

def create_cal_subscription(id, user_id, calendar_id, is_hidden):
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(calendar_id=calendar_id)
    kw.update(is_hidden=is_hidden)
    return cal_Subscription(**kw)

def create_cal_task(id, modified, created, start_date, start_time, user_id, owner_type_id, owner_id, summary, description, access_class, sequence, auto_type, priority, due_date, due_time, percent, state):
    owner_type_id = new_content_type_id(owner_type_id)
#    if access_class: access_class = settings.SITE.models.cal.AccessClasses.get_by_value(access_class)
#    if priority: priority = settings.SITE.models.xl.Priorities.get_by_value(priority)
#    if state: state = settings.SITE.models.cal.TaskStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(summary=summary)
    kw.update(description=description)
    kw.update(access_class=access_class)
    kw.update(sequence=sequence)
    kw.update(auto_type=auto_type)
    kw.update(priority=priority)
    kw.update(due_date=due_date)
    kw.update(due_time=due_time)
    kw.update(percent=percent)
    kw.update(state=state)
    return cal_Task(**kw)

def create_checkdata_problem(id, user_id, owner_type_id, owner_id, checker, message):
    owner_type_id = new_content_type_id(owner_type_id)
#    if checker: checker = settings.SITE.models.checkdata.Checkers.get_by_value(checker)
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(checker=checker)
    kw.update(message=message)
    return checkdata_Problem(**kw)

def create_comments_comment(id, modified, created, user_id, owner_type_id, owner_id, body, body_short_preview, body_full_preview, reply_to_id, private, comment_type_id):
    owner_type_id = new_content_type_id(owner_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(body=body)
    kw.update(body_short_preview=body_short_preview)
    kw.update(body_full_preview=body_full_preview)
    kw.update(reply_to_id=reply_to_id)
    kw.update(private=private)
    kw.update(comment_type_id=comment_type_id)
    return comments_Comment(**kw)

def create_comments_mention(id, modified, created, user_id, owner_type_id, owner_id, comment_id):
    owner_type_id = new_content_type_id(owner_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(comment_id=comment_id)
    return comments_Mention(**kw)

def create_comments_reaction(id, modified, created, user_id, comment_id, emotion):
#    if emotion: emotion = settings.SITE.models.comments.Emotions.get_by_value(emotion)
    kw = dict()
    kw.update(id=id)
    kw.update(modified=modified)
    kw.update(created=created)
    kw.update(user_id=user_id)
    kw.update(comment_id=comment_id)
    kw.update(emotion=emotion)
    return comments_Reaction(**kw)

def create_courses_course(id, start_date, start_time, end_date, end_time, user_id, every_unit, every, positions, monday, tuesday, wednesday, thursday, friday, saturday, sunday, max_events, room_id, max_date, line_id, teacher_id, slot_id, description, remark, state, max_places, name, enrolments_until):
#    if every_unit: every_unit = settings.SITE.models.cal.Recurrencies.get_by_value(every_unit)
#    if state: state = settings.SITE.models.courses.CourseStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(start_time=start_time)
    kw.update(end_date=end_date)
    kw.update(end_time=end_time)
    kw.update(user_id=user_id)
    kw.update(every_unit=every_unit)
    kw.update(every=every)
    kw.update(positions=positions)
    kw.update(monday=monday)
    kw.update(tuesday=tuesday)
    kw.update(wednesday=wednesday)
    kw.update(thursday=thursday)
    kw.update(friday=friday)
    kw.update(saturday=saturday)
    kw.update(sunday=sunday)
    kw.update(max_events=max_events)
    kw.update(room_id=room_id)
    kw.update(max_date=max_date)
    kw.update(line_id=line_id)
    kw.update(teacher_id=teacher_id)
    kw.update(slot_id=slot_id)
    if description is not None: kw.update(bv2kw('description',description))
    kw.update(remark=remark)
    kw.update(state=state)
    kw.update(max_places=max_places)
    kw.update(name=name)
    kw.update(enrolments_until=enrolments_until)
    return courses_Course(**kw)

def create_dashboard_widget(id, seqno, user_id, item_name, visible):
    kw = dict()
    kw.update(id=id)
    kw.update(seqno=seqno)
    kw.update(user_id=user_id)
    kw.update(item_name=item_name)
    kw.update(visible=visible)
    return dashboard_Widget(**kw)

def create_excerpts_excerpt(id, build_time, build_method, user_id, owner_type_id, owner_id, company_id, contact_person_id, contact_role_id, excerpt_type_id, language):
#    if build_method: build_method = settings.SITE.models.printing.BuildMethods.get_by_value(build_method)
    owner_type_id = new_content_type_id(owner_type_id)
    kw = dict()
    kw.update(id=id)
    kw.update(build_time=build_time)
    kw.update(build_method=build_method)
    kw.update(user_id=user_id)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(company_id=company_id)
    kw.update(contact_person_id=contact_person_id)
    kw.update(contact_role_id=contact_role_id)
    kw.update(excerpt_type_id=excerpt_type_id)
    kw.update(language=language)
    return excerpts_Excerpt(**kw)

def create_courses_enrolment(id, start_date, end_date, user_id, printed_by_id, course_area, course_id, pupil_id, request_date, state, places, remark, confirmation_details):
#    if course_area: course_area = settings.SITE.models.courses.ActivityLayouts.get_by_value(course_area)
#    if state: state = settings.SITE.models.courses.EnrolmentStates.get_by_value(state)
    kw = dict()
    kw.update(id=id)
    kw.update(start_date=start_date)
    kw.update(end_date=end_date)
    kw.update(user_id=user_id)
    kw.update(printed_by_id=printed_by_id)
    kw.update(course_area=course_area)
    kw.update(course_id=course_id)
    kw.update(pupil_id=pupil_id)
    kw.update(request_date=request_date)
    kw.update(state=state)
    kw.update(places=places)
    kw.update(remark=remark)
    kw.update(confirmation_details=confirmation_details)
    return courses_Enrolment(**kw)

def create_ledger_ledgerinfo(user_id, entry_date):
    kw = dict()
    kw.update(user_id=user_id)
    kw.update(entry_date=entry_date)
    return ledger_LedgerInfo(**kw)

def create_ledger_voucher(id, user_id, journal_id, entry_date, voucher_date, accounting_period_id, number, narration):
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(journal_id=journal_id)
    kw.update(entry_date=entry_date)
    kw.update(voucher_date=voucher_date)
    kw.update(accounting_period_id=accounting_period_id)
    kw.update(number=number)
    kw.update(narration=narration)
    return ledger_Voucher(**kw)

def create_ledger_movement(id, voucher_id, partner_id, seqno, account_id, amount, match, cleared, value_date):
    if amount is not None: amount = Decimal(amount)
    kw = dict()
    kw.update(id=id)
    kw.update(voucher_id=voucher_id)
    kw.update(partner_id=partner_id)
    kw.update(seqno=seqno)
    kw.update(account_id=account_id)
    kw.update(amount=amount)
    kw.update(match=match)
    kw.update(cleared=cleared)
    kw.update(value_date=value_date)
    return ledger_Movement(**kw)

def create_uploads_upload(id, user_id, file, mimetype, file_size, owner_type_id, owner_id, upload_area, type_id, volume_id, library_file, description):
    owner_type_id = new_content_type_id(owner_type_id)
#    if upload_area: upload_area = settings.SITE.models.uploads.UploadAreas.get_by_value(upload_area)
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(file=file)
    kw.update(mimetype=mimetype)
    kw.update(file_size=file_size)
    kw.update(owner_type_id=owner_type_id)
    kw.update(owner_id=owner_id)
    kw.update(upload_area=upload_area)
    kw.update(type_id=type_id)
    kw.update(volume_id=volume_id)
    kw.update(library_file=library_file)
    kw.update(description=description)
    return uploads_Upload(**kw)

def create_users_authority(id, user_id, authorized_id):
    kw = dict()
    kw.update(id=id)
    kw.update(user_id=user_id)
    kw.update(authorized_id=authorized_id)
    return users_Authority(**kw)




def main(args):
    loader = DpyLoader(globals(), quick=args.quick)
    from django.core.management import call_command
    call_command('initdb', interactive=args.interactive)
    os.chdir(os.path.dirname(__file__))
    loader.initialize()
    args = (globals(), locals())

    execfile("blogs_entrytype.py", *args)
    execfile("cal_calendar.py", *args)
    execfile("cal_eventtype.py", *args)
    execfile("cal_eventpolicy.py", *args)
    execfile("cal_guestrole.py", *args)
    execfile("cal_remotecalendar.py", *args)
    execfile("calview_dailyplannerrow.py", *args)
    execfile("comments_commenttype.py", *args)
    execfile("contacts_companytype.py", *args)
    execfile("contacts_roletype.py", *args)
    execfile("contacts_uploadvcardfile.py", *args)
    execfile("countries_country.py", *args)
    execfile("countries_place.py", *args)
    execfile("courses_slot.py", *args)
    execfile("courses_topic.py", *args)
    execfile("excerpts_excerpttype.py", *args)
    execfile("gfks_helptext.py", *args)
    execfile("households_type.py", *args)
    execfile("ledger_account.py", *args)
    execfile("ledger_fiscalyear.py", *args)
    execfile("ledger_accountingperiod.py", *args)
    execfile("ledger_paymentterm.py", *args)
    execfile("contacts_partner.py", *args)
    execfile("addresses_address.py", *args)
    execfile("contacts_company.py", *args)
    execfile("contacts_person.py", *args)
    execfile("cal_room.py", *args)
    execfile("contacts_role.py", *args)
    execfile("courses_line.py", *args)
    execfile("households_household.py", *args)
    execfile("households_member.py", *args)
    execfile("humanlinks_link.py", *args)
    execfile("lists_listtype.py", *args)
    execfile("lists_list.py", *args)
    execfile("lists_member.py", *args)
    execfile("phones_contactdetail.py", *args)
    execfile("sepa_account.py", *args)
    execfile("system_siteconfig.py", *args)
    execfile("topics_topic.py", *args)
    execfile("topics_interest.py", *args)
    execfile("uploads_uploadtype.py", *args)
    execfile("uploads_volume.py", *args)
    execfile("ledger_journal.py", *args)
    execfile("ledger_matchrule.py", *args)
    execfile("users_user.py", *args)
    execfile("blogs_entry.py", *args)
    execfile("cal_event.py", *args)
    execfile("cal_entryrepeater.py", *args)
    execfile("cal_guest.py", *args)
    execfile("cal_recurrentevent.py", *args)
    execfile("cal_subscription.py", *args)
    execfile("cal_task.py", *args)
    execfile("checkdata_problem.py", *args)
    execfile("comments_comment.py", *args)
    execfile("comments_mention.py", *args)
    execfile("comments_reaction.py", *args)
    execfile("courses_course.py", *args)
    execfile("dashboard_widget.py", *args)
    execfile("excerpts_excerpt.py", *args)
    execfile("courses_enrolment.py", *args)
    execfile("ledger_ledgerinfo.py", *args)
    execfile("ledger_voucher.py", *args)
    execfile("ledger_movement.py", *args)
    execfile("uploads_upload.py", *args)
    execfile("users_authority.py", *args)
    loader.finalize()
    logger.info("Loaded %d objects", loader.count_objects)
    call_command('resetsequences')

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Restore the data.')
    parser.add_argument('--noinput', dest='interactive',
        action='store_false', default=True,
        help="Don't ask for confirmation before flushing the database.")
    parser.add_argument('--quick', dest='quick',
        action='store_true',default=False,
        help='Do not call full_clean() on restored instances.')

    args = parser.parse_args()
    main(args)
