Ejemplo n.º 1
0
 def create_repository(self, repo_cls, name='test_repo', author=None, states='qualif prod'):
     assert issubclass(repo_cls, RepositoryModel)
     if author is None:
         author = User.objects.get_or_create(username='******')[0]
     repo = Repository(author=author, name=name, on_index=True, archive_type=repo_cls.archive_type, is_private=False)
     repo.save()
     for state in states.split():
         ArchiveState(repository=repo, name=state, author=author).save()
     return repo
Ejemplo n.º 2
0
 def index_file(self, request, rid, filename, mimetype):
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     uid = self.storage_uid % repo.id
     key = storage(settings.STORAGE_CACHE).uid_to_key(uid)
     return sendpath(settings.STORAGE_CACHE, key, filename, mimetype)
Ejemplo n.º 3
0
 def xr_search(request, rpc_args, self, rid, repo_slug, state_slug=None):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     state = get_object_or_404(ArchiveState, repository=repo, name=state_slug) if state_slug else None
     global_and = len(rpc_args) == 0 or rpc_args[1] != 'or'
     if len(rpc_args) == 0 or not isinstance(rpc_args[0], dict):
         raise PermissionDenied
     filters = None
     for query_name, attr_name in (('name', 'archive'), ('version', 'version'), ('home_page', 'official_link'),
                                   ('description', 'long_description'),):
         if query_name in rpc_args[0]:
             value = rpc_args[0][query_name]
             if isinstance(value, list):
                 if value:
                     value = value[0]
                 else:
                     value = ''
             value = value.replace('-', '').replace('_', '')
             sub_query = Q(**{attr_name + '__icontains': value})
             if filters is None:
                 filters = sub_query
             elif global_and:
                 filters = filters and sub_query
             else:
                 filters = filters or sub_query
     query = Element.objects.filter(repository=repo)
     if state:
         query = query.filter(states=state)
     if filters is not None:
         query = query.filter(filters)
     res = [{'name': x.archive, 'version': x.version, 'summary': x.long_description,
             '_pypi_ordering': 1.0} for x in query]
     return res
Ejemplo n.º 4
0
def add_element(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)

    class ElementForm(forms.Form):
        package = forms.FileField(label=_('Package'))
        states = forms.ModelMultipleChoiceField(repo.archivestate_set.all(), label=_('States'))

        def clean(self):
            data_ = super().clean()
            if 'package' in self.cleaned_data:
                if not repo.get_model().is_file_valid(self.cleaned_data['package']):
                    raise ValidationError(_('This repository is unable to handle this file.'))
            return data_

    if request.method == 'POST':
        form = ElementForm(request.POST, files=request.FILES)
        if form.is_valid():
            try:
                element = generic_add_element(request, repo, form.cleaned_data['package'],
                                              [x.name for x in form.cleaned_data['states']])
                messages.info(request, _('The package %(n)s has been successfully uploaded.') % {'n': element.filename})
            except InvalidRepositoryException as e:
                messages.error(request, _('Unable to add the package to this repository: %(msg)s.') % {'msg': str(e)})
            return HttpResponseRedirect(reverse('moneta:add_element', kwargs={'rid': rid}))
    else:
        form = ElementForm()
    template_values = {'form': form, 'repo': repo, 'upload_allowed': repo.upload_allowed(request)}
    return render_to_response('moneta/add_package.html', template_values, RequestContext(request))
Ejemplo n.º 5
0
    def index(self, request, rid):
        repo = get_object_or_404(Repository.reader_queryset(request),
                                 id=rid,
                                 archive_type=self.archive_type)
        states = list(
            ArchiveState.objects.filter(repository=repo).order_by('name'))
        template_values = {
            'repo': repo,
            'states': states,
            'upload_allowed': repo.upload_allowed(request),
            'admin_allowed': repo.admin_allowed(request),
        }
        state_infos = []
        template_values['state_slug'] = None
        request_context = RequestContext(request)
        viewname = moneta_url(repo, 'browse')
        url = reverse(viewname,
                      kwargs={
                          'rid': repo.id,
                          'repo_slug': repo.slug,
                      })

        state_infos.append(('all-packages', url, _('All states'), states))
        for state in states:
            template_values['state_slug'] = state.slug
            url = reverse(viewname,
                          kwargs={
                              'rid': repo.id,
                              'repo_slug': repo.slug,
                              'state_slug': state.slug
                          })
            state_infos.append((state.slug, url, state.name, [state]))
        template_values['state_infos'] = state_infos
        return render_to_response('repositories/flat_files/index.html',
                                  template_values, request_context)
Ejemplo n.º 6
0
 def index(self, request, rid):
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     states = list(
         ArchiveState.objects.filter(repository=repo).order_by('name'))
     template_values = {
         'repo': repo,
         'states': states,
         'upload_allowed': repo.upload_allowed(request),
         'admin_allowed': repo.admin_allowed(request),
     }
     state_infos = []
     template_values['state_slug'] = None
     request_context = RequestContext(request)
     setting_str = render_to_string(
         'repositories/maven3/maven_settings.xml', template_values,
         request_context)
     state_infos.append(
         ('all-packages', str(setting_str), _('All states'), states))
     for state in states:
         template_values['state_slug'] = state.slug
         setting_str = render_to_string(
             'repositories/maven3/maven_settings.xml', template_values,
             request_context)
         state_infos.append(
             (state.slug, str(setting_str), state.name, [state]))
     template_values['state_infos'] = state_infos
     return render_to_response('repositories/maven3/index.html',
                               template_values, request_context)
Ejemplo n.º 7
0
    def browse_repository(self, request: HttpRequest, rid, repo_slug, query_string='', state_slug=None) -> HttpResponse:
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        result = self.browse_repo_inner(rid, query_string, state_slug=state_slug)
        if isinstance(result, str):  # sha1/sha256/md5
            return HttpResponse(result)
        elif isinstance(result, Element):
            return get_file(request, eid=result.pk, element=result)
        assert isinstance(result, dict)

        new_query_string = ''
        bread_crumbs = [(_('Root'), self.get_browse_url(repo, new_query_string, state_slug=state_slug))]
        while len(result) == 1 and isinstance(result, dict):
            path_component, result = result.popitem()
            new_query_string += path_component + '/'
            bread_crumbs.append((path_component, self.get_browse_url(repo, new_query_string, state_slug=state_slug)))
        if isinstance(result, set):
            url_list = []
            for elt in result:
                new_gavf_elt_filename = new_query_string + elt.filename
                elt_url = self.get_browse_url(repo, new_gavf_elt_filename, state_slug=state_slug)
                url_list += [(new_gavf_elt_filename, elt_url, elt.id),
                             (new_gavf_elt_filename + '.sha1', elt_url + '.sha1', None),
                             (new_gavf_elt_filename + '.sha256', elt_url + '.sha256', None),
                             (new_gavf_elt_filename + '.md5', elt_url + '.md5', None),
                             ]
        else:
            assert isinstance(result, dict)
            url_list = [(new_query_string + key,
                         self.get_browse_url(repo, new_query_string + key, state_slug=state_slug), None)
                        for key in result]
        template_values = {'repo': repo, 'upload_allowed': repo.upload_allowed(request), 'repo_slug': repo_slug,
                           'admin': True, 'paths': url_list, 'request_path': new_query_string, 'state_slug': state_slug,
                           'bread_crumbs': bread_crumbs, }
        status_code = 200 if url_list else 404
        return TemplateResponse(request, 'repositories/maven3/browse.html', template_values, status=status_code)
Ejemplo n.º 8
0
 def quick_gem_specs(self,
                     request,
                     rid,
                     repo_slug,
                     state_slug=None,
                     filename=None,
                     compression=''):
     name, sep, version = filename.rpartition('-')
     if sep != '-':
         raise Http404
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState,
                                   repository=repo,
                                   name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, name=name, version=version)
     content = self.marshal_data(element)
     if compression == '.rz':
         content = zlib.compress(content)
     return HttpResponse(content, content_type='application/gzip')
Ejemplo n.º 9
0
 def folder_index(self, request, rid, repo_slug, state_slug, folder):
     """
     Return a HttpResponse
     :param request: HttpRequest
     :raise:
     """
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     state = get_object_or_404(ArchiveState,
                               repository=repo,
                               slug=state_slug)
     q = Element.objects.filter(repository__id=rid,
                                archive__startswith=folder,
                                states=state)
     element_query = q.select_related()[0:100]
     element_count = q.count()
     template_values = {
         'repo': repo,
         'state': state_slug,
         'element_count': element_count,
         'elements': element_query,
         'folder': folder,
         'upload_allowed': repo.upload_allowed(request),
     }
     return render_to_response('repositories/aptitude/folder_index.html',
                               template_values, RequestContext(request))
Ejemplo n.º 10
0
 def index(self, request, rid, repo_slug=None, state_slug=None):
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     states = list(
         ArchiveState.objects.filter(repository=repo).order_by('name'))
     template_values = {
         'repo': repo,
         'states': states,
         'upload_allowed': repo.upload_allowed(request),
         'admin_allowed': repo.admin_allowed(request),
     }
     view_name = moneta_url(repo, 'index')
     tab_infos = [
         (reverse(view_name,
                  kwargs={
                      'rid': repo.id,
                      'repo_slug': repo.slug
                  }), states,
          ArchiveState(name=_('All states'), slug='all-states')),
     ]
     for state in states:
         tab_infos.append((reverse(view_name,
                                   kwargs={
                                       'rid': repo.id,
                                       'repo_slug': repo.slug,
                                       'state_slug': state.slug
                                   }), [state], state))
     template_values['tab_infos'] = tab_infos
     return render_to_response('repositories/ruby/index.html',
                               template_values, RequestContext(request))
Ejemplo n.º 11
0
    def index(self, request, rid, repo_slug=None, state_slug=''):
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        base_query = Element.objects.filter(repository=repo)
        if state_slug:
            state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
            base_query = base_query.filter(states=state)
        base_query = base_query.order_by('archive', 'version')
        element_infos = []
        for element in base_query:
            if not element.extra_data:
                continue
            provider = json.loads(element.extra_data)['provider']
            element_infos.append(
                (element.name,
                 reverse('vagrant:get_box',
                         kwargs={'rid': rid, 'repo_slug': repo.slug, 'eid': element.id, 'provider': provider,
                                 'archive': element.archive, 'version': element.version}),
                 element.sha1)
            )
        states = [state for state in ArchiveState.objects.filter(repository=repo).order_by('name')]
        tab_infos = [(reverse('vagrant:index', kwargs={'rid': repo.id, 'state_slug': ''}),
                      ArchiveState(name=_('All states'), slug='all-states'), states, ''), ]
        tab_infos += [(reverse('vagrant:index',
                               kwargs={'rid': repo.id, 'state_slug': state.slug}),
                       state, [state], state.slug)
                      for state in states]
        # list of (relative URL, name, list of states, state_slug)

        template_values = {'repo': repo, 'states': states, 'upload_allowed': repo.upload_allowed(request),
                           'state_slug': state_slug, 'elements': element_infos,
                           'index_url': reverse(moneta_url(repo, 'index'), kwargs={'rid': repo.id, }),
                           'tab_infos': tab_infos, 'admin_allowed': repo.admin_allowed(request), }
        return render_to_response(self.index_html, template_values, RequestContext(request))
Ejemplo n.º 12
0
def modify_repository(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    author = None if request.user.is_anonymous() else request.user

    if request.method == 'POST':
        form = RepositoryUpdateForm(request.POST)
        if form.is_valid():
            new_state_names = set(form.cleaned_data['states'].split())
            old_state_names = {x.name for x in repo.archivestate_set.all()}
            for name in new_state_names - old_state_names:  # new states
                ArchiveState(name=name, repository=repo, author=author).save()
            repo.on_index = form.cleaned_data['on_index']
            repo.is_private = form.cleaned_data['is_private']
            repo.save()
            repo.admin_group.clear()
            for group in form.cleaned_data['admin_group']:
                repo.admin_group.add(group)
            # repo.reader_group.clear()
            # for group in form.cleaned_data['reader_group']:
            #     repo.reader_group.add(group)
            removed_states = ArchiveState.objects.filter(name__in=old_state_names - new_state_names, repository=repo)
            # noinspection PyUnresolvedReferences
            Element.states.through.objects.filter(archivestate__in=removed_states).delete()
            removed_states.delete()
            messages.info(request, _('The repository %(repo)s has been modified.') % {'repo': repo.name})
            return HttpResponseRedirect(reverse('moneta:modify_repository', kwargs={'rid': rid, }))
    else:
        form = RepositoryUpdateForm(initial={'on_index': repo.on_index, 'is_private': repo.is_private,
                                             'reader_group': list(repo.reader_group.all()),
                                             'states': ' '.join([x.name for x in repo.archivestate_set.all()]),
                                             'admin_group': list(repo.admin_group.all())})
    template_values = {'form': form, 'repo': repo, 'upload_allowed': repo.upload_allowed(request)}
    return render_to_response('moneta/modify_repo.html', template_values, RequestContext(request))
Ejemplo n.º 13
0
 def xr_search(request, rpc_args, self, rid, repo_slug, state_slug=None):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     state = get_object_or_404(ArchiveState, repository=repo, name=state_slug) if state_slug else None
     global_and = len(rpc_args) == 0 or rpc_args[1] != 'or'
     if len(rpc_args) == 0 or not isinstance(rpc_args[0], dict):
         raise PermissionDenied
     filters = None
     for query_name, attr_name in (('name', 'archive'), ('version', 'version'), ('home_page', 'official_link'),
                                   ('description', 'long_description'),):
         if query_name in rpc_args[0]:
             value = rpc_args[0][query_name]
             if isinstance(value, list):
                 if value:
                     value = value[0]
                 else:
                     value = ''
             value = value.replace('-', '').replace('_', '')
             sub_query = Q(**{attr_name + '__icontains': value})
             if filters is None:
                 filters = sub_query
             elif global_and:
                 filters = filters and sub_query
             else:
                 filters = filters or sub_query
     query = Element.objects.filter(repository=repo)
     if state:
         query = query.filter(states=state)
     if filters is not None:
         query = query.filter(filters)
     res = [{'name': x.archive, 'version': x.version, 'summary': x.long_description,
             '_pypi_ordering': 1.0} for x in query]
     return res
Ejemplo n.º 14
0
    def get_providers_by_version(self, request, rid, state_slug, archive):
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        base_query = Element.objects.filter(repository=repo, archive=archive)
        if state_slug:
            state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
            base_query = base_query.filter(states=state)
        versions = {}
        for element in base_query:
            metadata = json.loads(element.extra_data)
            versions.setdefault(element.version, []).append({
                'name': metadata['provider'],
                'url': '%s%s' % (settings.SERVER_NAME, element.get_direct_link()),
                'checksum_type': 'sha1',

                'checksum': element.sha1})
        result = {'name': archive, 'description': _('This box contains %(name)s') % {'name': archive},
                  'versions': [{'version': k, 'status': 'active', 'providers': v} for (k, v) in versions.items()]}
        return result
# Vagrant.configure(2) do |config|
#
#   config.vm.box = "{$relativePathInfo|escape}"
#   config.vm.box_url = '<a href="{$CATALOG_URI|escape}{$pathInfo|escape}">{$CATALOG_URI|escape}{$pathInfo|escape}</a>'
#
#   # Whatever other config stuff you want to do
# end
Ejemplo n.º 15
0
    def browse_repository(self, request: HttpRequest, rid, repo_slug, query_string='', state_slug=None) -> HttpResponse:
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        result = self.browse_repo_inner(rid, query_string, state_slug=state_slug)
        if isinstance(result, str):  # sha1/sha256/md5
            return HttpResponse(result)
        elif isinstance(result, Element):
            return get_file(request, eid=result.pk, element=result)
        assert isinstance(result, dict)

        new_query_string = ''
        bread_crumbs = [(_('Root'), self.get_browse_url(repo, new_query_string, state_slug=state_slug))]
        while len(result) == 1 and isinstance(result, dict):
            path_component, result = result.popitem()
            new_query_string += path_component + '/'
            bread_crumbs.append((path_component, self.get_browse_url(repo, new_query_string, state_slug=state_slug)))
        if isinstance(result, set):
            url_list = []
            for elt in result:
                new_gavf_elt_filename = new_query_string + elt.filename
                elt_url = self.get_browse_url(repo, new_gavf_elt_filename, state_slug=state_slug)
                url_list += [(new_gavf_elt_filename, elt_url, elt.id),
                             (new_gavf_elt_filename + '.sha1', elt_url + '.sha1', None),
                             (new_gavf_elt_filename + '.sha256', elt_url + '.sha256', None),
                             (new_gavf_elt_filename + '.md5', elt_url + '.md5', None),
                             ]
        else:
            assert isinstance(result, dict)
            url_list = [(new_query_string + key,
                         self.get_browse_url(repo, new_query_string + key, state_slug=state_slug), None)
                        for key in result]
        template_values = {'repo': repo, 'upload_allowed': repo.upload_allowed(request), 'repo_slug': repo_slug,
                           'admin': True, 'paths': url_list, 'request_path': new_query_string, 'state_slug': state_slug,
                           'bread_crumbs': bread_crumbs, }
        status_code = 200 if url_list else 404
        return TemplateResponse(request, 'repositories/maven3/browse.html', template_values, status=status_code)
Ejemplo n.º 16
0
    def index(self, request, rid):
        repo = get_object_or_404(Repository.reader_queryset(request),
                                 id=rid,
                                 archive_type=self.archive_type)
        states = [
            state for state in ArchiveState.objects.filter(
                repository=repo).order_by('name')
        ]
        tab_infos = [
            (states, ArchiveState(name=_('All states'), slug='all-states')),
        ]
        tab_infos += [([state], state) for state in states]

        template_values = {
            'repo':
            repo,
            'states':
            states,
            'upload_allowed':
            repo.upload_allowed(request),
            'index_url':
            reverse(moneta_url(repo, 'index'), kwargs={
                'rid': repo.id,
            }),
            'tab_infos':
            tab_infos,
            'admin_allowed':
            repo.admin_allowed(request),
        }
        return render_to_response(self.index_html, template_values,
                                  RequestContext(request))
Ejemplo n.º 17
0
 def gpg_key(self, request, rid, repo_slug=None, slug=None):
     get_object_or_404(Repository.reader_queryset(request),
                       id=rid,
                       archive_type=self.archive_type)
     signature = GPGSigner().export_key()
     # noinspection PyUnusedLocal
     repo_slug, slug = slug, repo_slug
     return HttpResponse(signature, content_type="text/plain")
Ejemplo n.º 18
0
 def create_repository(self,
                       repo_cls,
                       name='test_repo',
                       author=None,
                       states='qualif prod'):
     assert issubclass(repo_cls, RepositoryModel)
     if author is None:
         author = User.objects.get_or_create(username='******')[0]
     repo = Repository(author=author,
                       name=name,
                       on_index=True,
                       archive_type=repo_cls.archive_type,
                       is_private=False)
     repo.save()
     for state in states.split():
         ArchiveState(repository=repo, name=state, author=author).save()
     return repo
Ejemplo n.º 19
0
def index(request: HttpRequest):
    repositories = Repository.index_queryset(request).annotate(
        package_count=Count('element'))
    if not request.user.has_perm('repository.add_repository'):
        form = None
    elif request.method == 'POST':
        form = get_repository_form()(request.POST)
        if form.is_valid():
            author = None if request.user.is_anonymous() else request.user
            repo = Repository(author=author,
                              name=form.cleaned_data['name'],
                              on_index=form.cleaned_data['on_index'],
                              archive_type=form.cleaned_data['archive_type'],
                              is_private=form.cleaned_data['is_private'])
            repo.save()
            for group in form.cleaned_data['admin_group']:
                repo.admin_group.add(group)
            for state in set(form.cleaned_data['states'].split()):
                ArchiveState(repository=repo, name=state, author=author).save()
            messages.info(request, _('Your new repository has been created.'))
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = get_repository_form()()
    # compute repos with admin rights
    upload_ids = {x.id for x in Repository.upload_queryset(request)}
    admin_ids = {x.id for x in Repository.admin_queryset(request)}
    template_values = {
        'repositories': repositories,
        'form': form,
        'request': request,
        'upload_ids': upload_ids,
        'admin_ids': admin_ids,
    }
    return render_to_response('moneta/index.html', template_values,
                              RequestContext(request))
Ejemplo n.º 20
0
    def index(self, request, rid):
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        states = [state for state in ArchiveState.objects.filter(repository=repo).order_by('name')]
        tab_infos = [(states, ArchiveState(name=_('All states'), slug='all-states')), ]
        tab_infos += [([state], state) for state in states]

        template_values = {'repo': repo, 'states': states, 'upload_allowed': repo.upload_allowed(request),
                           'index_url': reverse(moneta_url(repo, 'index'), kwargs={'rid': repo.id, }),
                           'tab_infos': tab_infos, 'admin_allowed': repo.admin_allowed(request), }
        return TemplateResponse(request, self.index_html, template_values)
Ejemplo n.º 21
0
 def plugin_index(self, request: HttpRequest, rid, repo_slug, state_slug=None):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     return TemplateResponse(request, 'repositories/jetbrains/updatePlugins.xml', {'elements': base_query},
                             content_type='application/xml')
Ejemplo n.º 22
0
 def download(self, request, rid, repo_slug, state_slug=None, filename=None):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, filename=filename)
     from moneta.views import get_file
     return get_file(request, element.pk)
Ejemplo n.º 23
0
 def specs(self, request, rid, repo_slug, state_slug=None, filename='specs.4.8.gz'):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     if state_slug:
         filename = 'specs/%(slug)s/%(filename)s' % {'slug': state_slug, 'filename': filename, }
     else:
         filename = 'specs/%(filename)s' % {'filename': filename, }
     uid = self.storage_uid % repo.pk
     key = storage(settings.STORAGE_CACHE).uid_to_key(uid)
     return sendpath(settings.STORAGE_CACHE, key, filename, 'application/gzip')
Ejemplo n.º 24
0
 def xr_package_releases(request, rpc_args, self, rid, repo_slug, state_slug=None):
     if len(rpc_args) == 0:
         raise PermissionDenied
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, states=state, archive=rpc_args[0])
     else:
         query = Element.objects.filter(repository=repo, archive=rpc_args[0])
     versions = [LooseVersion(x.version) for x in query]
     versions.sort()
     return [str(x) for x in versions]
Ejemplo n.º 25
0
 def xr_package_releases(request, rpc_args, self, rid, repo_slug, state_slug=None):
     if len(rpc_args) == 0:
         raise PermissionDenied
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, states=state, archive=rpc_args[0])
     else:
         query = Element.objects.filter(repository=repo, archive=rpc_args[0])
     versions = [LooseVersion(x.version) for x in query]
     versions.sort()
     return [str(x) for x in versions]
Ejemplo n.º 26
0
def search_package(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.reader_queryset(request), id=rid)
    repo_states = list(ArchiveState.objects.filter(repository=repo))

    class ElementSearchForm(forms.Form):
        search = forms.CharField(label=_('Search'),
                                 help_text=_('Type your search'),
                                 max_length=200,
                                 required=False)
        states = forms.ModelMultipleChoiceField(
            label=_('Selected states'),
            required=False,
            initial=repo_states,
            queryset=ArchiveState.objects.filter(repository=repo),
        )
        states.help_text = ''

    if request.GET.get('search') is not None:
        form = ElementSearchForm(request.GET)
    else:
        form = ElementSearchForm()
    query = Element.objects.filter(repository=repo)
    search_pattern = ''
    if form.is_valid():
        search_pattern = form.cleaned_data['search']
        if search_pattern:
            query = query.filter(full_name__icontains=search_pattern)
        if form.cleaned_data['states']:
            query = query.filter(
                states__in=form.cleaned_data['states']).distinct()
    else:
        query = query.filter(states__in=repo_states).distinct()

    paginator = Paginator(query.order_by('full_name'), 25)
    page = request.GET.get('page')

    try:
        elements = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        elements = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        elements = paginator.page(paginator.num_pages)
    template_values = {
        'elements': elements,
        'repo': repo,
        'pattern': search_pattern,
        'form': form,
        'upload_allowed': repo.upload_allowed(request)
    }
    return render_to_response('moneta/search_repo.html', template_values,
                              RequestContext(request))
Ejemplo n.º 27
0
def index(request: HttpRequest):
    repositories = Repository.index_queryset(request).annotate(package_count=Count('element'))
    if not request.user.has_perm('repository.add_repository'):
        form = None
    elif request.method == 'POST':
        form = get_repository_form()(request.POST)
        if form.is_valid():
            author = None if request.user.is_anonymous() else request.user
            repo = Repository(author=author, name=form.cleaned_data['name'], on_index=form.cleaned_data['on_index'],
                              archive_type=form.cleaned_data['archive_type'],
                              is_private=form.cleaned_data['is_private'])
            repo.save()
            for group in form.cleaned_data['admin_group']:
                repo.admin_group.add(group)
            for state in set(form.cleaned_data['states'].split()):
                ArchiveState(repository=repo, name=state, author=author).save()
            messages.info(request, _('Your new repository has been created.'))
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = get_repository_form()()
    # compute repos with admin rights
    upload_ids = {x.id for x in Repository.upload_queryset(request)}
    admin_ids = {x.id for x in Repository.admin_queryset(request)}
    template_values = {'repositories': repositories, 'form': form, 'request': request,
                       'upload_ids': upload_ids, 'admin_ids': admin_ids, }
    return render_to_response('moneta/index.html', template_values, RequestContext(request))
Ejemplo n.º 28
0
def modify_repository(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    # noinspection PyUnresolvedReferences
    author = None if request.user.is_anonymous else request.user

    if request.method == 'POST':
        form = RepositoryUpdateForm(request.POST)
        if form.is_valid():
            new_state_names = set(form.cleaned_data['states'].split())
            old_state_names = {x.name for x in repo.archivestate_set.all()}
            for name in new_state_names - old_state_names:  # new states
                ArchiveState(name=name, repository=repo, author=author).save()
            repo.on_index = form.cleaned_data['on_index']
            repo.is_private = form.cleaned_data['is_private']
            repo.save()
            repo.admin_group.clear()
            for group in form.cleaned_data['admin_group']:
                repo.admin_group.add(group)
            # repo.reader_group.clear()
            # for group in form.cleaned_data['reader_group']:
            #     repo.reader_group.add(group)
            removed_states = ArchiveState.objects.filter(
                name__in=old_state_names - new_state_names, repository=repo)
            # noinspection PyUnresolvedReferences
            Element.states.through.objects.filter(
                archivestate__in=removed_states).delete()
            removed_states.delete()
            messages.info(
                request,
                _('The repository %(repo)s has been modified.') %
                {'repo': repo.name})
            return HttpResponseRedirect(
                reverse('moneta:modify_repository', kwargs={
                    'rid': rid,
                }))
    else:
        form = RepositoryUpdateForm(
            initial={
                'on_index': repo.on_index,
                'is_private': repo.is_private,
                'reader_group': list(repo.reader_group.all()),
                'states': ' '.join(
                    [x.name for x in repo.archivestate_set.all()]),
                'admin_group': list(repo.admin_group.all())
            })
    template_values = {
        'form': form,
        'repo': repo,
        'upload_allowed': repo.upload_allowed(request)
    }
    return TemplateResponse(request, 'moneta/modify_repo.html',
                            template_values)
Ejemplo n.º 29
0
 def simple(self, request, rid, repo_slug, state_slug=None, search_pattern=''):
     search_pattern = search_pattern.replace('-', '').replace('_', '')
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     if search_pattern:
         base_query = base_query.filter(archive__iexact=search_pattern)
     view_name = moneta_url(repo, 'get_file')
     elements = [(x.filename, x.md5, reverse(view_name, kwargs={'eid': x.id, })) for x in base_query[0:1000]]
     template_values = {'elements': elements, 'rid': rid, }
     return TemplateResponse(request, 'repositories/pypi/simple.html', template_values)
Ejemplo n.º 30
0
 def gem_specs(self, request, rid, repo_slug, state_slug=None, filename=None):
     name, sep, version = filename.rpartition('-')
     if sep != '-':
         raise Http404
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, name=name, version=version)
     return HttpResponse(self.yaml_data(element), content_type='text/yaml')
Ejemplo n.º 31
0
 def simple(self, request, rid, repo_slug, state_slug=None, search_pattern=''):
     search_pattern = search_pattern.replace('-', '').replace('_', '')
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     if search_pattern:
         base_query = base_query.filter(archive__iexact=search_pattern)
     view_name = moneta_url(repo, 'get_file')
     elements = [(x.filename, x.md5, reverse(view_name, kwargs={'eid': x.id, })) for x in base_query[0:1000]]
     template_values = {'elements': elements, 'rid': rid, }
     return TemplateResponse(request, 'repositories/pypi/simple.html', template_values)
Ejemplo n.º 32
0
 def repodata_file(self, request, rid, repo_slug, state_slug, arch, filename, compression):
     if filename not in ('comps.xml', 'primary.xml', 'other.xml', 'filelists.xml', 'repomd.xml', ):
         return HttpResponse(_('File not found'), status=404)
     if compression and filename == 'repomd.xml':
         return HttpResponse(_('File not found'), status=404)
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     filename = self.index_filename(state_slug, arch, filename + compression)
     mimetype = 'text/xml'
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     uid = self.storage_uid % repo.id
     key = storage(settings.STORAGE_CACHE).uid_to_key(uid)
     return sendpath(settings.STORAGE_CACHE, key, filename, mimetype)
Ejemplo n.º 33
0
 def get_box(self, request: HttpRequest, rid, repo_slug, eid, archive,
             version, provider):
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     elements = list(Element.objects.filter(repository=repo, pk=eid)[0:1])
     if not elements:
         return JsonResponse({
             'errors': [
                 'Not found',
             ],
         })
     return get_file(request, eid, element=elements[0])
Ejemplo n.º 34
0
 def get_file(self, request, rid, repo_slug, state_slug, folder, filename):
     """
     Return a HttpResponse
     :param request: HttpRequest
     :raise:
     """
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     q = Element.objects.filter(repository__id=rid, filename=filename)[0:1]
     q = list(q)
     if len(q) == 0:
         raise Http404
     element = q[0]
     return get_file(request, element.id, compression=None, path='', element=element, name=None)
Ejemplo n.º 35
0
 def setUpClass(cls):
     super().setUpClass()
     cls.repo = Repository(archive_type=Maven3.archive_type,
                           name='test_maven3')
     cls.repo.save()
     cls.elts = [
         Element(
             version='1.1.4c',
             archive='xpp3.xpp3_min',
             name='xpp3_min',
             filename='xpp3_min-1.1.4c.jar',
             full_name='xpp3_min-1.1.4c.jar',
             sha1='19d4e90b43059058f6e056f794f0ea4030d60b86',
         ),
         Element(
             version='1.0-2',
             archive='javax.xml.stream.stax-api',
             name='stax-api',
             filename='stax-api-1.0-2.jar',
             full_name='stax-api-1.0-2.jar',
             sha1='d6337b0de8b25e53e81b922352fbea9f9f57ba0b',
         ),
         Element(
             version='1.0-2',
             archive='javax.xml.stream.stax-api',
             name='stax-api',
             filename='stax-api-1.0-2.pom',
             full_name='stax-api-1.0-2.pom',
             sha1='5379b69f557c5ab7c144d22bf7c3768bd2adb93d',
         ),
         Element(
             version='2.2.2',
             archive='javax.xml.bind.jaxb-api',
             name='jaxb-api',
             filename='jaxb-api-2.2.2.jar',
             full_name='jaxb-api-2.2.2.jar',
             sha1='aeb3021ca93dde265796d82015beecdcff95bf09',
         ),
         Element(
             version='2.2.2',
             archive='javax.xml.bind.jaxb-api',
             name='jaxb-api',
             filename='jaxb-api-2.2.2.pom',
             full_name='jaxb-api-2.2.2.pom',
             sha1='a8368234f7555dd64d3a9060a0b02e6c215694fb',
         ),
     ]
     for elt in cls.elts:
         elt.repository = cls.repo
     Element.objects.bulk_create(cls.elts)
Ejemplo n.º 36
0
 def xr_list_packages(request, rpc_args, self, rid, repo_slug, state_slug=None):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, states=state).order_by('name')
     else:
         query = Element.objects.filter(repository=repo).order_by('name')
     results = []
     prev = None
     for value in query:
         if value.name != prev:
             results.append(value.name)
             prev = value.name
     return results
Ejemplo n.º 37
0
 def xr_list_packages(request, rpc_args, self, rid, repo_slug, state_slug=None):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, states=state).order_by('name')
     else:
         query = Element.objects.filter(repository=repo).order_by('name')
     results = []
     prev = None
     for value in query:
         if value.name != prev:
             results.append(value.name)
             prev = value.name
     return results
Ejemplo n.º 38
0
 def get_box(self, request: HttpRequest, rid, repo_slug, eid, archive, version, provider):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     # noinspection PyUnusedLocal
     archive = archive
     # noinspection PyUnusedLocal
     provider = provider
     # noinspection PyUnusedLocal
     version = version
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     elements = list(Element.objects.filter(repository=repo, pk=eid)[0:1])
     if not elements:
         return JsonResponse({'errors': ['Not found', ], })
     return get_file(request, eid, element=elements[0])
Ejemplo n.º 39
0
def delete_element(request: HttpRequest, rid, eid):
    from moneta.repository.forms import DeleteRepositoryForm

    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    element = get_object_or_404(Element.objects.filter(repository=repo, id=eid))
    if request.method == 'POST':
        form = DeleteRepositoryForm(request.POST)
        if form.is_valid():
            element.delete()
            messages.warning(request, _('The package %(repo)s has been deleted.') % {'repo': element.full_name})
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = DeleteRepositoryForm()
    template_values = {'form': form, 'repo': repo, 'element': element}
    return render_to_response('moneta/delete_element.html', template_values, RequestContext(request))
Ejemplo n.º 40
0
def delete_repository(request: HttpRequest, rid):
    from moneta.repository.forms import DeleteRepositoryForm

    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    if request.method == 'POST':
        form = DeleteRepositoryForm(request.POST)
        if form.is_valid():
            for element in Element.objects.filter(repository=repo):
                element.delete()
            repo.delete()
            messages.warning(request, _('The repository %(repo)s has been deleted.') % {'repo': repo.name})
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = DeleteRepositoryForm()
    template_values = {'form': form, 'repo': repo}
    return TemplateResponse(request, 'moneta/delete_repo.html', template_values)
Ejemplo n.º 41
0
 def quick_gem_specs(self, request, rid, repo_slug, state_slug=None, filename=None, compression=''):
     name, sep, version = filename.rpartition('-')
     if sep != '-':
         raise Http404
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, name=name, version=version)
     content = self.marshal_data(element)
     if compression == '.rz':
         content = zlib.compress(content)
     return HttpResponse(content, content_type='application/gzip')
Ejemplo n.º 42
0
 def folder_index(self, request, rid, repo_slug, state_slug, folder):
     """
     Return a HttpResponse
     :param request: HttpRequest
     :raise:
     """
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     state = get_object_or_404(ArchiveState, repository=repo, slug=state_slug)
     q = Element.objects.filter(repository__id=rid, archive__startswith=folder, states=state)
     element_query = q.select_related()[0:100]
     element_count = q.count()
     template_values = {'repo': repo, 'state': state_slug, 'element_count': element_count,
                        'elements': element_query, 'folder': folder,
                        'upload_allowed': repo.upload_allowed(request), }
     return render_to_response('repositories/aptitude/folder_index.html', template_values,
                               RequestContext(request))
Ejemplo n.º 43
0
 def index(self, request, rid):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     states = list(ArchiveState.objects.filter(repository=repo).order_by('name'))
     template_values = {'repo': repo, 'states': states, 'upload_allowed': repo.upload_allowed(request),
                        'admin_allowed': repo.admin_allowed(request), }
     view_name = moneta_url(repo, 'simple')
     tab_infos = [
         (reverse(view_name, kwargs={'rid': repo.id, 'repo_slug': repo.slug}), states,
          ArchiveState(name=_('All states'), slug='all-states')),
     ]
     for state in states:
         tab_infos.append((reverse(view_name,
                                   kwargs={'rid': repo.id, 'repo_slug': repo.slug, 'state_slug': state.slug}),
                           [state], state))
     template_values['tab_infos'] = tab_infos
     return TemplateResponse(request, 'repositories/pypi/index.html', template_values)
Ejemplo n.º 44
0
 def index(self, request, rid):
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
     states = list(ArchiveState.objects.filter(repository=repo).order_by('name'))
     template_values = {'repo': repo, 'states': states, 'upload_allowed': repo.upload_allowed(request),
                        'admin_allowed': repo.admin_allowed(request), }
     state_infos = []
     template_values['state_slug'] = None
     request_context = RequestContext(request)
     setting_str = render_to_string('repositories/maven3/maven_settings.xml', template_values, request_context)
     state_infos.append(('all-packages', str(setting_str), _('All states'), states))
     for state in states:
         template_values['state_slug'] = state.slug
         setting_str = render_to_string('repositories/maven3/maven_settings.xml', template_values, request_context)
         state_infos.append((state.slug, str(setting_str), state.name, [state]))
     template_values['state_infos'] = state_infos
     return TemplateResponse(request, 'repositories/maven3/index.html', template_values)
Ejemplo n.º 45
0
    def index(self, request, rid):
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        states = list(ArchiveState.objects.filter(repository=repo).order_by('name'))
        template_values = {'repo': repo, 'states': states, 'upload_allowed': repo.upload_allowed(request),
                           'admin_allowed': repo.admin_allowed(request), }
        state_infos = []
        template_values['state_slug'] = None
        viewname = moneta_url(repo, 'browse')
        url = reverse(viewname, kwargs={'rid': repo.id, 'repo_slug': repo.slug, })

        state_infos.append(('all-packages', url, _('All states'), states))
        for state in states:
            template_values['state_slug'] = state.slug
            url = reverse(viewname, kwargs={'rid': repo.id, 'repo_slug': repo.slug, 'state_slug': state.slug})
            state_infos.append((state.slug, url, state.name, [state]))
        template_values['state_infos'] = state_infos
        return TemplateResponse(request, 'repositories/flat_files/index.html', template_values)
Ejemplo n.º 46
0
    def get_providers_by_version(self, request, rid, state_slug, archive):
        repo = get_object_or_404(Repository.reader_queryset(request),
                                 id=rid,
                                 archive_type=self.archive_type)
        base_query = Element.objects.filter(repository=repo, archive=archive)
        if state_slug:
            state = get_object_or_404(ArchiveState,
                                      repository=repo,
                                      name=state_slug)
            base_query = base_query.filter(states=state)
        versions = {}
        for element in base_query:
            metadata = json.loads(element.extra_data)
            versions.setdefault(element.version, []).append({
                'name':
                metadata['provider'],
                'url':
                '%s%s' % (settings.SERVER_NAME, element.get_direct_link()),
                'checksum_type':
                'sha1',
                'checksum':
                element.sha1
            })
        result = {
            'name':
            archive,
            'description':
            _('This box contains %(name)s') % {
                'name': archive
            },
            'versions': [{
                'version': k,
                'status': 'active',
                'providers': v
            } for (k, v) in versions.items()]
        }
        return result


# Vagrant.configure(2) do |config|
#
#   config.vm.box = "{$relativePathInfo|escape}"
#   config.vm.box_url = '<a href="{$CATALOG_URI|escape}{$pathInfo|escape}">{$CATALOG_URI|escape}{$pathInfo|escape}</a>'
#
#   # Whatever other config stuff you want to do
# end
Ejemplo n.º 47
0
    def get_providers_by_version(self, request, rid, state_slug, archive):
        repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.archive_type)
        base_query = Element.objects.filter(repository=repo, archive=archive)
        if state_slug:
            state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
            base_query = base_query.filter(states=state)
        versions = {}
        for element in base_query:
            metadata = json.loads(element.extra_data)
            versions.setdefault(element.version, []).append({
                'name': metadata['provider'],
                'url': '%s%s' % (settings.SERVER_BASE_URL[:-1], element.get_direct_link()),
                'checksum_type': 'sha1',

                'checksum': element.sha1})
        result = {'name': archive, 'description': _('This box contains %(name)s') % {'name': archive},
                  'versions': [{'version': k, 'status': 'active', 'providers': v} for (k, v) in versions.items()]}
        return result
Ejemplo n.º 48
0
 def xr_user_packages(request, rpc_args, self, rid, repo_slug, state_slug=None):
     if len(rpc_args) == 0:
         raise PermissionDenied
     author = str(rpc_args[0]).lower()
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, state=state).filter(author__name__icontains=author) \
             .order_by('name')
     else:
         query = Element.objects.filter(repository=repo).filter(author__name__icontains=author).order_by('name')
     results = []
     prev = None
     for value in query:
         if value.name != prev:
             results.append(('Owner', value.name))
             prev = value.name
     return results
Ejemplo n.º 49
0
 def xr_user_packages(request, rpc_args, self, rid, repo_slug, state_slug=None):
     if len(rpc_args) == 0:
         raise PermissionDenied
     author = str(rpc_args[0]).lower()
     repo = get_object_or_404(Repository.reader_queryset(request), id=rid, archive_type=self.model)
     if state_slug:
         state = get_object_or_404(ArchiveState, repository=repo, name=state_slug)
         query = Element.objects.filter(repository=repo, state=state).filter(author__name__icontains=author) \
             .order_by('name')
     else:
         query = Element.objects.filter(repository=repo).filter(author__name__icontains=author).order_by('name')
     results = []
     prev = None
     for value in query:
         if value.name != prev:
             results.append(('Owner', value.name))
             prev = value.name
     return results
Ejemplo n.º 50
0
def add_element(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)

    class ElementForm(forms.Form):
        package = forms.FileField(label=_('Package'))
        states = forms.ModelMultipleChoiceField(repo.archivestate_set.all(),
                                                label=_('States'))

        def clean(self):
            data_ = super().clean()
            if 'package' in self.cleaned_data:
                if not repo.get_model().is_file_valid(
                        self.cleaned_data['package']):
                    raise ValidationError(
                        _('This repository is unable to handle this file.'))
            return data_

    if request.method == 'POST':
        form = ElementForm(request.POST, files=request.FILES)
        if form.is_valid():
            try:
                element = generic_add_element(
                    request, repo, form.cleaned_data['package'],
                    [x.name for x in form.cleaned_data['states']])
                messages.info(
                    request,
                    _('The package %(n)s has been successfully uploaded.') %
                    {'n': element.filename})
            except InvalidRepositoryException as e:
                messages.error(
                    request,
                    _('Unable to add the package to this repository: %(msg)s.')
                    % {'msg': str(e)})
            return HttpResponseRedirect(
                reverse('moneta:add_element', kwargs={'rid': rid}))
    else:
        form = ElementForm()
    template_values = {
        'form': form,
        'repo': repo,
        'upload_allowed': repo.upload_allowed(request)
    }
    return render_to_response('moneta/add_package.html', template_values,
                              RequestContext(request))
Ejemplo n.º 51
0
def compare_states(request: HttpRequest, rid):
    repo = get_object_or_404(Repository.reader_queryset(request), id=rid)
    states = ArchiveState.objects.filter(repository=repo)
    operators = {'<': lambda x, y: x < y, '≤': lambda x, y: x <= y, '=': lambda x, y: x == y,
                 '≥': lambda x, y: x >= y, '>': lambda x, y: x > y}

    class CompareForm(forms.Form):
        state_left = forms.ModelChoiceField(states, label=_('Left state'))
        operator = forms.ChoiceField([(x, x) for x in operators], label=_('Assert operator'))
        state_right = forms.ModelChoiceField(states, label=_('Right state'))

    template_values = {'repo': repo, 'upload_allowed': repo.upload_allowed(request)}
    if request.method == 'POST':
        form = CompareForm(request.POST)
        if form.is_valid():
            state_left = form.cleaned_data['state_left']
            state_right = form.cleaned_data['state_right']
            operator = operators[form.cleaned_data['operator']]
            left_elements = Element.objects.filter(repository=repo, states=state_left)
            right_elements = Element.objects.filter(repository=repo, states=state_right)
            left_dict = {}
            right_dict = {}
            for element in left_elements:
                left_dict.setdefault(element.archive, (LooseVersion(element.version), []))[1].append(element)
            for element in right_elements:
                right_dict.setdefault(element.archive, (LooseVersion(element.version), []))[1].append(element)
            only_left, only_right = {}, {}
            left_keys, right_keys = set(left_dict.keys()), set(right_dict.keys())
            for key in left_keys - right_keys:
                only_left[key] = left_dict[key]
            for key in right_keys - left_keys:
                only_right[key] = right_dict[key]
            invalid_values = []
            for key in left_keys.intersection(right_keys):
                if not operator(left_dict[key][0], right_dict[key][0]):
                    invalid_values.append((key, left_dict[key], right_dict[key]))
            invalid_values.sort(key=lambda x: x[0])
            template_values.update({'only_left': only_left, 'only_right': only_right, 'invalid_values': invalid_values,
                                    'state_left': state_left, 'state_right': state_right})
            template_values['all_valid'] = bool(not only_left and not only_right and not invalid_values)
    else:
        form = CompareForm()
    template_values['form'] = form
    return render_to_response('moneta/compare_states.html', template_values, RequestContext(request))
Ejemplo n.º 52
0
def add_element_post(request: HttpRequest, rid):
    try:
        repo = Repository.upload_queryset(request).get(id=rid)
    except Repository.DoesNotExist:
        return HttpResponse(_('You cannot upload new packages to this repository'), status=403)
    if request.method != 'POST':
        return HttpResponse(_('Method not allowed'), status=405)
    validators = [RegexValidator(r'[\w\.\-\(\)/]+')]

    class ElementForm(forms.Form):
        filename = forms.CharField(label=_('Package'), max_length=255, validators=validators)
        states = forms.MultipleChoiceField(choices=[(x.name, x.name) for x in repo.archivestate_set.all()],
                                           label=_('States'))
        name = forms.CharField(label=_('Name'), max_length=255, validators=validators, required=False)
        archive = forms.CharField(label=_('Archive'), max_length=255, validators=validators, required=False)
        version = forms.CharField(label=_('Version'), max_length=255, validators=validators, required=False)

    form = ElementForm(request.GET)
    if not form.is_valid():
        return TemplateResponse(request, 'moneta/not_allowed.html', {}, status=405)

    tmp_file = tempfile.TemporaryFile(mode='w+b', dir=settings.FILE_UPLOAD_TEMP_DIR)
    c = False
    chunk = request.read(32768)
    while chunk:
        tmp_file.write(chunk)
        c = True
        chunk = request.read(32768)
    tmp_file.flush()
    tmp_file.seek(0)
    if not c:
        return HttpResponse(_('Empty file. You must POST a valid file.\n'), status=400)
    uploaded_file = UploadedFile(name=form.cleaned_data['filename'], file=tmp_file)
    try:
        element = generic_add_element(request, repo, uploaded_file, form.cleaned_data['states'],
                                      name=form.cleaned_data.get('name'), archive=form.cleaned_data.get('archive'),
                                      version=form.cleaned_data.get('version'), )
    except InvalidRepositoryException as e:
        return HttpResponse(str(e), status=400)
    finally:
        tmp_file.close()
    template_values = {'repo': repo, 'element': element}
    return HttpResponse(_('Package %(element)s successfully added to repository %(repo)s.\n') % template_values)
Ejemplo n.º 53
0
 def plugin_index(self,
                  request: HttpRequest,
                  rid,
                  repo_slug,
                  state_slug=None):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState,
                                   repository=repo,
                                   name=state_slug)
         base_query = base_query.filter(states=state)
     return TemplateResponse(request,
                             'repositories/jetbrains/updatePlugins.xml',
                             {'elements': base_query},
                             content_type='application/xml')
Ejemplo n.º 54
0
def delete_repository(request: HttpRequest, rid):
    from moneta.repository.forms import DeleteRepositoryForm

    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    if request.method == 'POST':
        form = DeleteRepositoryForm(request.POST)
        if form.is_valid():
            for element in Element.objects.filter(repository=repo):
                element.delete()
            repo.delete()
            messages.warning(
                request,
                _('The repository %(repo)s has been deleted.') %
                {'repo': repo.name})
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = DeleteRepositoryForm()
    template_values = {'form': form, 'repo': repo}
    return render_to_response('moneta/delete_repo.html', template_values,
                              RequestContext(request))
Ejemplo n.º 55
0
 def get_file(self, request, rid, repo_slug, state_slug, folder, filename):
     """
     Return a HttpResponse
     :param request: HttpRequest
     :raise:
     """
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     q = Element.objects.filter(repository__id=rid, filename=filename)[0:1]
     q = list(q)
     if len(q) == 0:
         raise Http404
     element = q[0]
     return get_file(request,
                     element.id,
                     compression=None,
                     path='',
                     element=element,
                     name=None)
Ejemplo n.º 56
0
 def download(self,
              request,
              rid,
              repo_slug,
              state_slug=None,
              filename=None):
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState,
                                   repository=repo,
                                   name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, filename=filename)
     from moneta.views import get_file
     return get_file(request, element.pk)
Ejemplo n.º 57
0
def delete_element(request: HttpRequest, rid, eid):
    from moneta.repository.forms import DeleteRepositoryForm

    repo = get_object_or_404(Repository.upload_queryset(request), id=rid)
    element = get_object_or_404(Element.objects.filter(repository=repo,
                                                       id=eid))
    if request.method == 'POST':
        form = DeleteRepositoryForm(request.POST)
        if form.is_valid():
            element.delete()
            messages.warning(
                request,
                _('The package %(repo)s has been deleted.') %
                {'repo': element.full_name})
            return HttpResponseRedirect(reverse('moneta:index'))
    else:
        form = DeleteRepositoryForm()
    template_values = {'form': form, 'repo': repo, 'element': element}
    return TemplateResponse(request, 'moneta/delete_element.html',
                            template_values)
Ejemplo n.º 58
0
    def index(self, request, rid):
        repo = get_object_or_404(Repository.reader_queryset(request),
                                 id=rid,
                                 archive_type=self.archive_type)
        states = [
            state for state in ArchiveState.objects.filter(
                repository=repo).order_by('name')
        ]
        tab_infos = [
            (reverse('repository:jetbrains:plugin_index',
                     kwargs={
                         'rid': repo.id,
                         'repo_slug': repo.slug
                     }), ArchiveState(name=_('All states'),
                                      slug='all-states'), states),
        ]
        tab_infos += [(reverse('repository:jetbrains:plugin_index',
                               kwargs={
                                   'rid': repo.id,
                                   'repo_slug': repo.slug,
                                   'state_slug': state.slug
                               }), state, [state]) for state in states]

        template_values = {
            'repo':
            repo,
            'states':
            states,
            'upload_allowed':
            repo.upload_allowed(request),
            'index_url':
            reverse(moneta_url(repo, 'index'), kwargs={
                'rid': repo.id,
            }),
            'tab_infos':
            tab_infos,
            'admin_allowed':
            repo.admin_allowed(request),
        }
        return TemplateResponse(request, self.index_html, template_values)
Ejemplo n.º 59
0
 def gem_specs(self,
               request,
               rid,
               repo_slug,
               state_slug=None,
               filename=None):
     name, sep, version = filename.rpartition('-')
     if sep != '-':
         raise Http404
     # noinspection PyUnusedLocal
     repo_slug = repo_slug
     repo = get_object_or_404(Repository.reader_queryset(request),
                              id=rid,
                              archive_type=self.archive_type)
     base_query = Element.objects.filter(repository=repo)
     if state_slug:
         state = get_object_or_404(ArchiveState,
                                   repository=repo,
                                   name=state_slug)
         base_query = base_query.filter(states=state)
     element = get_object_or_404(base_query, name=name, version=version)
     return HttpResponse(self.yaml_data(element), content_type='text/yaml')