Exemple #1
0
def get_user_info(email):

    user = User.objects.get(email=email)
    d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email)
    profile = Profile.objects.get_profile_by_user(email)

    info = {}
    info['email'] = email
    info['name'] = email2nickname(email)
    info['contact_email'] = profile.contact_email if profile and profile.contact_email else ''
    info['login_id'] = profile.login_id if profile and profile.login_id else ''

    info['is_staff'] = user.is_staff
    info['is_active'] = user.is_active
    info['create_time'] = user.ctime
    info['reference_id'] = user.reference_id if user.reference_id else ''

    info['department'] = d_profile.department if d_profile else ''

    info['quota_total'] = seafile_api.get_user_quota(email)
    info['quota_usage'] = seafile_api.get_user_self_usage(email)

    info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)

    if is_pro_version():
        info['role'] = user.role

    return info
Exemple #2
0
    def get_file_info(self, username, repo_id, file_path):

        repo = seafile_api.get_repo(repo_id)
        file_obj = seafile_api.get_dirent_by_path(repo_id, file_path)
        file_name = file_obj.obj_name
        file_size = file_obj.size

        can_preview, error_msg = can_preview_file(file_name, file_size, repo)
        can_edit, error_msg  = can_edit_file(file_name, file_size, repo)

        try:
            is_locked, locked_by_me = check_file_lock(repo_id, file_path, username)
        except Exception as e:
            logger.error(e)
            is_locked = False

        file_info = {
            'type': 'file',
            'repo_id': repo_id,
            'parent_dir': os.path.dirname(file_path),
            'obj_name': file_name,
            'obj_id': file_obj.obj_id,
            'size': file_size,
            'mtime': timestamp_to_isoformat_timestr(file_obj.mtime),
            'is_locked': is_locked,
            'can_preview': can_preview,
            'can_edit': can_edit,
        }

        return file_info
Exemple #3
0
def get_trash_repo_info(repo):

    result = {}
    result["name"] = repo.repo_name
    result["id"] = repo.repo_id
    result["owner"] = repo.owner_id
    result["delete_time"] = timestamp_to_isoformat_timestr(repo.del_time)

    return result
def get_trash_repo_info(repo):

    result = {}
    result['name'] = repo.repo_name
    result['id'] = repo.repo_id
    result['owner'] = repo.owner_id
    result['delete_time'] = timestamp_to_isoformat_timestr(repo.del_time)

    return result
Exemple #5
0
    def get(self, request, repo_id):
        """ Return repo info

        Permission checking:
        1. all authenticated user can perform this action.
        """

        # resource check
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = 'Library %s not found.' % repo_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # permission check
        permission = check_folder_permission(request, repo_id, '/')
        if permission is None:
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        username = request.user.username

        lib_need_decrypt = False
        if repo.encrypted \
                and not seafile_api.is_password_set(repo.id, username):
            lib_need_decrypt = True

        repo_owner = get_repo_owner(request, repo_id)

        try:
            has_been_shared_out = repo_has_been_shared_out(request, repo_id)
        except Exception as e:
            has_been_shared_out = False
            logger.error(e)

        result = {
            "repo_id": repo.id,
            "repo_name": repo.name,

            "owner_email": repo_owner,
            "owner_name": email2nickname(repo_owner),
            "owner_contact_email": email2contact_email(repo_owner),

            "size": repo.size,
            "encrypted": repo.encrypted,
            "file_count": repo.file_count,
            "permission": permission,
            "no_quota": True if seafile_api.check_quota(repo_id) < 0 else False,
            "is_admin": is_repo_admin(username, repo_id),
            "is_virtual": repo.is_virtual,
            "has_been_shared_out": has_been_shared_out,

            "lib_need_decrypt": lib_need_decrypt,
            "last_modified": timestamp_to_isoformat_timestr(repo.last_modify),
        }

        return Response(result)
Exemple #6
0
def get_group_info(group_id):
    group = ccnet_api.get_group(group_id)
    isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
    group_info = {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": isoformat_timestr,
    }

    return group_info
Exemple #7
0
def get_group_repo_info(request, group_repo):

    group_id = group_repo.group_id
    repo_id = group_repo.repo_id

    is_admin = ExtraGroupsSharePermission.objects.get_group_permission(repo_id,
            group_id)

    group_repo_info = {}
    group_repo_info['repo_id'] = repo_id
    group_repo_info['repo_name'] = group_repo.name

    group_repo_info['mtime'] = timestamp_to_isoformat_timestr(group_repo.last_modified)
    group_repo_info['last_modified'] = timestamp_to_isoformat_timestr(group_repo.last_modified)
    group_repo_info['permission'] = group_repo.permission
    group_repo_info['size'] = group_repo.size
    group_repo_info['encrypted'] = group_repo.encrypted
    group_repo_info['is_admin'] = True if is_admin else False

    return group_repo_info
Exemple #8
0
    def _get_group_info(self, request, group, avatar_size):
        isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
        group_info = {
            "id": group.id,
            "parent_group_id": group.parent_group_id,
            "name": group.group_name,
            "owner": group.creator_name,
            "created_at": isoformat_timestr,
        }

        return group_info
Exemple #9
0
    def get_item_info(self, commit):
        email = commit.creator_name
        item_info = {
            "name": email2nickname(email),
            "contact_email": Profile.objects.get_contact_email_by_user(email),
            'email': email,
            'time': timestamp_to_isoformat_timestr(commit.ctime),
            'description': commit.desc,
            'commit_id': commit.id,
        }

        return item_info
Exemple #10
0
def address_book_group_to_dict(group):
    if isinstance(group, int):
        group = ccnet_api.get_group(group)

    return {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": timestamp_to_isoformat_timestr(group.timestamp),
        "parent_group_id": group.parent_group_id,
        "quota": seafile_api.get_group_quota(group.id),
    }
Exemple #11
0
 def to_dict(self):
     return {
         'id': self.pk,
         'owner': self.username,
         'owner_nickname': email2nickname(self.username),
         'name': self.name,
         'slug': self.slug,
         'link': self.link,
         'permission': self.permission,
         'created_at': datetime_to_isoformat_timestr(self.created_at),
         'updated_at': timestamp_to_isoformat_timestr(self.updated_at),
     }
Exemple #12
0
def address_book_group_to_dict(group):
    if isinstance(group, int):
        group = ccnet_api.get_group(group)

    return {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": timestamp_to_isoformat_timestr(group.timestamp),
        "parent_group_id": group.parent_group_id,
        "quota": seafile_api.get_group_quota(group.id),
    }
Exemple #13
0
    def get(self, request, repo_id):
        """ Get dir info.

        Permission checking:
        1. user with either 'r' or 'rw' permission.
        """

        # parameter check
        path = request.GET.get('path', None)
        if not path:
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        path = normalize_dir_path(path)
        if path == '/':
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        # resource check
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = 'Library %s not found.' % repo_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        dir_id = seafile_api.get_dir_id_by_path(repo_id, path)
        if not dir_id:
            error_msg = 'Folder %s not found.' % path
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # permission check
        if not check_folder_permission(request, repo_id, path):
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        try:
            dir_obj = seafile_api.get_dirent_by_path(repo_id, path)
            count_info = seafile_api.get_file_count_info_by_path(repo_id, path)
        except SearpcError as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        dir_info = {
            'repo_id': repo_id,
            'path': path,
            'name': dir_obj.obj_name,
            'file_count': count_info.file_count,
            'dir_count': count_info.dir_count,
            'size': count_info.size,
            'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
        }

        return Response(dir_info)
Exemple #14
0
    def get(self, request, org_id):
        """ Get all groups in an org.

        Permission checking:
        1. only admin can perform this action.
        """
        org_id = int(org_id)
        if org_id == 0:
            error_msg = 'org_id invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        org = ccnet_api.get_org_by_id(org_id)
        if not org:
            error_msg = 'Organization %d not found.' % org_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        try:
            groups = ccnet_api.get_org_groups(org_id, -1, -1)
        except Exception as e:
            logger.error(e)
            error_msg = "Internal Server Error"
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        # Use dict to reduce memcache fetch cost in large for-loop.
        nickname_dict = {}
        contact_email_dict = {}
        creator_name_set = set([g.creator_name for g in groups])
        for e in creator_name_set:
            if e not in nickname_dict:
                nickname_dict[e] = email2nickname(e)
            if e not in contact_email_dict:
                contact_email_dict[e] = email2contact_email(e)

        groups_info = []
        for group in groups:
            group_info = {}
            group_info['group_name'] = group.group_name
            group_info['creator_email'] = group.creator_name
            group_info['creator_name'] = nickname_dict.get(
                group.creator_name, '')
            group_info['creator_contact_email'] = contact_email_dict.get(
                group.creator_name, '')
            group_info['created_at'] = timestamp_to_isoformat_timestr(
                group.timestamp)
            group_info[
                'parent_group_id'] = group.parent_group_id if is_pro_version(
                ) else 0
            group_info['group_id'] = group.id

            groups_info.append(group_info)

        return Response({'group_list': groups_info})
Exemple #15
0
    def get(self, request, email):
        """ List 'all' libraries shared to a user

        Permission checking:
        1. only admin can perform this action.
        """

        if not request.user.admin_permissions.can_manage_user():
            return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')

        try:
            User.objects.get(email=email)
        except User.DoesNotExist as e:
            logger.error(e)
            error_msg = 'User %s not found.' % email
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        try:
            beshared_repos = seafile_api.get_share_in_repo_list(email, -1, -1)
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        # Use dict to reduce memcache fetch cost in large for-loop.
        nickname_dict = {}
        owner_set = set([x.user for x in beshared_repos])
        for email in owner_set:
            if email not in nickname_dict:
                if '@seafile_group' in email:
                    group_id = get_group_id_by_repo_owner(email)
                    group_name= group_id_to_name(group_id)
                    nickname_dict[email] = group_name
                else:
                    nickname_dict[email] = email2nickname(email)

        repos_info = []
        for repo in beshared_repos:
            repo_info = {}
            repo_info['id'] = repo.repo_id
            repo_info['name'] = repo.repo_name
            repo_info['owner_email'] = repo.user
            repo_info['owner_name'] = nickname_dict.get(repo.user, '')
            repo_info['size'] = repo.size
            repo_info['encrypted'] = repo.encrypted
            repo_info['file_count'] = repo.file_count
            repo_info['status'] = normalize_repo_status_code(repo.status)
            repo_info['last_modify'] = timestamp_to_isoformat_timestr(repo.last_modify)

            repos_info.append(repo_info)

        return Response({'repo_list': repos_info})
Exemple #16
0
    def get_dir_info(self, repo_id, dir_path):

        dir_obj = seafile_api.get_dirent_by_path(repo_id, dir_path)
        dir_info = {
            'type': 'dir',
            'repo_id': repo_id,
            'parent_dir': os.path.dirname(dir_path.rstrip('/')),
            'obj_name': dir_obj.obj_name,
            'obj_id': dir_obj.obj_id,
            'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
        }

        return dir_info
Exemple #17
0
    def get_dir_info(self, repo_id, dir_path):

        dir_obj = seafile_api.get_dirent_by_path(repo_id, dir_path)
        dir_info = {
            'type': 'dir',
            'repo_id': repo_id,
            'parent_dir': os.path.dirname(dir_path.rstrip('/')),
            'obj_name': dir_obj.obj_name,
            'obj_id': dir_obj.obj_id,
            'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
        }

        return dir_info
Exemple #18
0
 def get_related_file(self, r_repo_id, r_file_path, r_uuid):
     related_file = dict()
     related_file["name"] = r_uuid.filename
     related_file["repo_id"] = r_repo_id
     r_repo = seafile_api.get_repo(r_repo_id)
     if not r_repo:
         related_file["repo_name"] = ""
     related_file["repo_name"] = r_repo.name
     related_file["path"] = r_file_path
     file_obj = seafile_api.get_dirent_by_path(r_repo_id, r_file_path)
     related_file["size"] = file_obj.size
     related_file["last_modified"] = timestamp_to_isoformat_timestr(file_obj.mtime)
     return related_file
def get_group_repo_info(request, group_repo):

    group_id = group_repo.group_id
    repo_id = group_repo.repo_id

    is_admin = ExtraGroupsSharePermission.objects.get_group_permission(
        repo_id, group_id)

    group_repo_info = {}
    group_repo_info['repo_id'] = repo_id
    group_repo_info['repo_name'] = group_repo.name

    group_repo_info['mtime'] = timestamp_to_isoformat_timestr(
        group_repo.last_modified)
    group_repo_info['last_modified'] = timestamp_to_isoformat_timestr(
        group_repo.last_modified)
    group_repo_info['permission'] = group_repo.permission
    group_repo_info['size'] = group_repo.size
    group_repo_info['encrypted'] = group_repo.encrypted
    group_repo_info['is_admin'] = True if is_admin else False

    return group_repo_info
Exemple #20
0
def get_group_info(group_id):
    group = ccnet_api.get_group(group_id)
    isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
    group_info = {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": isoformat_timestr,
    }

    if is_pro_version():
        group_info['quota'] = seafile_api.get_group_quota(group_id)

    return group_info
Exemple #21
0
    def get(self, request):
        """list all departments
        """

        if not is_pro_version():
            return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')

        try:
            departments = ccnet_api.list_all_departments()
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        try:
            avatar_size = int(
                request.GET.get('avatar_size', GROUP_AVATAR_DEFAULT_SIZE))
        except ValueError:
            avatar_size = GROUP_AVATAR_DEFAULT_SIZE

        result = []
        for department in departments:
            department = seaserv.get_group(department.id)

            username = request.user.username
            if not is_group_member(department.id, username):
                continue

            try:
                avatar_url, is_default, date_uploaded = api_grp_avatar_url(
                    department.id, avatar_size)
            except Exception as e:
                logger.error(e)
                avatar_url = get_default_group_avatar_url()

            created_at = timestamp_to_isoformat_timestr(department.timestamp)

            department_info = {
                "id": department.id,
                "email": '%s@seafile_group' % str(department.id),
                "parent_group_id": department.parent_group_id,
                "name": department.group_name,
                "owner": department.creator_name,
                "created_at": created_at,
                "avatar_url": request.build_absolute_uri(avatar_url),
            }

            result.append(department_info)

        return Response(result)
Exemple #22
0
def get_group_info(group_id):
    group = ccnet_api.get_group(group_id)
    isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
    group_info = {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "owner_name": email2nickname(group.creator_name),
        "created_at": isoformat_timestr,
        "quota": seafile_api.get_group_quota(group_id) if is_pro_version() else 0,
        "parent_group_id": group.parent_group_id if is_pro_version() else 0
    }

    return group_info
Exemple #23
0
def get_dir_file_recursively(repo_id, path, all_dirs):
    is_pro = is_pro_version()
    path_id = seafile_api.get_dir_id_by_path(repo_id, path)
    dirs = seafile_api.list_dir_by_path(repo_id, path, -1, -1)

    for dirent in dirs:
        entry = {}
        if stat.S_ISDIR(dirent.mode):
            entry["type"] = 'dir'
        else:
            entry["type"] = 'file'
            entry['modifier_email'] = dirent.modifier
            entry["size"] = dirent.size

            if is_pro:
                entry["is_locked"] = dirent.is_locked
                entry["lock_owner"] = dirent.lock_owner
                if dirent.lock_owner:
                    entry["lock_owner_name"] = email2nickname(
                        dirent.lock_owner)
                entry["lock_time"] = dirent.lock_time

        entry["parent_dir"] = path
        entry["id"] = dirent.obj_id
        entry["name"] = dirent.obj_name
        entry["mtime"] = timestamp_to_isoformat_timestr(dirent.mtime)

        all_dirs.append(entry)

        # Use dict to reduce memcache fetch cost in large for-loop.
        file_list = [item for item in all_dirs if item['type'] == 'file']
        contact_email_dict = {}
        nickname_dict = {}
        modifiers_set = {x['modifier_email'] for x in file_list}
        for e in modifiers_set:
            if e not in contact_email_dict:
                contact_email_dict[e] = email2contact_email(e)
            if e not in nickname_dict:
                nickname_dict[e] = email2nickname(e)

        for e in file_list:
            e['modifier_contact_email'] = contact_email_dict.get(
                e['modifier_email'], '')
            e['modifier_name'] = nickname_dict.get(e['modifier_email'], '')

        if stat.S_ISDIR(dirent.mode):
            sub_path = posixpath.join(path, dirent.obj_name)
            get_dir_file_recursively(repo_id, sub_path, all_dirs)

    return all_dirs
Exemple #24
0
def get_group_info(group_id):
    group = ccnet_api.get_group(group_id)
    isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
    group_info = {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": isoformat_timestr,
        "quota":
        seafile_api.get_group_quota(group_id) if is_pro_version() else 0,
        "parent_group_id": group.parent_group_id if is_pro_version() else 0
    }

    return group_info
def get_dirent_info(dirent):

    if stat.S_ISDIR(dirent.mode):
        is_file = False
    else:
        is_file = True

    result = {}
    result['is_file'] = is_file
    result['obj_name'] = dirent.obj_name
    result['file_size'] = filesizeformat(dirent.size) if is_file else ''
    result['last_update'] = timestamp_to_isoformat_timestr(dirent.mtime)

    return result
Exemple #26
0
def get_dirent_info(dirent):

    if stat.S_ISDIR(dirent.mode):
        is_file = False
    else:
        is_file = True

    result = {}
    result['is_file'] = is_file
    result['obj_name'] = dirent.obj_name
    result['file_size'] = filesizeformat(dirent.size) if is_file else ''
    result['last_update'] = timestamp_to_isoformat_timestr(dirent.mtime)

    return result
Exemple #27
0
    def get(self, request):
        """List all admins from database and ldap imported
        """
        try:
            admin_users = ccnet_api.get_superusers()
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        admin_users_info = []
        for user in admin_users:
            user_info = {}
            profile = Profile.objects.get_profile_by_user(user.email)
            user_info['email'] = user.email
            user_info['name'] = email2nickname(user.email)
            user_info['contact_email'] = email2contact_email(user.email)
            user_info[
                'login_id'] = profile.login_id if profile and profile.login_id else ''

            user_info['is_staff'] = user.is_staff
            user_info['is_active'] = user.is_active

            orgs = ccnet_api.get_orgs_by_user(user.email)
            try:
                if orgs:
                    org_id = orgs[0].org_id
                    user_info['org_id'] = org_id
                    user_info['org_name'] = orgs[0].org_name
            except Exception as e:
                logger.error(e)

            user_info['create_time'] = timestamp_to_isoformat_timestr(
                user.ctime)
            last_login_obj = UserLastLogin.objects.get_by_username(user.email)
            user_info['last_login'] = datetime_to_isoformat_timestr(
                last_login_obj.last_login) if last_login_obj else ''

            try:
                admin_role = AdminRole.objects.get_admin_role(user.email)
                user_info['admin_role'] = admin_role.role
            except AdminRole.DoesNotExist:
                user_info['admin_role'] = DEFAULT_ADMIN
            admin_users_info.append(user_info)

        result = {
            'admin_user_list': admin_users_info,
        }
        return Response(result)
Exemple #28
0
    def get(self, request, format=None):
        """  Search file by name.
        """

        # argument check
        repo_id = request.GET.get('repo_id', None)
        if not repo_id:
            error_msg = 'repo_id invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        q = request.GET.get('q', None)
        if not q:
            error_msg = 'q invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        # resource check
        if not seafile_api.get_repo(repo_id):
            error_msg = 'Library %s not found.' % repo_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # permission check
        if not check_folder_permission(request, repo_id, '/'):
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        file_list = []
        folder_list = []

        searched_files = seafile_api.search_files(repo_id, q)

        for searched_file in searched_files:
            # {'path': '/123.docx', 'size': 19446, 'mtime': 1604130882, 'is_dir': False}
            dirent_info = {}
            dirent_info['path'] = searched_file.path
            dirent_info['size'] = searched_file.size
            dirent_info['mtime'] = timestamp_to_isoformat_timestr(
                searched_file.mtime)

            if searched_file.is_dir:
                dirent_info['type'] = 'folder'
                folder_list.append(dirent_info)
            else:
                dirent_info['type'] = 'file'
                file_list.append(dirent_info)

        folder_list.sort(key=lambda x: x['mtime'], reverse=True)
        file_list.sort(key=lambda x: x['mtime'], reverse=True)

        return Response({'data': folder_list + file_list})
Exemple #29
0
    def get(self, request, repo_id):
        """ Get dir info.

        Permission checking:
        1. user with either 'r' or 'rw' permission.
        """

        # parameter check
        path = request.GET.get('path', None)
        if not path:
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        path = normalize_dir_path(path)
        if path == '/':
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        # resource check
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = 'Library %s not found.' % repo_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        dir_id = seafile_api.get_dir_id_by_path(repo_id, path)
        if not dir_id:
            error_msg = 'Folder %s not found.' % path
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # permission check
        if not check_folder_permission(request, repo_id, path):
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        try:
            dir_obj = seafile_api.get_dirent_by_path(repo_id, path)
        except SearpcError as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        dir_info = {
            'repo_id': repo_id,
            'path': path,
            'name': dir_obj.obj_name,
            'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
        }

        return Response(dir_info)
Exemple #30
0
    def get_item_info(self, commit):
        email = commit.creator_name
        item_info = {
            'email': email,
            "name": email2nickname(email),
            "contact_email": email2contact_email(email),
            'time': timestamp_to_isoformat_timestr(commit.ctime),
            'commit_id': commit.id,
            'description': translate_commit_desc(commit.desc),
            'client_version': commit.client_version,
            'device_name': commit.device_name,
            'second_parent_id': commit.second_parent_id,
        }

        return item_info
Exemple #31
0
    def get_starred_item_info(self, repo, starred_item):

        item_info = {}

        repo_id = starred_item.repo_id
        item_info['repo_id'] = repo_id
        item_info['repo_name'] = repo.repo_name if repo else ''
        item_info['repo_encrypted'] = repo.encrypted
        item_info['is_dir'] = starred_item.is_dir

        path = starred_item.path

        item_info['path'] = path
        if path == '/':
            item_info['obj_name'] = repo.repo_name if repo else ''
            item_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified) if \
                    repo else ''
        else:
            item_info['obj_name'] = os.path.basename(path.rstrip('/'))
            dirent = seafile_api.get_dirent_by_path(repo_id, path)
            item_info['mtime'] = timestamp_to_isoformat_timestr(dirent.mtime) if \
                    dirent else ''

        return item_info
Exemple #32
0
    def get_starred_item_info(self, repo, starred_item):

        item_info = {}

        repo_id = starred_item.repo_id
        item_info['repo_id'] = repo_id
        item_info['repo_name'] = repo.repo_name if repo else ''
        item_info['repo_encrypted'] = repo.encrypted
        item_info['is_dir'] = starred_item.is_dir

        path = starred_item.path

        item_info['path'] = path
        if path == '/':
            item_info['obj_name'] = repo.repo_name if repo else ''
            item_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified) if \
                    repo else ''
        else:
            item_info['obj_name'] = os.path.basename(path.rstrip('/'))
            dirent = seafile_api.get_dirent_by_path(repo_id, path)
            item_info['mtime'] = timestamp_to_isoformat_timestr(dirent.mtime) if \
                    dirent else ''
            if not starred_item.is_dir:
                file_type, file_ext = get_file_type_and_ext(
                    item_info['obj_name'])
                if file_type in (IMAGE, XMIND) or \
                        (file_type == VIDEO and ENABLE_VIDEO_THUMBNAIL):
                    thumbnail_size = THUMBNAIL_DEFAULT_SIZE
                    thumbnail_file_path = os.path.join(THUMBNAIL_ROOT,
                                                       str(thumbnail_size),
                                                       dirent.obj_id)
                    if os.path.exists(thumbnail_file_path):
                        src = get_thumbnail_src(repo_id, thumbnail_size, path)
                        item_info['encoded_thumbnail_src'] = urlquote(src)

        return item_info
    def get_starred_item_info(self, repo, starred_item):

        item_info = {}

        repo_id = starred_item.repo_id
        item_info['repo_id'] = repo_id
        item_info['repo_name'] = repo.repo_name if repo else ''
        item_info['repo_encrypted'] = repo.encrypted
        item_info['is_dir'] = starred_item.is_dir

        path = starred_item.path

        item_info['path'] = path
        if path == '/':
            item_info['obj_name'] = repo.repo_name if repo else ''
            item_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified) if \
                    repo else ''
        else:
            item_info['obj_name'] = os.path.basename(path.rstrip('/'))
            dirent = seafile_api.get_dirent_by_path(repo_id, path)
            item_info['mtime'] = timestamp_to_isoformat_timestr(dirent.mtime) if \
                    dirent else ''

        return item_info
Exemple #34
0
def get_group_owned_repo_info(request, repo_id):

    repo = seafile_api.get_repo(repo_id)

    repo_info = {}
    repo_info['repo_id'] = repo_id
    repo_info['repo_name'] = repo.name

    repo_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified)
    repo_info['size'] = repo.size
    repo_info['encrypted'] = repo.encrypted

    repo_owner = get_repo_owner(request, repo_id)
    repo_info['owner_email'] = repo_owner

    return repo_info
def get_group_owned_repo_info(request, repo_id):

    repo = seafile_api.get_repo(repo_id)

    repo_info = {}
    repo_info['repo_id'] = repo_id
    repo_info['repo_name'] = repo.name

    repo_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified)
    repo_info['size'] = repo.size
    repo_info['encrypted'] = repo.encrypted

    repo_owner = get_repo_owner(request, repo_id)
    repo_info['owner_email'] = repo_owner

    return repo_info
Exemple #36
0
def get_trash_repo_info(repo):

    result = {}

    owner = repo.owner_id

    result['name'] = repo.repo_name
    result['id'] = repo.repo_id
    result['owner'] = owner
    result['delete_time'] = timestamp_to_isoformat_timestr(repo.del_time)

    if '@seafile_group' in owner:
        group_id = get_group_id_by_repo_owner(owner)
        result['group_name'] = group_id_to_name(group_id)

    return result
Exemple #37
0
    def get_file_info(self, username, repo_id, file_path):

        file_obj = seafile_api.get_dirent_by_path(repo_id, file_path)
        is_locked, locked_by_me = check_file_lock(repo_id, file_path, username)
        file_info = {
            'type': 'file',
            'repo_id': repo_id,
            'parent_dir': os.path.dirname(file_path),
            'obj_name': file_obj.obj_name,
            'obj_id': file_obj.obj_id,
            'size': file_obj.size,
            'mtime': timestamp_to_isoformat_timestr(file_obj.mtime),
            'is_locked': is_locked,
        }

        return file_info
Exemple #38
0
def get_institution_user_info(user_obj, institution):
    info = {}
    info['email'] = user_obj.email
    info['name'] = email2nickname(user_obj.email)
    info['contact_email'] = email2contact_email(user_obj.email)

    info['quota_usage'], info['quota_total'] = get_user_quota_usage_and_total(user_obj.email)

    info['create_time'] = timestamp_to_isoformat_timestr(user_obj.ctime)
    info['is_active'] = user_obj.is_active
    info['is_institution_admin'] = is_institution_admin(user_obj.email, institution)

    last_login_obj = UserLastLogin.objects.get_by_username(user_obj.email)
    info['last_login'] = datetime_to_isoformat_timestr(last_login_obj.last_login) if last_login_obj else ''

    return info
Exemple #39
0
    def get_file_info(self, username, repo_id, file_path):

        file_obj = seafile_api.get_dirent_by_path(repo_id, file_path)
        is_locked, locked_by_me = check_file_lock(repo_id, file_path, username)
        file_info = {
            'type': 'file',
            'repo_id': repo_id,
            'parent_dir': os.path.dirname(file_path),
            'obj_name': file_obj.obj_name,
            'obj_id': file_obj.obj_id,
            'size': file_obj.size,
            'mtime': timestamp_to_isoformat_timestr(file_obj.mtime),
            'is_locked': is_locked,
        }

        return file_info
Exemple #40
0
    def get(self, request, org_id, email):
        """Org admin list user owned repos

        """
        # resource check
        org_id = int(org_id)
        if not ccnet_api.get_org_by_id(org_id):
            error_msg = 'Organization %s not found.' % org_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            err_msg = 'User %s not found.' % email
            return api_error(status.HTTP_404_NOT_FOUND, err_msg)

        # permission check
        if not ccnet_api.org_user_exists(org_id, email):
            err_msg = _('User %s not found in organization.') % email
            return api_error(status.HTTP_404_NOT_FOUND, err_msg)

        # list repos
        repo_info_list = list()
        owned_repos = seafile_api.get_org_owned_repo_list(org_id, email)

        for r in owned_repos:
            # do not return virtual repos
            if r.is_virtual:
                continue

            repo_info = {
                "repo_id": r.id,
                "repo_name": r.name,
                "owner_email": email,
                "owner_name": email2nickname(email),
                "owner_contact_email": email2contact_email(email),
                "last_modified": timestamp_to_isoformat_timestr(r.last_modify),
                "modifier_email": r.last_modifier,
                "size": r.size,
                "encrypted": r.encrypted,
                "permission":
                'rw',  # Always have read-write permission to owned repo
                "status": normalize_repo_status_code(r.status),
            }
            repo_info_list.append(repo_info)

        return Response({'repo_list': repo_info_list})
Exemple #41
0
    def get(self, request, repo_id):
        """ Get dir info.

        Permission checking:
        1. user with either 'r' or 'rw' permission.
        """

        # parameter check
        path = request.GET.get('path', None)
        if not path:
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        path = normalize_dir_path(path)
        if path == '/':
            error_msg = 'path invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        # resource check
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = 'Library %s not found.' % repo_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        dir_id = seafile_api.get_dir_id_by_path(repo_id, path)
        if not dir_id:
            error_msg = 'Folder %s not found.' % path
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # permission check
        permission = check_folder_permission(request, repo_id, path)
        if not permission:
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        dir_obj = seafile_api.get_dirent_by_path(repo_id, path)
        dir_info = {
            'repo_id': repo_id,
            'path': path,
            'name': dir_obj.obj_name if dir_obj else '',
            'mtime':
            timestamp_to_isoformat_timestr(dir_obj.mtime) if dir_obj else '',
            'permission': permission,
        }

        return Response(dir_info)
Exemple #42
0
def get_trash_repo_info(repo):

    result = {}

    owner = repo.owner_id

    result['name'] = repo.repo_name
    result['id'] = repo.repo_id
    result['owner'] = owner
    result['owner_name'] = email2nickname(owner)
    result['delete_time'] = timestamp_to_isoformat_timestr(repo.del_time)

    if '@seafile_group' in owner:
        group_id = get_group_id_by_repo_owner(owner)
        result['group_name'] = group_id_to_name(group_id)

    return result
Exemple #43
0
def get_user_info(email):

    user = User.objects.get(email=email)
    profile = Profile.objects.get_profile_by_user(email)

    info = {}
    info['email'] = email
    info['name'] = email2nickname(email)
    info[
        'contact_email'] = profile.contact_email if profile and profile.contact_email else ''
    info['login_id'] = profile.login_id if profile and profile.login_id else ''

    info['is_staff'] = user.is_staff
    info['is_active'] = user.is_active
    info['reference_id'] = user.reference_id if user.reference_id else ''

    orgs = ccnet_api.get_orgs_by_user(email)
    try:
        if orgs:
            org_id = orgs[0].org_id
            info['org_id'] = org_id
            info['org_name'] = orgs[0].org_name
            info['quota_usage'] = seafile_api.get_org_user_quota_usage(
                org_id, user.email)
            info['quota_total'] = seafile_api.get_org_user_quota(
                org_id, user.email)
        else:
            info['quota_usage'] = seafile_api.get_user_self_usage(user.email)
            info['quota_total'] = seafile_api.get_user_quota(user.email)
    except Exception as e:
        logger.error(e)
        info['quota_usage'] = -1
        info['quota_total'] = -1

    info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)

    info['has_default_device'] = True if default_device(user) else False
    info['is_force_2fa'] = UserOptions.objects.is_force_2fa(email)

    if getattr(settings, 'MULTI_INSTITUTION', False):
        info['institution'] = profile.institution if profile else ''

    info['role'] = get_user_role(user)

    return info
Exemple #44
0
    def get(self, request):
        """List all admins from database and ldap imported
        """
        try:
            db_users = ccnet_api.get_emailusers('DB', -1, -1)
            ldap_imported_users = ccnet_api.get_emailusers('LDAPImport', -1, -1)
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        admin_users = []
        for user in db_users + ldap_imported_users:
            if user.is_staff is True:
                admin_users.append(user)

        admin_users_info = []
        for user in admin_users:
            user_info = {}
            profile = Profile.objects.get_profile_by_user(user.email)
            user_info['email'] = user.email
            user_info['name'] = email2nickname(user.email)
            user_info['contact_email'] = profile.contact_email if profile and profile.contact_email else ''
            user_info['login_id'] = profile.login_id if profile and profile.login_id else ''

            user_info['is_staff'] = user.is_staff
            user_info['is_active'] = user.is_active

            user_info['quota_total'] = seafile_api.get_user_quota(user.email)
            user_info['quota_usage'] = seafile_api.get_user_self_usage(user.email)

            user_info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)
            user_info['last_login'] = UserLastLogin.objects.get_by_username(user.email).last_login if UserLastLogin.objects.get_by_username(user.email) else ''

            try:
                admin_role = AdminRole.objects.get_admin_role(user.email)
                user_info['admin_role'] = admin_role.role
            except AdminRole.DoesNotExist:
                user_info['admin_role'] = DEFAULT_ADMIN
            admin_users_info.append(user_info)

        result = {
            'admin_user_list': admin_users_info,
        }
        return Response(result)
Exemple #45
0
    def get_file_info(self, username, repo_id, file_path):

        repo = seafile_api.get_repo(repo_id)
        file_obj = seafile_api.get_dirent_by_path(repo_id, file_path)
        if file_obj:
            file_name = file_obj.obj_name
            file_size = file_obj.size
            can_preview, error_msg = can_preview_file(file_name, file_size,
                                                      repo)
            can_edit, error_msg = can_edit_file(file_name, file_size, repo)
        else:
            can_preview = False
            can_edit = False

        try:
            is_locked, locked_by_me = check_file_lock(repo_id, file_path,
                                                      username)
        except Exception as e:
            logger.error(e)
            is_locked = False

        file_info = {
            'type':
            'file',
            'repo_id':
            repo_id,
            'parent_dir':
            os.path.dirname(file_path),
            'obj_name':
            file_name,
            'obj_id':
            file_obj.obj_id if file_obj else '',
            'size':
            file_size,
            'mtime':
            timestamp_to_isoformat_timestr(file_obj.mtime) if file_obj else '',
            'is_locked':
            is_locked,
            'can_preview':
            can_preview,
            'can_edit':
            can_edit,
        }

        return file_info
Exemple #46
0
    def get(self, request):
        """list all departments
        """

        try:
            departments = ccnet_api.list_all_departments()
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        try:
            avatar_size = int(request.GET.get('avatar_size', GROUP_AVATAR_DEFAULT_SIZE))
        except ValueError:
            avatar_size = GROUP_AVATAR_DEFAULT_SIZE

        result = []
        for department in departments:
            department = seaserv.get_group(department.id)

            username = request.user.username
            if not is_group_member(department.id, username):
                continue

            try:
                avatar_url, is_default, date_uploaded = api_grp_avatar_url(department.id, avatar_size)
            except Exception as e:
                logger.error(e)
                avatar_url = get_default_group_avatar_url()

            created_at = timestamp_to_isoformat_timestr(department.timestamp)

            department_info = {
                "id": department.id,
                "email": '%s@seafile_group' % str(department.id),
                "parent_group_id": department.parent_group_id,
                "name": department.group_name,
                "owner": department.creator_name,
                "created_at": created_at,
                "avatar_url": request.build_absolute_uri(avatar_url),
            }

            result.append(department_info)

        return Response(result)
Exemple #47
0
 def to_dict(self):
     repo = seafile_api.get_repo(self.repo_id)
     if not repo:
         return None
     commit = seaserv.get_commit(repo.id, repo.revision, self.revision_id)
     email = commit.creator_name
     return  {"tag":self.tag.name,
              "tag_creator": self.username,
              "revision": {
                  "repo_id": self.repo_id,
                  "commit_id": self.revision_id,
                  "email": email,
                  "name": email2nickname(email),
                  "contact_email": email2contact_email(email),
                  "time": timestamp_to_isoformat_timestr(commit.ctime),
                  "description": commit.desc,
                  "link": reverse("repo_history_view", args=[self.repo_id])+"?commit_id=%s"%self.revision_id
                  }}
Exemple #48
0
    def get(self, request, org_id):
        """List organization group
        """
        # resource check
        org_id = int(org_id)
        if not ccnet_api.get_org_by_id(org_id):
            error_msg = 'Organization %s not found.' % org_id
            return api_error(status.HTTP_404_NOT_FOUND, error_msg)

        # Make sure page request is an int. If not, deliver first page.
        try:
            current_page = int(request.GET.get('page', '1'))
            per_page = int(request.GET.get('per_page', '25'))
        except ValueError:
            current_page = 1
            per_page = 25

        groups_plus_one = get_org_groups(org_id, per_page * (current_page - 1),
                                         per_page + 1)
        groups = groups_plus_one[:per_page]

        groups_list = []
        for i in groups:
            group = {}
            group['id'] = i.id
            group['group_name'] = i.group_name
            group['ctime'] = timestamp_to_isoformat_timestr(i.timestamp)
            group['creator_name'] = email2nickname(i.creator_name)
            group['creator_email'] = i.creator_name
            group['creator_contact_email'] = email2contact_email(i.creator_name)
            groups_list.append(group)


        if len(groups_plus_one) == per_page + 1:
            page_next = True
        else:
            page_next = False

        return Response({
                'groups': groups_list,
                'page': current_page,
                'per_page': per_page,
                'page_next': page_next,
                })
Exemple #49
0
def get_file_history_info(commit, avatar_size):

    info = {}

    creator_name = commit.creator_name
    url, is_default, date_uploaded = api_avatar_url(creator_name, avatar_size)

    info['creator_avatar_url'] = url
    info['creator_email'] = creator_name
    info['creator_name'] = email2nickname(creator_name)
    info['creator_contact_email'] = email2contact_email(creator_name)
    info['ctime'] = timestamp_to_isoformat_timestr(commit.ctime)
    info['description'] = commit.desc
    info['commit_id'] = commit.id
    info['size'] = commit.rev_file_size
    info['rev_file_id'] = commit.rev_file_id
    info['rev_renamed_old_path'] = commit.rev_renamed_old_path

    return info
Exemple #50
0
def get_wiki_page_object(wiki_object, page_name):
    page_name = clean_page_name(page_name)
    filepath = "/" + page_name + ".md"
    repo_id = wiki_object.repo_id

    try:
        dirent = seafile_api.get_dirent_by_path(repo_id, filepath)
        if dirent:
            latest_contributor, last_modified = dirent.modifier, dirent.mtime
        else:
            latest_contributor, last_modified = None, 0
    except SearpcError as e:
        logger.error(e)
        latest_contributor, last_modified = None, 0

    try:
        repo = seafile_api.get_repo(wiki_object.repo_id)
    except SearpcError as e:
        logger.error(e)

    file_url = get_inner_file_url(repo, dirent.obj_id, dirent.obj_name)

    edit_url = get_site_scheme_and_netloc().rstrip('/') + "%s?p=%s" % (
        reverse('file_edit', args=[repo_id]),
        urlquote(filepath.encode('utf-8')))

    slug = wiki_object.slug
    page_url = get_site_scheme_and_netloc().rstrip('/') + reverse('wiki:slug',
                                                                  args=[slug, page_name])

    # FIX ME: move to top after wiki code refactor
    from seahub.base.templatetags.seahub_tags import email2nickname, \
        email2contact_email

    return {"name": page_name,
            "link": page_url,
            "file_link": file_url,
            "file_edit_link": edit_url,
            "updated_at": timestamp_to_isoformat_timestr(last_modified),
            "last_modifier": latest_contributor,
            "last_modifier_contact_email": email2contact_email(latest_contributor),
            "last_modifier_name": email2nickname(latest_contributor),
            }
Exemple #51
0
def get_file_history_info(commit, avatar_size):

    info = {}

    creator_name = commit.creator_name
    url, is_default, date_uploaded = api_avatar_url(creator_name, avatar_size)

    info['creator_avatar_url'] = url
    info['creator_email'] = creator_name
    info['creator_name'] = email2nickname(creator_name)
    info['creator_contact_email'] = email2contact_email(creator_name)
    info['ctime'] = timestamp_to_isoformat_timestr(commit.ctime)
    info['description'] = commit.desc
    info['commit_id'] = commit.id
    info['size'] = commit.rev_file_size
    info['rev_file_id'] = commit.rev_file_id
    info['rev_renamed_old_path'] = commit.rev_renamed_old_path

    return info
Exemple #52
0
    def get_info_of_users_order_by_quota_usage(self, org, all_users, direction,
                                               page, per_page):

        # get user's quota usage info
        user_usage_dict = {}
        users_with_usage = seafile_api.list_org_user_quota_usage(org.org_id)
        for user in users_with_usage:
            email = user.user
            if email not in user_usage_dict:
                user_usage_dict[email] = user.usage

        # get all users and map quota usage to user
        for user in all_users:
            email = user.email
            user.quota_usage = user_usage_dict.get(email, 0)

        # sort
        all_users.sort(key=lambda item: item.quota_usage,
                       reverse=direction == 'desc')

        data = []
        for user in all_users[(page - 1) * per_page:page * per_page]:

            info = {}
            info['email'] = user.email
            info['name'] = email2nickname(user.email)
            info['contact_email'] = email2contact_email(user.email)

            info['is_staff'] = user.is_staff
            info['is_active'] = user.is_active
            info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)

            info['quota_usage'] = user.quota_usage
            info['quota_total'] = seafile_api.get_org_user_quota(
                org.org_id, user.email)

            last_login_obj = UserLastLogin.objects.get_by_username(user.email)
            info['last_login'] = datetime_to_isoformat_timestr(
                last_login_obj.last_login) if last_login_obj else ''

            data.append(info)

        return data
Exemple #53
0
def get_wiki_page_object(wiki_object, page_name):
    page_name = clean_page_name(page_name)
    filepath = "/" + page_name + ".md"
    repo_id = wiki_object.repo_id

    try:
        dirent = seafile_api.get_dirent_by_path(repo_id, filepath)
        if dirent:
            latest_contributor, last_modified = dirent.modifier, dirent.mtime
        else:
            latest_contributor, last_modified = None, 0
    except SearpcError as e:
        logger.error(e)
        latest_contributor, last_modified = None, 0

    try:
        repo = seafile_api.get_repo(wiki_object.repo_id)
    except SearpcError as e:
        logger.error(e)

    file_url = get_inner_file_url(repo, dirent.obj_id, dirent.obj_name)

    edit_url = get_site_scheme_and_netloc().rstrip('/') + "%s?p=%s" % (reverse(
        'file_edit', args=[repo_id]), urlquote(filepath.encode('utf-8')))

    slug = wiki_object.slug
    page_url = get_site_scheme_and_netloc().rstrip('/') + reverse(
        'wiki:slug', args=[slug, page_name])

    # FIX ME: move to top after wiki code refactor
    from seahub.base.templatetags.seahub_tags import email2nickname, \
        email2contact_email

    return {
        "name": page_name,
        "link": page_url,
        "file_link": file_url,
        "file_edit_link": edit_url,
        "updated_at": timestamp_to_isoformat_timestr(last_modified),
        "last_modifier": latest_contributor,
        "last_modifier_contact_email": email2contact_email(latest_contributor),
        "last_modifier_name": email2nickname(latest_contributor),
    }
Exemple #54
0
def get_group_info(request, group_id, avatar_size=GROUP_AVATAR_DEFAULT_SIZE):
    group = seaserv.get_group(group_id)
    try:
        avatar_url, is_default, date_uploaded = api_grp_avatar_url(group.id, avatar_size)
    except Exception as e:
        logger.error(e)
        avatar_url = get_default_group_avatar_url()

    isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp)
    group_info = {
        "id": group.id,
        "name": group.group_name,
        "owner": group.creator_name,
        "created_at": isoformat_timestr,
        "avatar_url": request.build_absolute_uri(avatar_url),
        "admins": get_group_admins(group.id),
        "wiki_enabled": is_wiki_mod_enabled_for_group(group_id)
    }

    return group_info
Exemple #55
0
def get_org_info(org):
    org_id = org.org_id

    org_info = {}
    org_info['org_id'] = org_id
    org_info['org_name'] = org.org_name
    org_info['ctime'] = timestamp_to_isoformat_timestr(org.ctime)
    org_info['org_url_prefix'] = org.url_prefix
    org_info['role'] = OrgSettings.objects.get_role_by_org(org)

    creator = org.creator
    org_info['creator_email'] = creator
    org_info['creator_name'] = email2nickname(creator)
    org_info['creator_contact_email'] = email2contact_email(creator)

    org_info['quota'] = seafile_api.get_org_quota(org_id)
    org_info['quota_usage'] = seafile_api.get_org_quota_usage(org_id)

    if ORG_MEMBER_QUOTA_ENABLED:
        org_info['max_user_number'] = OrgMemberQuota.objects.get_quota(org_id)

    return org_info
Exemple #56
0
    def get(self, request, format=None):
        if not is_pro_version():
            error_msg = 'Feature disabled.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        return_results = []
        try:
            device_errors = seafile_api.list_repo_sync_errors()
        except SearpcError as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        for error in device_errors:
            result = {}
            result['email'] = error.email if error.email else ''
            result['name'] = email2nickname(error.email)
            result['device_ip'] = error.peer_ip if error.peer_ip else ''
            result['repo_name'] = error.repo_name if error.repo_name else ''
            result['repo_id'] = error.repo_id if error.repo_id else ''
            result['error_msg'] = error.error_con if error.error_con else ''

            tokens = TokenV2.objects.filter(device_id = error.peer_id)
            if tokens:
                result['device_name'] = tokens[0].device_name
                result['client_version'] = tokens[0].client_version
            else:
                result['device_name'] = ''
                result['client_version'] = ''

            if error.error_time:
                result['error_time'] = timestamp_to_isoformat_timestr(error.error_time)
            else:
                result['error_time'] = ''

            return_results.append(result)

        return Response(return_results)
def get_group_owned_repo_info(request, repo_id):

    repo = seafile_api.get_repo(repo_id)

    repo_info = {}
    repo_info['id'] = repo_id
    repo_info['name'] = repo.name

    repo_info['mtime'] = timestamp_to_isoformat_timestr(repo.last_modified)
    repo_info['size'] = repo.size
    repo_info['encrypted'] = repo.encrypted

    repo_owner = get_repo_owner(request, repo_id)
    repo_info['owner'] = repo_owner

    try:
        group_id = get_group_id_by_repo_owner(repo_owner)
        group = ccnet_api.get_group(int(group_id))
        repo_info['group_name'] = group.group_name
    except Exception as e:
        logger.error(e)
        repo_info['group_name'] = ''

    return repo_info
Exemple #58
0
def get_tagged_files(repo, repo_tag_id):

    # get tagged files
    tagged_file_objs = FileTags.objects.filter(
        repo_tag__id=repo_tag_id).select_related('repo_tag', 'file_uuid')

    tagged_files = defaultdict(list)
    for tagged_file_obj in tagged_file_objs:
        file_tag_id = tagged_file_obj.pk
        parent_path = tagged_file_obj.file_uuid.parent_path
        filename = tagged_file_obj.file_uuid.filename
        file_path = posixpath.join(parent_path, filename)

        tagged_file = dict()
        file_obj = seafile_api.get_dirent_by_path(repo.store_id, file_path)
        if not file_obj:
            exception = "Can't find tagged file. Repo_id: %s, Path: %s." % (repo.id, file_path)
            logger.warning(exception)
            tagged_file["file_deleted"] = True
            tagged_file["file_tag_id"] = file_tag_id
            tagged_file["filename"] = filename
            tagged_files["tagged_files"].append(tagged_file)
            continue

        tagged_file["file_tag_id"] = file_tag_id
        tagged_file["parent_path"] = parent_path
        tagged_file["filename"] = filename
        tagged_file["size"] = file_obj.size
        tagged_file["mtime"] = file_obj.mtime
        tagged_file["last_modified"] = timestamp_to_isoformat_timestr(file_obj.mtime)
        tagged_file["modifier_email"] = file_obj.modifier
        tagged_file["modifier_contact_email"] = email2contact_email(file_obj.modifier)
        tagged_file["modifier_name"] = email2nickname(file_obj.modifier)
        tagged_files["tagged_files"].append(tagged_file)

    return tagged_files
Exemple #59
0
    def get(self, request):
        """ List all groups.
        """

        org_id = None
        username = request.user.username
        if is_org_context(request):
            org_id = request.user.org.org_id
            user_groups = seaserv.get_org_groups_by_user(org_id, username)
        else:
            user_groups = ccnet_api.get_groups(username, return_ancestors=True)

        try:
            avatar_size = int(request.GET.get('avatar_size', GROUP_AVATAR_DEFAULT_SIZE))
        except ValueError:
            avatar_size = GROUP_AVATAR_DEFAULT_SIZE

        try:
            with_repos = int(request.GET.get('with_repos', 0))
        except ValueError:
            with_repos = 0

        if with_repos not in (0, 1):
            error_msg = 'with_repos invalid.'
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        groups = []
        if with_repos:
            gids = [g.id for g in user_groups]
            admin_info = ExtraGroupsSharePermission.objects.batch_get_repos_with_admin_permission(gids)

            try:
                starred_repos = UserStarredFiles.objects.get_starred_repos_by_user(username)
                starred_repo_id_list = [item.repo_id for item in starred_repos]
            except Exception as e:
                logger.error(e)
                starred_repo_id_list = []

        for g in user_groups:
            group_info = get_group_info(request, g.id, avatar_size)

            if with_repos:
                if org_id:
                    group_repos = seafile_api.get_org_group_repos(org_id, g.id)
                else:
                    group_repos = seafile_api.get_repos_by_group(g.id)

                repos = []

                # get repo id owner dict
                all_repo_owner = []
                repo_id_owner_dict = {}
                for repo in group_repos:
                    repo_id = repo.id
                    if repo_id not in repo_id_owner_dict:
                        repo_owner = get_repo_owner(request, repo_id)
                        all_repo_owner.append(repo_owner)
                        repo_id_owner_dict[repo_id] = repo_owner

                # Use dict to reduce memcache fetch cost in large for-loop.
                name_dict = {}
                contact_email_dict = {}

                for email in all_repo_owner:

                    if email not in name_dict:
                        if '@seafile_group' in email:
                            group_id = get_group_id_by_repo_owner(email)
                            group_name= group_id_to_name(group_id)
                            name_dict[email] = group_name
                        else:
                            name_dict[email] = email2nickname(email)

                    if email not in contact_email_dict:
                        if '@seafile_group' in email:
                            contact_email_dict[email] = ''
                        else:
                            contact_email_dict[email] = email2contact_email(email)

                for r in group_repos:
                    repo_owner = repo_id_owner_dict.get(r.id, r.user)
                    repo = {
                        "id": r.id,
                        "repo_id": r.id,
                        "name": r.name,
                        "repo_name": r.name,
                        "size": r.size,
                        "size_formatted": filesizeformat(r.size),
                        "mtime": r.last_modified,
                        "mtime_relative": translate_seahub_time(r.last_modified),
                        "last_modified": timestamp_to_isoformat_timestr(r.last_modified),
                        "encrypted": r.encrypted,
                        "permission": r.permission,
                        "owner": repo_owner,
                        "owner_email": repo_owner,
                        "owner_name": name_dict.get(repo_owner, ''),
                        "owner_contact_email": contact_email_dict.get(repo_owner, ''),
                        "is_admin": (r.id, g.id) in admin_info,
                        "starred": r.repo_id in starred_repo_id_list,
                    }
                    repos.append(repo)

                group_info['repos'] = repos

            groups.append(group_info)

        return Response(groups)
Exemple #60
0
    def get(self, request):
        """ Return repos user can access.

        Permission checking:
        1. all authenticated user can perform this action.
        """

        filter_by = {
            'mine': False,
            'shared': False,
            'group': False,
            'public': False,
        }

        request_type_list = request.GET.getlist('type', "")
        if not request_type_list:
            # set all to True, no filter applied
            filter_by = filter_by.fromkeys(filter_by.iterkeys(), True)

        for request_type in request_type_list:
            request_type = request_type.strip()
            filter_by[request_type] = True

        email = request.user.username

        # Use dict to reduce memcache fetch cost in large for-loop.
        contact_email_dict = {}
        nickname_dict = {}

        org_id = None
        if is_org_context(request):
            org_id = request.user.org.org_id

        try:
            starred_repos = UserStarredFiles.objects.get_starred_repos_by_user(email)
            starred_repo_id_list = [item.repo_id for item in starred_repos]
        except Exception as e:
            logger.error(e)
            starred_repo_id_list = []

        repo_info_list = []
        if filter_by['mine']:

            if org_id:
                owned_repos = seafile_api.get_org_owned_repo_list(org_id,
                        email, ret_corrupted=True)
            else:
                owned_repos = seafile_api.get_owned_repo_list(email,
                        ret_corrupted=True)

            # Reduce memcache fetch ops.
            modifiers_set = set([x.last_modifier for x in owned_repos])
            for e in modifiers_set:
                if e not in contact_email_dict:
                    contact_email_dict[e] = email2contact_email(e)
                if e not in nickname_dict:
                    nickname_dict[e] = email2nickname(e)

            owned_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify))
            for r in owned_repos:

                # do not return virtual repos
                if r.is_virtual:
                    continue

                repo_info = {
                    "type": "mine",
                    "repo_id": r.id,
                    "repo_name": r.name,
                    "owner_email": email,
                    "owner_name": email2nickname(email),
                    "owner_contact_email": email2contact_email(email),
                    "last_modified": timestamp_to_isoformat_timestr(r.last_modify),
                    "modifier_email": r.last_modifier,
                    "modifier_name": nickname_dict.get(r.last_modifier, ''),
                    "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
                    "size": r.size,
                    "encrypted": r.encrypted,
                    "permission": 'rw',  # Always have read-write permission to owned repo
                    "starred": r.repo_id in starred_repo_id_list,
                }

                if is_pro_version() and ENABLE_STORAGE_CLASSES:
                    repo_info['storage_name'] = r.storage_name
                    repo_info['storage_id'] = r.storage_id

                repo_info_list.append(repo_info)

        if filter_by['shared']:

            if org_id:
                shared_repos = seafile_api.get_org_share_in_repo_list(org_id,
                        email, -1, -1)
            else:
                shared_repos = seafile_api.get_share_in_repo_list(
                        email, -1, -1)

            repos_with_admin_share_to = ExtraSharePermission.objects.\
                    get_repos_with_admin_permission(email)

            # Reduce memcache fetch ops.
            owners_set = set([x.user for x in shared_repos])
            modifiers_set = set([x.last_modifier for x in shared_repos])
            for e in owners_set | modifiers_set:
                if e not in contact_email_dict:
                    contact_email_dict[e] = email2contact_email(e)
                if e not in nickname_dict:
                    nickname_dict[e] = email2nickname(e)

            shared_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify))
            for r in shared_repos:

                owner_email = r.user

                group_name = ''
                is_group_owned_repo = False
                if '@seafile_group' in owner_email:
                    is_group_owned_repo = True
                    group_id = get_group_id_by_repo_owner(owner_email)
                    group_name= group_id_to_name(group_id)

                owner_name = group_name if is_group_owned_repo else \
                        nickname_dict.get(owner_email, '')
                owner_contact_email = '' if is_group_owned_repo else \
                        contact_email_dict.get(owner_email, '')

                repo_info = {
                    "type": "shared",
                    "repo_id": r.repo_id,
                    "repo_name": r.repo_name,
                    "last_modified": timestamp_to_isoformat_timestr(r.last_modify),
                    "modifier_email": r.last_modifier,
                    "modifier_name": nickname_dict.get(r.last_modifier, ''),
                    "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
                    "owner_email": owner_email,
                    "owner_name": owner_name,
                    "owner_contact_email": owner_contact_email,
                    "size": r.size,
                    "encrypted": r.encrypted,
                    "permission": r.permission,
                    "starred": r.repo_id in starred_repo_id_list,
                }

                if r.repo_id in repos_with_admin_share_to:
                    repo_info['is_admin'] = True
                else:
                    repo_info['is_admin'] = False

                repo_info_list.append(repo_info)

        if filter_by['group']:

            if org_id:
                group_repos = seafile_api.get_org_group_repos_by_user(email, org_id)
            else:
                group_repos = seafile_api.get_group_repos_by_user(email)

            group_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify))

            # Reduce memcache fetch ops.
            share_from_set = set([x.user for x in group_repos])
            modifiers_set = set([x.last_modifier for x in group_repos])
            for e in modifiers_set | share_from_set:
                if e not in contact_email_dict:
                    contact_email_dict[e] = email2contact_email(e)
                if e not in nickname_dict:
                    nickname_dict[e] = email2nickname(e)

            for r in group_repos:
                repo_info = {
                    "type": "group",
                    "group_id": r.group_id,
                    "group_name": r.group_name,
                    "repo_id": r.repo_id,
                    "repo_name": r.repo_name,
                    "last_modified": timestamp_to_isoformat_timestr(r.last_modify),
                    "modifier_email": r.last_modifier,
                    "modifier_name": nickname_dict.get(r.last_modifier, ''),
                    "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
                    "size": r.size,
                    "encrypted": r.encrypted,
                    "permission": r.permission,
                    "starred": r.repo_id in starred_repo_id_list,
                }
                repo_info_list.append(repo_info)

        if filter_by['public'] and request.user.permissions.can_view_org():
            public_repos = list_inner_pub_repos(request)

            # get repo id owner dict
            all_repo_owner = []
            repo_id_owner_dict = {}
            for repo in public_repos:
                repo_id = repo.repo_id
                if repo_id not in repo_id_owner_dict:
                    repo_owner = get_repo_owner(request, repo_id)
                    all_repo_owner.append(repo_owner)
                    repo_id_owner_dict[repo_id] = repo_owner

            # Reduce memcache fetch ops.
            owner_set = set(all_repo_owner)
            share_from_set = set([x.user for x in public_repos])
            modifiers_set = set([x.last_modifier for x in public_repos])
            for e in modifiers_set | share_from_set | owner_set:
                if e not in contact_email_dict:
                    contact_email_dict[e] = email2contact_email(e)
                if e not in nickname_dict:
                    nickname_dict[e] = email2nickname(e)

            for r in public_repos:
                repo_owner = repo_id_owner_dict[r.repo_id]
                repo_info = {
                    "type": "public",
                    "repo_id": r.repo_id,
                    "repo_name": r.repo_name,
                    "last_modified": timestamp_to_isoformat_timestr(r.last_modify),
                    "modifier_email": r.last_modifier,
                    "modifier_name": nickname_dict.get(r.last_modifier, ''),
                    "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
                    "owner_email": repo_owner,
                    "owner_name": nickname_dict.get(repo_owner, ''),
                    "owner_contact_email": contact_email_dict.get(repo_owner, ''),
                    "size": r.size,
                    "encrypted": r.encrypted,
                    "permission": r.permission,
                    "starred": r.repo_id in starred_repo_id_list,
                }
                repo_info_list.append(repo_info)

        utc_dt = datetime.datetime.utcnow()
        timestamp = utc_dt.strftime('%Y-%m-%d %H:%M:%S')
        org_id = request.user.org.org_id if is_org_context(request) else -1
        try:
            send_message('seahub.stats', 'user-login\t%s\t%s\t%s' % (email, timestamp, org_id))
        except Exception as e:
            logger.error('Error when sending user-login message: %s' % str(e))

        return Response({'repos': repo_info_list})