Exemplo n.º 1
0
def export(request, year):
    year = int(year)
    # import localy so that openpyxl is only required if needed.
    e = ExelYearView(year)
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response[
        'Content-Disposition'] = "attachment; filename=regnskap%d.xlsx" % year
    response.write(e.getExcelFileStream())
    return response
Exemplo n.º 2
0
    def handle(self, *args, **options):
        # Update database_dump.sql
        self.dump_db()
        # Update excel workbooks for this year and last year
        if settings.REGNSKAP_FIRST_YEAR == date.today().year:
            excel_years = [date.today().year]
        else:
            excel_years = [date.today().year - 1, date.today().year]
        for year in excel_years:
            e = ExelYearView(year)
            e.save(self.excel_filename(year))

        # iterate over all users
        user_objects = get_user_model().objects
        users = [
            user_objects.get_by_natural_key(opt) for opt in options['username']
        ]
        for user in users:
            client = get_client(user)
            for year in range(settings.REGNSKAP_FIRST_YEAR,
                              date.today().year + 1):
                # Copy bilag files to Dropbox
                db_folder = "/regnskap/%d" % year
                server_folder = os.path.join(settings.MEDIA_ROOT, str(year))
                try:
                    files = client.metadata(db_folder)['contents']
                except ErrorResponse as e:
                    if e.status == 404:
                        client.file_create_folder(db_folder)
                        files = client.metadata(db_folder)['contents']
                    else:
                        raise e
                files = [f["path"].lower() for f in files]
                for f in os.listdir(server_folder):
                    path = db_folder + "/" + f
                    if path.lower() not in files:
                        with open(os.path.join(server_folder, f)) as handle:
                            client.put_file(db_folder + "/" + f, handle)
            # Copy db backup
            with open("/home/ivarne/media/db-backup.sql") as handle:
                db_client_update_file(client, "/regnskap/db-backup.sql",
                                      handle)
            # copy last 2 excel workbooks
            for year in excel_years:
                with open(self.excel_filename(year)) as handle:
                    db_client_update_file(client,
                                          "/regnskap/regnskap%s.xslx" % year,
                                          handle)
Exemplo n.º 3
0
def saveBackup(request, year, dropbox_client):
    year = int(year)
    # Store bilag files
    db_folder = "/regnskap/%d" % year
    try:
        files = dropbox_client.metadata(db_folder)['contents']
    except Exception as e:
        if e.status == 404:
            dropbox_client.file_create_folder(db_folder)
            files = dropbox_client.metadata(db_folder)['contents']
        else:
            return HttpResponse(str(e), status=e.status)
    files = [f["path"].lower() for f in files]
    server_folder = os.path.join(settings.MEDIA_ROOT, str(year))
    #    ret = [files]
    for f in os.listdir(server_folder):
        path = db_folder + "/" + f
        if path.lower() not in files:
            with open(os.path.join(server_folder, f)) as handle:
                #                ret.append(path)
                dropbox_client.put_file(db_folder + "/" + f, handle)

    # update the full excel export of the accounting year
    e = ExelYearView(year)

    filename = "regnskap/%d-regnskap.xlsx" % year
    try:
        metadata = dropbox_client.metadata(filename)['rev']
    except Exception:
        metadata = None
    ret = dropbox_client.put_file(filename,
                                  e.getExcelFileStream(),
                                  parent_rev=metadata)
    # update database dump
    filename = "regnskap/database_dump.sql"
    try:
        metadata = dropbox_client.metadata(filename)['rev']
    except Exception:
        metadata = None
    with open(os.path.join(settings.MEDIA_ROOT, "database_dump.sql")) as file:
        dropbox_client.put_file(filename, file, parent_rev=metadata)

    return HttpResponse(str(ret))