def restore(self): if self.name == '': raise AdminException("Instance not precise!") if not isfile(self.setting_path) or not isfile(self.instance_conf): raise AdminException("Instance not exists!") if self.filename == '': raise AdminException("Archive file not precise!") self.read() from lucterios.framework.filetools import get_tmp_dir, get_user_dir import tarfile tmp_path = join(get_tmp_dir(), 'tmp_resore') delete_path(tmp_path) mkdir(tmp_path) with tarfile.open(self.filename, "r:gz") as tar: for item in tar: tar.extract(item, tmp_path) output_filename = join(tmp_path, 'dump.json') success = False if isfile(output_filename): self._migrate_from_old_targets(tmp_path) self.clear_info_() from django.core.management import call_command call_command('loaddata', output_filename) self.run_new_migration(tmp_path) self.print_info_("instance restored with '%s'" % self.filename) if isdir(join(tmp_path, 'usr')): target_dir = get_user_dir() if isdir(target_dir): delete_path(target_dir) move(join(tmp_path, 'usr'), target_dir) success = True delete_path(tmp_path) self.refresh() return success
def get(self, request, *args, **kwargs): getLogger("lucterios.core.request").debug( ">> get %s [%s]", request.path, request.user) try: self._initialize(request, *args, **kwargs) full_path = join( get_user_dir(), six.text_type(self.getparam('filename'))) if not isfile(full_path): raise LucteriosException(IMPORTANT, _("File not found!")) sign = self.getparam('sign', '') if sign != md5sum(full_path): raise LucteriosException(IMPORTANT, _("File invalid!")) content_type, encoding = mimetypes.guess_type(full_path) content_type = content_type or 'application/octet-stream' statobj = os.stat(full_path) response = StreamingHttpResponse(open(full_path, 'rb'), content_type=content_type) response["Last-Modified"] = http_date(statobj.st_mtime) if stat.S_ISREG(statobj.st_mode): response["Content-Length"] = statobj.st_size if encoding: response["Content-Encoding"] = encoding return response finally: getLogger("lucterios.core.request").debug( "<< get %s [%s]", request.path, request.user)
def archive(self): if self.name == '': raise AdminException("Instance not precise!") if not isfile(self.setting_path) or not isfile(self.instance_conf): raise AdminException("Instance not exists!") if self.filename == '': raise AdminException("Archive file not precise!") self.read() from lucterios.framework.filetools import get_tmp_dir, get_user_dir from django.core.management import call_command from django.db import connection output_filename = join(get_tmp_dir(), 'dump.json') with open(output_filename, 'w') as output: call_command('dumpdata', stdout=output) target_filename = join(get_tmp_dir(), 'target') with open(target_filename, 'w') as output: with connection.cursor() as curs: curs.execute( "SELECT app,name FROM django_migrations ORDER BY applied") output.write(six.text_type(curs.fetchall())) import tarfile with tarfile.open(self.filename, "w:gz") as tar: tar.add(output_filename, arcname="dump.json") tar.add(target_filename, arcname="target") user_dir = get_user_dir() if isdir(user_dir): tar.add(user_dir, arcname="usr") delete_path(target_filename) delete_path(output_filename) return isfile(self.filename)
def archive(self): if self.name == '': raise AdminException("Instance not precise!") if not isfile(self.setting_path) or not isfile(self.instance_conf): raise AdminException("Instance not exists!") if self.filename == '': raise AdminException("Archive file not precise!") self.read() from lucterios.framework.filetools import get_tmp_dir, get_user_dir from django.core.management import call_command from django.db import connection output_filename = join(get_tmp_dir(), 'dump.json') with open(output_filename, 'w') as output: call_command('dumpdata', stdout=output) target_filename = join(get_tmp_dir(), 'target') with open(target_filename, 'w') as output: with connection.cursor() as curs: curs.execute( "SELECT app,name FROM django_migrations ORDER BY applied") output.write(six.text_type(curs.fetchall())) import tarfile with tarfile.open(self.filename, "w:gz") as tar: tar.add(output_filename, arcname="dump.json") tar.add(target_filename, arcname="target") user_dir = get_user_dir() if isdir(user_dir): tar.add(user_dir, arcname="usr") delete_path(target_filename) delete_path(output_filename) if isfile(self.filename): self.print_info_("Instance '%s' saved as '%s'." % (self.name, self.filename)) return True else: return False
def get_post(self, request, *args, **kwargs): getLogger("lucterios.core.request").debug( ">> get %s [%s]", request.path, request.user) try: self._initialize(request, *args, **kwargs) full_path = join( get_user_dir(), six.text_type(self.getparam('filename'))) if not isfile(full_path): raise LucteriosException(IMPORTANT, _("File not found!")) sign = self.getparam('sign', '') if sign != md5sum(full_path): raise LucteriosException(IMPORTANT, _("File invalid!")) content_type, encoding = mimetypes.guess_type(full_path) content_type = content_type or 'application/octet-stream' statobj = os.stat(full_path) response = StreamingHttpResponse(open(full_path, 'rb'), content_type=content_type) response["Last-Modified"] = http_date(statobj.st_mtime) if stat.S_ISREG(statobj.st_mode): response["Content-Length"] = statobj.st_size if encoding: response["Content-Encoding"] = encoding return response finally: getLogger("lucterios.core.request").debug( "<< get %s [%s]", request.path, request.user)
def setUp(self): self.xfer_class = XferContainerAcknowledge initial_thirds() LucteriosTest.setUp(self) default_compta() rmtree(get_user_dir(), True) fill_entries(1)
def setUp(self): LucteriosTest.setUp(self) rmtree(get_user_dir(), True) current_user = add_empty_user() current_user.is_superuser = False current_user.save() group = LucteriosGroup.objects.create(name="my_group") group.save() group = LucteriosGroup.objects.create(name="other_group") group.save() self.factory.user = LucteriosUser.objects.get(username='******') self.factory.user.groups.set(LucteriosGroup.objects.filter(id__in=[2])) self.factory.user.user_permissions.set(Permission.objects.all()) self.factory.user.save() folder1 = Folder.objects.create(name='truc1', description='blabla') folder1.viewer.set(LucteriosGroup.objects.filter(id__in=[1, 2])) folder1.modifier.set(LucteriosGroup.objects.filter(id__in=[1])) folder1.save() folder2 = Folder.objects.create(name='truc2', description='bouuuuu!') folder2.viewer.set(LucteriosGroup.objects.filter(id__in=[2])) folder2.modifier.set(LucteriosGroup.objects.filter(id__in=[2])) folder2.save() folder3 = Folder.objects.create(name='truc3', description='----') folder3.parent = folder2 folder3.viewer.set(LucteriosGroup.objects.filter(id__in=[2])) folder3.save() folder4 = Folder.objects.create(name='truc4', description='no') folder4.parent = folder2 folder4.save()
def setUp(self): self.xfer_class = XferContainerAcknowledge LucteriosTest.setUp(self) rmtree(get_user_dir(), True) current_user = add_empty_user() current_user.is_superuser = False current_user.save() group = LucteriosGroup.objects.create(name="my_group") group.save() group = LucteriosGroup.objects.create(name="other_group") group.save() self.factory.user = LucteriosUser.objects.get(username='******') self.factory.user.groups = LucteriosGroup.objects.filter(id__in=[2]) self.factory.user.user_permissions = Permission.objects.all() self.factory.user.save() folder1 = Folder.objects.create(name='truc1', description='blabla') folder1.viewer = LucteriosGroup.objects.filter(id__in=[1, 2]) folder1.modifier = LucteriosGroup.objects.filter(id__in=[1]) folder1.save() folder2 = Folder.objects.create(name='truc2', description='bouuuuu!') folder2.viewer = LucteriosGroup.objects.filter(id__in=[2]) folder2.modifier = LucteriosGroup.objects.filter(id__in=[2]) folder2.save() folder3 = Folder.objects.create(name='truc3', description='----') folder3.parent = folder2 folder3.viewer = LucteriosGroup.objects.filter(id__in=[2]) folder3.save() folder4 = Folder.objects.create(name='truc4', description='no') folder4.parent = folder2 folder4.save()
def setUp(self): self.xfer_class = XferContainerAcknowledge LucteriosTest.setUp(self) rmtree(get_user_dir(), True) default_season() default_params() set_parameters( ["team", "activite", "age", "licence", "genre", 'numero', 'birth'])
def setUp(self): LucteriosTest.setUp(self) rmtree(get_user_dir(), True) default_season() default_params() default_adherents() default_event_params() set_parameters(["team", "activite", "age", "licence", "genre", 'numero', 'birth'])
def setUp(self): self.xfer_class = XferContainerAcknowledge LucteriosTest.setUp(self) set_accounting_system() initial_thirds() default_compta() fill_entries(1) rmtree(get_user_dir(), True)
def setUp(self): self.xfer_class = XferContainerAcknowledge initial_thirds() LucteriosTest.setUp(self) default_compta() default_costaccounting() default_bankaccount() default_setowner() rmtree(get_user_dir(), True)
def setUp(self): self.xfer_class = XferContainerAcknowledge initial_thirds() InvoiceTest.setUp(self) default_compta() default_articles() default_bankaccount() rmtree(get_user_dir(), True) self.add_bills()
def clear(self, only_delete=False, ignore_db=None): self.read() from lucterios.framework.filetools import get_user_dir from django.db import connection option = 'CASCADE' if self.database[0] == 'postgresql': if only_delete: sql_cmd = 'DELETE FROM "%s" %s;' else: sql_cmd = 'DROP TABLE IF EXISTS "%s" %s;' else: if only_delete: sql_cmd = 'DELETE FROM %s %s;' else: sql_cmd = 'DROP TABLE IF EXISTS %s %s;' try: with connection.cursor() as curs: curs.execute('SET foreign_key_checks = 0;') except Exception: option = 'CASCADE' loop = 10 while loop > 0: tables = connection.introspection.table_names() tables.sort() no_error = True for table in tables: if (ignore_db is None) or (table not in ignore_db): try: try: with connection.cursor() as curs: curs.execute(sql_cmd % (table, option)) except Exception: with connection.cursor() as curs: curs.execute(sql_cmd % (table, '')) except Exception: no_error = False if no_error: loop = -1 else: loop -= 1 if self.database[0] != 'postgresql': try: with connection.cursor() as curs: curs.execute('SET foreign_key_checks = 1;') except Exception: pass if loop == 0: raise Exception("not clear!!") user_path = get_user_dir() delete_path(user_path) self.print_info_("Instance '%s' clear." % self.name)
def _get_reports_archive(self): filename = "pdfreports.zip" download_file = join(get_user_dir(), filename) with ZipFile(download_file, 'w') as zip_ref: for pdf_name, pdf_content in self.get_persistent_pdfreport(): if isinstance(pdf_content, BytesIO): pdf_content = pdf_content.read() if isinstance(pdf_content, six.text_type): pdf_content = pdf_content.encode() if isinstance(pdf_content, six.binary_type): zip_ref.writestr(zinfo_or_arcname=pdf_name, data=pdf_content) return filename
def setUp(self): self.xfer_class = XferContainerAcknowledge LucteriosTest.setUp(self) rmtree(get_user_dir(), True) default_financial() default_season() default_params() default_adherents() default_subscription() default_event_params() set_parameters( ["team", "activite", "age", "licence", "genre", 'numero', 'birth']) add_default_degree()
def clear(self, only_delete=False, ignore_db=None): self.read(True) from lucterios.framework.filetools import get_user_dir from django.db import connection option = 'CASCADE' if self.database[0] == 'postgresql': if only_delete: sql_cmd = 'DELETE FROM "%s" %s;' else: sql_cmd = 'DROP TABLE IF EXISTS "%s" %s;' else: if only_delete: sql_cmd = 'DELETE FROM %s %s;' else: sql_cmd = 'DROP TABLE IF EXISTS %s %s;' try: with connection.cursor() as curs: curs.execute('SET foreign_key_checks = 0;') except Exception: option = 'CASCADE' loop = 10 while loop > 0: tables = connection.introspection.table_names() tables.sort() no_error = True for table in tables: if (ignore_db is None) or (table not in ignore_db): try: try: with connection.cursor() as curs: curs.execute(sql_cmd % (table, option)) except Exception: with connection.cursor() as curs: curs.execute(sql_cmd % (table, '')) except Exception: no_error = False if no_error: loop = -1 else: loop -= 1 if self.database[0] != 'postgresql': try: with connection.cursor() as curs: curs.execute('SET foreign_key_checks = 1;') except Exception: pass if loop == 0: raise Exception("not clear!!") user_path = get_user_dir() delete_path(user_path) self.print_info_("Instance '%s' clear." % self.name)
def setUp(self): LucteriosTest.setUp(self) if hasattr(settings, "ETHERPAD"): settings.ETHERPAD = {} if hasattr(settings, "ETHERCALC"): settings.ETHERCALC = {} rmtree(get_user_dir(), True) default_groups() default_folders() self.factory.user = LucteriosUser.objects.get(username='******') self.factory.user.groups.set(LucteriosGroup.objects.filter(id__in=[2])) self.factory.user.user_permissions.set(Permission.objects.all()) self.factory.user.save()
def setUp(self): DocumentShow.url_text initial_thirds_fr() LucteriosTest.setUp(self) default_compta_fr(with12=False) default_bankaccount_fr() default_setowner_fr() contact = Individual.objects.get(id=5) contact.user = add_user('joe') contact.save() rmtree(get_user_dir(), True) add_years() init_compta() add_test_callfunds(False, True) add_test_expenses_fr(False, True)
def run_archive(self): tmp_dir = join(get_tmp_dir(), 'zipfile') download_file = join(get_user_dir(), 'extract.zip') if exists(tmp_dir): rmtree(tmp_dir) makedirs(tmp_dir) try: self.item.extract_files(tmp_dir) with ZipFile(download_file, 'w') as zip_ref: for (dirpath, _dirs, filenames) in walk(tmp_dir): for filename in filenames: zip_ref.write(join(dirpath, filename), join(dirpath[len(tmp_dir):], filename)) finally: if exists(tmp_dir): rmtree(tmp_dir) self.open_zipfile('extract.zip')
def run_archive(self): tmp_dir = join(get_tmp_dir(), 'zipfile') download_file = join(get_user_dir(), 'extract.zip') if exists(tmp_dir): rmtree(tmp_dir) makedirs(tmp_dir) try: self.item.extract_files(tmp_dir) with ZipFile(download_file, 'w') as zip_ref: for (dirpath, _dirs, filenames) in walk(tmp_dir): for filename in filenames: zip_ref.write( join(dirpath, filename), join(dirpath[len(tmp_dir):], filename)) finally: if exists(tmp_dir): rmtree(tmp_dir) self.open_zipfile('extract.zip')
def show(self, xfer): destination_file = join("documents", "document_%s" % six.text_type(self.item.id)) if not isfile(join(get_user_dir(), destination_file)): raise LucteriosException(IMPORTANT, _("File not found!")) obj_cmt = xfer.get_components('creator') down = XferCompDownLoad('filename') down.compress = True down.http_file = True down.maxsize = 0 down.set_value(self.item.name) down.set_download(destination_file) down.set_action(xfer.request, ActionsManage.get_action_url('documents.Document', 'AddModify', xfer), modal=FORMTYPE_MODAL, close=CLOSE_NO) down.set_location(obj_cmt.col, obj_cmt.row + 1, 4) xfer.add_component(down)
def extract_file(self): tmp_dir = join(get_tmp_dir(), 'zipfile') download_file = join(get_user_dir(), 'extract-%d.mdl' % self.id) if exists(tmp_dir): rmtree(tmp_dir) makedirs(tmp_dir) try: content_model = "# -*- coding: utf-8 -*-\n\n" content_model += "from __future__ import unicode_literals\n\n" content_model += 'name = "%s"\n' % self.name content_model += 'kind = %d\n' % int(self.kind) content_model += 'modelname = "%s"\n' % self.modelname content_model += 'value = """%s"""\n' % self.value content_model += 'mode = %d\n' % int(self.mode) with ZipFile(download_file, 'w') as zip_ref: zip_ref.writestr('printmodel', content_model) finally: if exists(tmp_dir): rmtree(tmp_dir) return 'extract-%d.mdl' % self.id
def show(self, xfer): destination_file = join("documents", "document_%s" % six.text_type(self.item.id)) if not isfile(join(get_user_dir(), destination_file)): raise LucteriosException(IMPORTANT, _("File not found!")) obj_cmt = xfer.get_components('creator') down = XferCompDownLoad('filename') down.compress = True down.http_file = True down.maxsize = 0 down.set_value(self.item.name) down.set_download(destination_file) down.set_action(xfer.request, ActionsManage.get_action_url('documents.Document', 'AddModify', xfer), modal=FORMTYPE_MODAL, close=CLOSE_NO) down.set_location(obj_cmt.col, obj_cmt.row + 1, 4) xfer.add_component(down) link = self.item.shared_link if link is not None: shared_link = XferCompEdit('shared_link') shared_link.description = _('shared link') shared_link.set_value(link) shared_link.set_location(obj_cmt.col, obj_cmt.row + 2, 4) xfer.add_component(shared_link)
def setUp(self): self.xfer_class = XferContainerAcknowledge change_ourdetail() LucteriosTest.setUp(self) rmtree(get_user_dir(), True) self.server.start(1025)
def setUp(self): LucteriosTest.setUp(self) rmtree(get_user_dir(), True)
def setUp(self): LucteriosTest.setUp(self) rmtree(get_user_dir(), True) set_parameters( ["team", "activite", "age", "licence", "genre", 'numero', 'birth'])
def setUp(self): LucteriosTest.setUp(self) change_ourdetail() create_jack(add_empty_user()) rmtree(get_user_dir(), True)
def setUp(self): self.xfer_class = XferContainerAcknowledge LucteriosTest.setUp(self) rmtree(get_user_dir(), True) change_ourdetail()