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:
Veiko Aasa 2019-10-20 15:46:00 +00:00 committed by Sunil Mohan Adapa
parent d01b04d218
commit a17749ce3e
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
3 changed files with 25 additions and 21 deletions

View File

@ -91,36 +91,31 @@ class GitwebApp(app_module.App):
'gitweb-freedombox-auth')
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):
"""Change the login_required property of shortcut."""
shortcut = self.remove('shortcut-gitweb')
shortcut.login_required = login_required
self.add(shortcut)
def update_repo_list(self):
def get_repo_list(self):
"""List all Git repositories and set Gitweb as public or private."""
repos = []
if os.path.exists(GIT_REPO_PATH):
for repo in os.listdir(GIT_REPO_PATH):
if not repo.endswith('.git'):
continue
private_file = os.path.join(GIT_REPO_PATH, repo, 'private')
access = 'public'
if os.path.exists(private_file):
access = 'private'
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
if self.have_public_repos():
def update_service_access(self):
"""Update the frontpage shortcut and webserver auth requirement."""
repos = self.get_repo_list()
if have_public_repos(repos):
self._enable_public_access()
else:
self._disable_public_access()
@ -149,7 +144,8 @@ class GitwebWebserverAuth(Webserver):
def is_enabled(self):
"""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():
@ -159,16 +155,16 @@ def init():
register_group(group)
setup_helper = globals()['setup_helper']
if setup_helper.get_state() != 'needs-setup' and app.is_enabled():
app.update_repo_list()
app.set_enabled(True)
if setup_helper.get_state() != 'needs-setup':
app.update_service_access()
if app.is_enabled():
app.set_enabled(True)
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.install(managed_packages)
helper.call('post', actions.superuser_run, 'gitweb', ['setup'])
app.update_repo_list()
helper.call('post', app.enable)
@ -182,6 +178,11 @@ def diagnose():
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):
"""Create a new repository by calling the action script."""
args = [

View File

@ -62,7 +62,7 @@ class EditRepoForm(forms.Form):
if (not name) or name.startswith(('-', '.')):
raise ValidationError(_('Invalid repository name.'))
for repo in gitweb.app.repos:
for repo in gitweb.app.get_repo_list():
if name == repo['name']:
raise ValidationError(
_('A repository with this name already exists.'))

View File

@ -48,8 +48,7 @@ class GitwebAppView(views.AppView):
def get_context_data(self, *args, **kwargs):
"""Add repositories to the context data."""
context = super().get_context_data(*args, **kwargs)
gitweb.app.update_repo_list()
context['repos'] = gitweb.app.repos
context['repos'] = gitweb.app.get_repo_list()
return context
@ -79,6 +78,8 @@ class CreateRepoView(SuccessMessageMixin, FormView):
gitweb.create_repo(form_data['name'], form_data['description'],
form_data['owner'], form_data['is_private'])
gitweb.app.update_service_access()
return super().form_valid(form)
@ -100,7 +101,7 @@ class EditRepoView(SuccessMessageMixin, FormView):
def get_initial(self):
"""Load information about repository being edited."""
name = self.kwargs['name']
for repo in gitweb.app.repos:
for repo in gitweb.app.get_repo_list():
if repo['name'] == name:
break
else:
@ -123,6 +124,7 @@ class EditRepoView(SuccessMessageMixin, FormView):
except ActionError:
messages.error(self.request,
_('An error occurred during configuration.'))
gitweb.app.update_service_access()
return super().form_valid(form)
@ -133,7 +135,7 @@ def delete(request, name):
On GET, display a confirmation page.
On POST, delete the repository.
"""
for repo in gitweb.app.repos:
for repo in gitweb.app.get_repo_list():
if repo['name'] == name:
break
else:
@ -149,6 +151,7 @@ def delete(request, name):
_('Could not delete {name}: {error}').format(
name=name, error=error),
)
gitweb.app.update_service_access()
return redirect(reverse_lazy('gitweb:index'))