Exemplo n.º 1
0
    def handle(self, *args, **options):
        conn = sqlite3.connect(options['userdb_path'][0])
        """
        CREATE TABLE users (
        0    login TEXT PRIMARY KEY, 
        1    firstname TEXT, 
        2    lastname TEXT, 
        3    email TEXT, 
        4    password TEXT,
        5    path TEXT,
        6    token TEXT, 
        7    accepted INTEGER,
        8    project TEXT,
        9    affiliation TEXT,
        10   ip TEXT,
        11   clearance TEXT,
        12   date TEXT,
        13   visit TEXT)
        """

        print("Migrating users table...")
        user_paths = {}

        for row in conn.execute('SELECT * FROM users;'):
            username = sanitize_username(row[0])
            password = "******" + row[4]
            email = row[3]
            is_active = True
            is_superuser = True if row[11] == 'admin' else False

            user_paths[row[5]] = username
            date_joined = datetime.strptime(
                row[12], "%Y-%m-%d").replace(tzinfo=timezone.utc)

            newuser = User(username=username,
                           password=password,
                           email=email,
                           is_active=is_active,
                           is_superuser=is_superuser,
                           is_staff=is_superuser,
                           date_joined=date_joined)
            newuser.save()

            fullname = "%s %s" % (row[1], row[2])
            institution = row[9]

            if len(fullname) < 2:
                fullname = None

            newuser_profile = UserProfile(user=newuser,
                                          fullname=fullname,
                                          orcid=None,
                                          institution=institution)
            newuser_profile.save()

        print(" - Successful.")
        print("Moving project files... ")

        for path in user_paths:
            username = user_paths[path]

            # old user project dir
            src = os.path.join(options['userfiles_path'][0], path)

            #new user project dir
            dst = os.path.join(settings.USER_DATA_DIR, username)

            try:
                shutil.copytree(src, dst)
            except FileNotFoundError:
                # if user path does not exists create empty dir for user
                os.makedirs(dst)

        print(" - Successful")
        print("Migratins project table... ")
        """
        CREATE TABLE projects (
        0    name TEXT PRIMARY KEY, 
        1    path TEXT, 
        2    user TEXT, 
        3    description TEXT)
        """
        for row in conn.execute('SELECT * FROM projects;'):
            name = row[0]
            slug = slugify(name).replace('-', '_')
            path = row[1]
            username = sanitize_username(row[2])
            description = row[3]

            #rename project files
            fileTypes_old = [
                'treeFile', 'dataFile', 'fastaFile', 'samplesOrderFile',
                'samplesInformationFile'
            ]
            fileTypes_new = [
                'tree.txt', 'data.txt', 'fasta.fa', 'samples-order.txt',
                'samples-info.txt'
            ]

            for i in range(5):
                try:
                    os.rename(
                        os.path.join(settings.USER_DATA_DIR, username, path,
                                     fileTypes_old[i]),
                        os.path.join(settings.USER_DATA_DIR, username, path,
                                     fileTypes_new[i]))
                except:
                    pass

            try:
                if not description or not len(description) > 0:
                    description = ""

                project = Project(name=name,
                                  slug=slug,
                                  user=User.objects.get(username=username),
                                  secret=path)

                samples_info = project.get_file_path('samples-order.txt',
                                                     default=None)
                samples_order = project.get_file_path('samples-info.txt',
                                                      default=None)

                if (samples_info
                        or samples_order) and not project.get_file_path(
                            'samples.db', default=None):
                    s = dbops.SamplesInformationDatabase(project.get_file_path(
                        'samples.db', dont_check_exists=True),
                                                         quiet=True)
                    s.create(samples_order, samples_info)

                interactive = project.get_interactive()

                # try to get number of leaves
                try:
                    leaves = get_names_order_from_newick_tree(
                        project.get_file_path('tree.txt', default=None))
                    project.num_leaves = len(leaves) if leaves != [''] else 0
                except:
                    project.num_leaves = 0

                # try to get number of layers
                try:
                    project.num_layers = len(
                        interactive.views['single']
                        [0]) - 1  # <- -1 because first column is contigs
                except:
                    project.num_layers = 0

                # store description
                dbops.update_description_in_db(
                    project.get_file_path('profile.db', default=None),
                    description or '')

                project.synchronize_num_states()
                project.synchronize_num_collections()

                project.save()

                # try to migrate old links.
                for row_links in conn.execute(
                        'SELECT * FROM views WHERE project LIKE \'%s\';' %
                    (name)):
                    old_link = OldLinks(
                        name=row_links[0],
                        user=sanitize_username(row_links[1]),
                        project=Project.objects.filter(
                            name=row_links[2],
                            user__username=sanitize_username(row_links[1]))[0],
                        is_public=True if row_links[3] == 1 else False,
                        token=row_links[4])
                    old_link.save()

                    project_link = ProjectLink(project=old_link.project,
                                               link=old_link.token)
                    project_link.save()

            except Exception as e:
                print(username + " " + name + " " + path +
                      " failed to create project, here is the exception " +
                      str(e))
                shutil.rmtree(
                    os.path.join(settings.USER_DATA_DIR, username, path))

        print(" - Successful")
Exemplo n.º 2
0
def new_project(request):
    if request.method == "POST":
        try:
            name = request.POST.get('name')
            slug = slugify(name).replace('-', '_')

            if len(slug) < 1:
                raise Exception("Project name should not be empty.")

            project = Project.objects.filter(user=request.user, slug=slug)
            if project:
                if request.POST.get('delete-if-exists'):
                    project[0].delete_project_path()
                    project[0].delete()
                else:
                    raise Exception("Project with same name already exists, please give another name.")

            project = Project(name=name,
                              slug=slug,
                              user=request.user,
                              secret=get_random_string(length=32))

            project.create_project_path()

            fileTypes = ['tree.txt', 'data.txt', 'fasta.fa', 
                         'samples-order.txt', 'samples-info.txt',
                         'additional-layers.txt', 'state.json', 
                         'bins.txt', 'bins-info.txt', 'items-order.txt']

            for fileType in fileTypes:
                if fileType in request.FILES:
                    put_project_file(project.get_path(), fileType, request.FILES[fileType])

            interactive = project.get_interactive()
            profile_db_path = project.get_file_path('profile.db', default=None)

            samples_info = project.get_file_path('samples-info.txt', default=None)
            if samples_info:
                miscdata.MiscDataTableFactory(argparse.Namespace(target_data_table='layers', profile_db=profile_db_path)).populate_from_file(samples_info)

            samples_order = project.get_file_path('samples-order.txt', default=None)
            if samples_order:
                miscdata.MiscDataTableFactory(argparse.Namespace(target_data_table='layer_orders', profile_db=profile_db_path)).populate_from_file(samples_order)


            state_file = project.get_file_path('state.json', default=None)
            if state_file:
                interactive.states_table.store_state('default', open(state_file, 'r').read(), datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S"))
                project.num_states = 1

            bins_file = project.get_file_path('bins.txt', default=None)
            bins_info_file = project.get_file_path('bins-info.txt', default=None)
            if bins_file and bins_info_file:
                collections_table = collections.TablesForCollections(profile_db_path)

                bins = get_TAB_delimited_file_as_dictionary(bins_file, no_header = True, column_names = ['split_id', 'bin_name'])
                bins_info = get_TAB_delimited_file_as_dictionary(bins_info_file, no_header = True, column_names = ['bin_name', 'source', 'html_color'])

                bin_data = {}
                for split_name in bins:
                    bin_name = bins[split_name]['bin_name']
                    if not bin_name in bin_data:
                        bin_data[bin_name] = set([])

                    bin_data[bin_name].add(split_name)

                collections_table.append('default', bin_data, bins_info)
                project.num_collections = 1

            # try to get number of leaves
            try:
                project.num_leaves = len(interactive.displayed_item_names_ordered)
            except:
                project.num_leaves = 0

            # try to get number of layers
            try:
                project.num_layers = len(interactive.views['single'][0]) - 1 # <- -1 because first column is contigs
            except:
                project.num_layers = 0

            # store description
            dbops.update_description_in_db(profile_db_path, request.POST.get('description') or '')

            project.save()
            return JsonResponse({'status': 0})
        except Exception as e:
            try:
                project.delete_project_path()
            except:
                # slug is not unique, so there is no project instance or direcotry created.
                pass

            message = str(e.clear_text()) if 'clear_text' in dir(e) else str(e)

            # trim the full path from exception message, show only file name
            message = re.sub(r"(\'.*\/)(.+?)(\.txt\')", r"'\2'", message)

            return JsonResponse({
                    'status': 1,
                    'message': message
                    })

    return render(request, 'projects/new.html')