def sys_group_admin_export_excel(request): """ Export all groups to excel """ next_page = request.META.get('HTTP_REFERER', None) if not next_page: next_page = SITE_ROOT try: groups = ccnet_threaded_rpc.get_all_groups(-1, -1) except Exception as e: logger.error(e) messages.error(request, _('Failed to export Excel')) return HttpResponseRedirect(next_page) head = [_("Name"), _("Creator"), _("Create At")] data_list = [] for grp in groups: create_at = tsstr_sec(grp.timestamp) if grp.timestamp else '' row = [grp.group_name, grp.creator_name, create_at] data_list.append(row) wb = write_xls('groups', head, data_list) if not wb: messages.error(request, _('Failed to export Excel')) return HttpResponseRedirect(next_page) response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename=groups.xlsx' wb.save(response) return response
def repo_history_changes(request, repo_id): changes = {} content_type = 'application/json; charset=utf-8' repo = get_repo(repo_id) if not repo: err_msg = _('Library does not exist.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # perm check if check_folder_permission(request, repo_id, '/') is None: if request.user.is_staff is True: pass # Allow system staff to check repo changes else: err_msg = _("Permission denied") return HttpResponse(json.dumps({"error": err_msg}), status=403, content_type=content_type) username = request.user.username try: server_crypto = UserOptions.objects.is_server_crypto(username) except CryptoOptionNotSetError: # Assume server_crypto is ``False`` if this option is not set. server_crypto = False if repo.encrypted and \ (repo.enc_version == 1 or (repo.enc_version == 2 and server_crypto)) \ and not seafile_api.is_password_set(repo_id, username): err_msg = _('Library is encrypted.') return HttpResponse(json.dumps({'error': err_msg}), status=403, content_type=content_type) commit_id = request.GET.get('commit_id', '') if not commit_id: err_msg = _('Argument missing') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) changes = get_diff(repo_id, '', commit_id) c = get_commit(repo.id, repo.version, commit_id) if c.parent_id is None: # A commit is a first commit only if it's parent id is None. changes['cmt_desc'] = repo.desc elif c.second_parent_id is None: # Normal commit only has one parent. if c.desc.startswith('Changed library'): changes['cmt_desc'] = _('Changed library name or description') else: # A commit is a merge only if it has two parents. changes['cmt_desc'] = _('No conflict in the merge.') changes['date_time'] = tsstr_sec(c.ctime) return HttpResponse(json.dumps(changes), content_type=content_type)
def handle(self, *args, **options): self.stdout.write("Export users to '../users.xlsx'.") try: users = ccnet_api.get_emailusers('DB', -1, -1) + \ ccnet_api.get_emailusers('LDAPImport', -1, -1) except Exception as e: self.stdout.write('Error: ' + str(e)) return if is_pro_version(): is_pro = True else: is_pro = False if is_pro: head = [ _("Email"), _("Name"), _("Contact Email"), _("Status"), _("Role"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"), ] else: head = [ _("Email"), _("Name"), _("Contact Email"), _("Status"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"), ] # only operate 100 users for every `for` loop looped = 0 limit = 100 data_list = [] while looped < len(users): current_users = users[looped:looped + limit] last_logins = UserLastLogin.objects.filter(username__in=[x.email \ for x in current_users]) user_profiles = Profile.objects.filter(user__in=[x.email \ for x in current_users]) for user in current_users: # populate name and contact email user.contact_email = '' user.name = '' for profile in user_profiles: if profile.user == user.email: user.contact_email = profile.contact_email user.name = profile.nickname # populate space usage and quota MB = get_file_size_unit('MB') # populate user quota usage orgs = ccnet_api.get_orgs_by_user(user.email) try: if orgs: user.org = orgs[0] org_id = user.org.org_id user.space_usage = seafile_api.get_org_user_quota_usage( org_id, user.email) user.space_quota = seafile_api.get_org_user_quota( org_id, user.email) else: user.space_usage = seafile_api.get_user_self_usage( user.email) user.space_quota = seafile_api.get_user_quota( user.email) except Exception as e: self.stdout.write('Debug: ' + str(e)) user.space_usage = -1 user.space_quota = -1 if user.space_usage > 0: try: space_usage_MB = round(float(user.space_usage) / MB, 2) except Exception as e: self.stdout.write('Debug: ' + str(e)) space_usage_MB = '--' else: space_usage_MB = '' if user.space_quota > 0: try: space_quota_MB = round(float(user.space_quota) / MB, 2) except Exception as e: self.stdout.write('Debug: ' + str(e)) space_quota_MB = '--' else: space_quota_MB = '' # populate user last login time user.last_login = None for last_login in last_logins: if last_login.username == user.email: user.last_login = last_login.last_login if user.is_active: status = _('Active') else: status = _('Inactive') create_at = tsstr_sec(user.ctime) if user.ctime else '' last_login = user.last_login.strftime("%Y-%m-%d %H:%M:%S") if \ user.last_login else '' is_admin = _('Yes') if user.is_staff else '' ldap_import = _('Yes') if user.source == 'LDAPImport' else '' if is_pro: if user.role: if user.role == GUEST_USER: role = _('Guest') elif user.role == DEFAULT_USER: role = _('Default') else: role = user.role else: role = _('Default') row = [ user.email, user.name, user.contact_email, status, role, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import ] else: row = [ user.email, user.name, user.contact_email, status, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import ] data_list.append(row) # update `looped` value when `for` loop finished looped += limit wb = write_xls('users', head, data_list) if not wb: self.stdout.write('Error: please check the log.') return wb.save('../users.xlsx') self.stdout.write('Done.\n')
def sys_useradmin_export_excel(request): """ Export all users from database to excel """ next_page = request.META.get('HTTP_REFERER', None) if not next_page: next_page = SITE_ROOT try: users = ccnet_api.get_emailusers('DB', -1, -1) + \ ccnet_api.get_emailusers('LDAPImport', -1, -1) except Exception as e: logger.error(e) messages.error(request, _('Failed to export Excel')) return HttpResponseRedirect(next_page) if is_pro_version(): is_pro = True else: is_pro = False if is_pro: head = [ _("Email"), _("Name"), _("Contact Email"), _("Status"), _("Role"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"), ] else: head = [ _("Email"), _("Name"), _("Contact Email"), _("Status"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"), ] # only operate 100 users for every `for` loop looped = 0 limit = 100 data_list = [] while looped < len(users): current_users = users[looped:looped + limit] last_logins = UserLastLogin.objects.filter(username__in=[x.email \ for x in current_users]) user_profiles = Profile.objects.filter(user__in=[x.email \ for x in current_users]) for user in current_users: # populate name and contact email user.contact_email = '' user.name = '' for profile in user_profiles: if profile.user == user.email: user.contact_email = profile.contact_email user.name = profile.nickname # populate space usage and quota MB = get_file_size_unit('MB') _populate_user_quota_usage(user) if user.space_usage > 0: try: space_usage_MB = round(float(user.space_usage) / MB, 2) except Exception as e: logger.error(e) space_usage_MB = '--' else: space_usage_MB = '' if user.space_quota > 0: try: space_quota_MB = round(float(user.space_quota) / MB, 2) except Exception as e: logger.error(e) space_quota_MB = '--' else: space_quota_MB = '' # populate user last login time user.last_login = None for last_login in last_logins: if last_login.username == user.email: user.last_login = last_login.last_login if user.is_active: status = _('Active') else: status = _('Inactive') create_at = tsstr_sec(user.ctime) if user.ctime else '' last_login = user.last_login.strftime("%Y-%m-%d %H:%M:%S") if \ user.last_login else '' is_admin = _('Yes') if user.is_staff else '' ldap_import = _('Yes') if user.source == 'LDAPImport' else '' if is_pro: if user.role: if user.role == GUEST_USER: role = _('Guest') elif user.role == DEFAULT_USER: role = _('Default') else: role = user.role else: role = _('Default') row = [ user.email, user.name, user.contact_email, status, role, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import ] else: row = [ user.email, user.name, user.contact_email, status, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import ] data_list.append(row) # update `looped` value when `for` loop finished looped += limit wb = write_xls('users', head, data_list) if not wb: messages.error(request, _('Failed to export Excel')) return HttpResponseRedirect(next_page) response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename=users.xlsx' wb.save(response) return response
def handle(self, *args, **options): self.stdout.write("Export users to '../users.xlsx'.") try: users = ccnet_api.get_emailusers('DB', -1, -1) + \ ccnet_api.get_emailusers('LDAPImport', -1, -1) except Exception as e: self.stdout.write('Error: ' + str(e)) return if is_pro_version(): is_pro = True else: is_pro = False if is_pro: head = [_("Email"), _("Name"), _("Contact Email"), _("Status"), _("Role"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"),] else: head = [_("Email"), _("Name"), _("Contact Email"), _("Status"), _("Space Usage") + "(MB)", _("Space Quota") + "(MB)", _("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"),] # only operate 100 users for every `for` loop looped = 0 limit = 100 data_list = [] while looped < len(users): current_users = users[looped:looped+limit] last_logins = UserLastLogin.objects.filter(username__in=[x.email \ for x in current_users]) user_profiles = Profile.objects.filter(user__in=[x.email \ for x in current_users]) for user in current_users: # populate name and contact email user.contact_email = '' user.name = '' for profile in user_profiles: if profile.user == user.email: user.contact_email = profile.contact_email user.name = profile.nickname # populate space usage and quota MB = get_file_size_unit('MB') # populate user quota usage orgs = ccnet_api.get_orgs_by_user(user.email) try: if orgs: user.org = orgs[0] org_id = user.org.org_id user.space_usage = seafile_api.get_org_user_quota_usage(org_id, user.email) user.space_quota = seafile_api.get_org_user_quota(org_id, user.email) else: user.space_usage = seafile_api.get_user_self_usage(user.email) user.space_quota = seafile_api.get_user_quota(user.email) except Exception as e: self.stdout.write('Debug: ' + str(e)) user.space_usage = -1 user.space_quota = -1 if user.space_usage > 0: try: space_usage_MB = round(float(user.space_usage) / MB, 2) except Exception as e: self.stdout.write('Debug: ' + str(e)) space_usage_MB = '--' else: space_usage_MB = '' if user.space_quota > 0: try: space_quota_MB = round(float(user.space_quota) / MB, 2) except Exception as e: self.stdout.write('Debug: ' + str(e)) space_quota_MB = '--' else: space_quota_MB = '' # populate user last login time user.last_login = None for last_login in last_logins: if last_login.username == user.email: user.last_login = last_login.last_login if user.is_active: status = _('Active') else: status = _('Inactive') create_at = tsstr_sec(user.ctime) if user.ctime else '' last_login = user.last_login.strftime("%Y-%m-%d %H:%M:%S") if \ user.last_login else '' is_admin = _('Yes') if user.is_staff else '' ldap_import = _('Yes') if user.source == 'LDAPImport' else '' if is_pro: if user.role: if user.role == GUEST_USER: role = _('Guest') elif user.role == DEFAULT_USER: role = _('Default') else: role = user.role else: role = _('Default') row = [user.email, user.name, user.contact_email, status, role, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import] else: row = [user.email, user.name, user.contact_email, status, space_usage_MB, space_quota_MB, create_at, last_login, is_admin, ldap_import] data_list.append(row) # update `looped` value when `for` loop finished looped += limit wb = write_xls('users', head, data_list) if not wb: self.stdout.write('Error: please check the log.') return wb.save('../users.xlsx') self.stdout.write('Done.\n')