mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
gitweb: update repository list where necessary
Closes #1663 Signed-off-by: Veiko Aasa <veiko17@disroot.org> [sunil@medhas.org Add docstring to update_service_access method] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
d01b04d218
commit
a17749ce3e
@ -91,36 +91,31 @@ class GitwebApp(app_module.App):
|
|||||||
'gitweb-freedombox-auth')
|
'gitweb-freedombox-auth')
|
||||||
self.add(self.auth_webserver)
|
self.add(self.auth_webserver)
|
||||||
|
|
||||||
def have_public_repos(self):
|
|
||||||
"""If Gitweb have public repos."""
|
|
||||||
return any((repo['access'] == 'public' for repo in self.repos))
|
|
||||||
|
|
||||||
def set_shortcut_login_required(self, login_required):
|
def set_shortcut_login_required(self, login_required):
|
||||||
"""Change the login_required property of shortcut."""
|
"""Change the login_required property of shortcut."""
|
||||||
shortcut = self.remove('shortcut-gitweb')
|
shortcut = self.remove('shortcut-gitweb')
|
||||||
shortcut.login_required = login_required
|
shortcut.login_required = login_required
|
||||||
self.add(shortcut)
|
self.add(shortcut)
|
||||||
|
|
||||||
def update_repo_list(self):
|
def get_repo_list(self):
|
||||||
"""List all Git repositories and set Gitweb as public or private."""
|
"""List all Git repositories and set Gitweb as public or private."""
|
||||||
repos = []
|
repos = []
|
||||||
if os.path.exists(GIT_REPO_PATH):
|
if os.path.exists(GIT_REPO_PATH):
|
||||||
for repo in os.listdir(GIT_REPO_PATH):
|
for repo in os.listdir(GIT_REPO_PATH):
|
||||||
if not repo.endswith('.git'):
|
if not repo.endswith('.git'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
private_file = os.path.join(GIT_REPO_PATH, repo, 'private')
|
private_file = os.path.join(GIT_REPO_PATH, repo, 'private')
|
||||||
access = 'public'
|
access = 'public'
|
||||||
if os.path.exists(private_file):
|
if os.path.exists(private_file):
|
||||||
access = 'private'
|
access = 'private'
|
||||||
|
|
||||||
repos.append({'name': repo[:-4], 'access': access})
|
repos.append({'name': repo[:-4], 'access': access})
|
||||||
|
|
||||||
repos = sorted(repos, key=lambda repo: repo['name'])
|
return sorted(repos, key=lambda repo: repo['name'])
|
||||||
|
|
||||||
self.repos = repos
|
def update_service_access(self):
|
||||||
|
"""Update the frontpage shortcut and webserver auth requirement."""
|
||||||
if self.have_public_repos():
|
repos = self.get_repo_list()
|
||||||
|
if have_public_repos(repos):
|
||||||
self._enable_public_access()
|
self._enable_public_access()
|
||||||
else:
|
else:
|
||||||
self._disable_public_access()
|
self._disable_public_access()
|
||||||
@ -149,7 +144,8 @@ class GitwebWebserverAuth(Webserver):
|
|||||||
|
|
||||||
def is_enabled(self):
|
def is_enabled(self):
|
||||||
"""Return if configuration is enabled or public access is enabled."""
|
"""Return if configuration is enabled or public access is enabled."""
|
||||||
return app.have_public_repos() or super().is_enabled()
|
repos = app.get_repo_list()
|
||||||
|
return have_public_repos(repos) or super().is_enabled()
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
@ -159,16 +155,16 @@ def init():
|
|||||||
register_group(group)
|
register_group(group)
|
||||||
|
|
||||||
setup_helper = globals()['setup_helper']
|
setup_helper = globals()['setup_helper']
|
||||||
if setup_helper.get_state() != 'needs-setup' and app.is_enabled():
|
if setup_helper.get_state() != 'needs-setup':
|
||||||
app.update_repo_list()
|
app.update_service_access()
|
||||||
app.set_enabled(True)
|
if app.is_enabled():
|
||||||
|
app.set_enabled(True)
|
||||||
|
|
||||||
|
|
||||||
def setup(helper, old_version=None):
|
def setup(helper, old_version=None):
|
||||||
"""Install and configure the module."""
|
"""Install and configure the module."""
|
||||||
helper.install(managed_packages)
|
helper.install(managed_packages)
|
||||||
helper.call('post', actions.superuser_run, 'gitweb', ['setup'])
|
helper.call('post', actions.superuser_run, 'gitweb', ['setup'])
|
||||||
app.update_repo_list()
|
|
||||||
helper.call('post', app.enable)
|
helper.call('post', app.enable)
|
||||||
|
|
||||||
|
|
||||||
@ -182,6 +178,11 @@ def diagnose():
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def have_public_repos(repos):
|
||||||
|
"""Check for public repositories"""
|
||||||
|
return any((repo['access'] == 'public' for repo in repos))
|
||||||
|
|
||||||
|
|
||||||
def create_repo(repo, repo_description, owner, is_private):
|
def create_repo(repo, repo_description, owner, is_private):
|
||||||
"""Create a new repository by calling the action script."""
|
"""Create a new repository by calling the action script."""
|
||||||
args = [
|
args = [
|
||||||
|
|||||||
@ -62,7 +62,7 @@ class EditRepoForm(forms.Form):
|
|||||||
if (not name) or name.startswith(('-', '.')):
|
if (not name) or name.startswith(('-', '.')):
|
||||||
raise ValidationError(_('Invalid repository name.'))
|
raise ValidationError(_('Invalid repository name.'))
|
||||||
|
|
||||||
for repo in gitweb.app.repos:
|
for repo in gitweb.app.get_repo_list():
|
||||||
if name == repo['name']:
|
if name == repo['name']:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_('A repository with this name already exists.'))
|
_('A repository with this name already exists.'))
|
||||||
|
|||||||
@ -48,8 +48,7 @@ class GitwebAppView(views.AppView):
|
|||||||
def get_context_data(self, *args, **kwargs):
|
def get_context_data(self, *args, **kwargs):
|
||||||
"""Add repositories to the context data."""
|
"""Add repositories to the context data."""
|
||||||
context = super().get_context_data(*args, **kwargs)
|
context = super().get_context_data(*args, **kwargs)
|
||||||
gitweb.app.update_repo_list()
|
context['repos'] = gitweb.app.get_repo_list()
|
||||||
context['repos'] = gitweb.app.repos
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
@ -79,6 +78,8 @@ class CreateRepoView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
gitweb.create_repo(form_data['name'], form_data['description'],
|
gitweb.create_repo(form_data['name'], form_data['description'],
|
||||||
form_data['owner'], form_data['is_private'])
|
form_data['owner'], form_data['is_private'])
|
||||||
|
gitweb.app.update_service_access()
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
@ -100,7 +101,7 @@ class EditRepoView(SuccessMessageMixin, FormView):
|
|||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
"""Load information about repository being edited."""
|
"""Load information about repository being edited."""
|
||||||
name = self.kwargs['name']
|
name = self.kwargs['name']
|
||||||
for repo in gitweb.app.repos:
|
for repo in gitweb.app.get_repo_list():
|
||||||
if repo['name'] == name:
|
if repo['name'] == name:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -123,6 +124,7 @@ class EditRepoView(SuccessMessageMixin, FormView):
|
|||||||
except ActionError:
|
except ActionError:
|
||||||
messages.error(self.request,
|
messages.error(self.request,
|
||||||
_('An error occurred during configuration.'))
|
_('An error occurred during configuration.'))
|
||||||
|
gitweb.app.update_service_access()
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
@ -133,7 +135,7 @@ def delete(request, name):
|
|||||||
On GET, display a confirmation page.
|
On GET, display a confirmation page.
|
||||||
On POST, delete the repository.
|
On POST, delete the repository.
|
||||||
"""
|
"""
|
||||||
for repo in gitweb.app.repos:
|
for repo in gitweb.app.get_repo_list():
|
||||||
if repo['name'] == name:
|
if repo['name'] == name:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -149,6 +151,7 @@ def delete(request, name):
|
|||||||
_('Could not delete {name}: {error}').format(
|
_('Could not delete {name}: {error}').format(
|
||||||
name=name, error=error),
|
name=name, error=error),
|
||||||
)
|
)
|
||||||
|
gitweb.app.update_service_access()
|
||||||
|
|
||||||
return redirect(reverse_lazy('gitweb:index'))
|
return redirect(reverse_lazy('gitweb:index'))
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user