Example #1
0
def test_vfolder_directory_clash(af_vfolder_test_browser_defines_po):
    """Tests that the creation of a virtual folder fails if it clashes with
    some already existing directory.

    References #3905.
    """
    from django.core.exceptions import ValidationError

    from virtualfolder.models import VirtualFolder

    vfolder_item = {
        'name': "browser",
        'location': "/af/vfolder_test/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.save()

    assert (u"Problem adding virtual folder 'browser' with location "
            u"'/af/vfolder_test/': VirtualFolderTreeItem clashes with "
            u"Directory /af/vfolder_test/browser/") in str(excinfo.value)
Example #2
0
def test_vfolder_directory_clash(af_vfolder_test_browser_defines_po):
    """Tests that the creation of a virtual folder fails if it clashes with
    some already existing directory.

    References #3905.
    """
    from django.core.exceptions import ValidationError

    from virtualfolder.models import VirtualFolder

    vfolder_item = {
        'name': "browser",
        'location': "/af/vfolder_test/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.save()

    assert (u"Problem adding virtual folder 'browser' with location "
            u"'/af/vfolder_test/': VirtualFolderTreeItem clashes with "
            u"Directory /af/vfolder_test/browser/") in str(excinfo.value)
Example #3
0
def test_vfolder_priority_not_greater_than_zero():
    """Tests that the creation of a virtual folder fails if the provided
    priority is not greater than zero.
    """

    # Test priority less than zero.
    vfolder_item = {
        'name': "whatever",
        'location': "/af/vfolder_test/",
        'priority': -3,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Priority must be greater than zero.' in str(excinfo.value)

    # Test zero priority.
    vfolder_item['priority'] = 0
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Priority must be greater than zero.' in str(excinfo.value)
Example #4
0
def test_vfolder_location_starts_with_projects():
    """Tests that the creation of a virtual folder fails if it uses a location
    that starts with /projects/.
    """

    # Test just /projects/ location.
    vfolder_item = {
        'name': "whatever",
        'location': "/projects/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'Locations starting with "/projects/" are not allowed. Use '
            u'"/{LANG}/" instead.') in str(excinfo.value)

    # Test /projects/tutorial/ location.
    vfolder_item['location'] = "/projects/tutorial/"
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'Locations starting with "/projects/" are not allowed. Use '
            u'"/{LANG}/" instead.') in str(excinfo.value)
Example #5
0
def get_vfolders(directory, all_vfolders=False):
    """Return a list of virtual folders for this ``directory``.

    The elements of the list are dictionaries which keys are populated after
    in the templates.

    If ``all_vfolders`` is True then all the virtual folders matching the
    provided directory are returned. If not only the visible ones are returned.
    """
    if all_vfolders:
        return [make_vfolder_item(vf, directory.pootle_path)
            for vf in VirtualFolder.get_matching_for(directory.pootle_path)]

    return [make_vfolder_item(vf, directory.pootle_path)
            for vf in VirtualFolder.get_visible_for(directory.pootle_path)]
Example #6
0
def get_overview_context(request):
    """Returns a common context for overview browser pages.

    :param request: a :cls:`django.http.HttpRequest` object.
    """
    resource_obj = request.resource_obj
    resource_path = getattr(request, 'resource_path', '')

    filters = {}

    if (not isinstance(resource_obj, Store)
            and VirtualFolder.get_matching_for(request.pootle_path).count()):
        filters['sort'] = 'priority'

    url_action_continue = resource_obj.get_translate_url(state='incomplete',
                                                         **filters)
    url_action_fixcritical = resource_obj.get_critical_url(**filters)
    url_action_review = resource_obj.get_translate_url(state='suggestions',
                                                       **filters)
    url_action_view_all = resource_obj.get_translate_url(state='all')

    return {
        'page': 'overview',
        'pootle_path': request.pootle_path,
        'resource_obj': resource_obj,
        'resource_path': resource_path,
        'resource_path_parts': get_path_parts(resource_path),
        'translation_states': get_translation_states(resource_obj),
        'check_categories': get_qualitycheck_schema(resource_obj),
        'url_action_continue': url_action_continue,
        'url_action_fixcritical': url_action_fixcritical,
        'url_action_review': url_action_review,
        'url_action_view_all': url_action_view_all,
    }
Example #7
0
def test_vfolder_with_no_filter_rules():
    """Tests that the creation of a virtual folder fails if it doesn't have any
    filter rules.
    """

    vfolder_item = {
        'name': "whatever",
        'location': "/af/vfolder_test/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Some filtering rule must be specified.' in str(excinfo.value)
Example #8
0
def test_vfolder_with_no_filter_rules():
    """Tests that the creation of a virtual folder fails if it doesn't have any
    filter rules.
    """

    vfolder_item = {
        'name': "whatever",
        'location': "/af/vfolder_test/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Some filtering rule must be specified.' in str(excinfo.value)
Example #9
0
def get_overview_stats(request, *args, **kwargs):
    stats = request.resource_obj.get_stats()

    if isinstance(request.resource_obj, Directory):
        stats['vfolders'] = VirtualFolder.get_stats_for(
            request.resource_obj.pootle_path,
            request.user.is_superuser
        )

    return JsonResponse(stats)
Example #10
0
def test_vfolder_root_location():
    """Tests that the creation of a virtual folder fails if it uses location /
    instead of /{LANG}/{PROJ}/.
    """

    vfolder_item = {
        'name': "whatever",
        'location': "/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'The "/" location is not allowed. Use "/{LANG}/{PROJ}/" instead.'
            in str(excinfo.value))
Example #11
0
def test_vfolder_root_location():
    """Tests that the creation of a virtual folder fails if it uses location /
    instead of /{LANG}/{PROJ}/.
    """

    vfolder_item = {
        'name': "whatever",
        'location': "/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'The "/" location is not allowed. Use "/{LANG}/{PROJ}/" instead.'
            in str(excinfo.value))
Example #12
0
def make_directory_item(directory):
    filters = {}

    if VirtualFolder.get_matching_for(directory.pootle_path).count():
        # The directory has virtual folders, so append priority sorting to URL.
        filters['sort'] = 'priority'

    item = make_generic_item(directory, **filters)
    item.update({
        'icon': 'folder',
    })
    return item
Example #13
0
def make_directory_item(directory):
    filters = {}

    if VirtualFolder.get_matching_for(directory.pootle_path).count():
        # The directory has virtual folders, so append priority sorting to URL.
        filters['sort'] = 'priority'

    item = make_generic_item(directory, **filters)
    item.update({
        'icon': 'folder',
    })
    return item
Example #14
0
def test_vfolder_location_starts_with_projects():
    """Tests that the creation of a virtual folder fails if it uses a location
    that starts with /projects/.
    """
    from django.core.exceptions import ValidationError

    from virtualfolder.models import VirtualFolder

    # Test just /projects/ location.
    vfolder_item = {
        'name': "whatever",
        'location': "/projects/",
        'priority': 4,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'Locations starting with "/projects/" are not allowed. Use '
            u'"/{LANG}/" instead.') in str(excinfo.value)

    # Test /projects/tutorial/ location.
    vfolder_item['location'] = "/projects/tutorial/"
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert (u'Locations starting with "/projects/" are not allowed. Use '
            u'"/{LANG}/" instead.') in str(excinfo.value)
Example #15
0
def test_vfolder_priority_not_greater_than_zero():
    """Tests that the creation of a virtual folder fails if the provided
    priority is not greater than zero.
    """
    from django.core.exceptions import ValidationError

    from virtualfolder.models import VirtualFolder

    # Test priority less than zero.
    vfolder_item = {
        'name': "whatever",
        'location': "/af/vfolder_test/",
        'priority': -3,
        'is_public': True,
        'filter_rules': "browser/defines.po",
    }
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Priority must be greater than zero.' in str(excinfo.value)

    # Test zero priority.
    vfolder_item['priority'] = 0
    vfolder = VirtualFolder(**vfolder_item)

    with pytest.raises(ValidationError) as excinfo:
        vfolder.clean_fields()

    assert u'Priority must be greater than zero.' in str(excinfo.value)
Example #16
0
def test_extract_vfolder_from_path():
    """Tests that vfolder is correctly extracted from path, if any."""
    subdir0 = TranslationProject.objects.get(
        language__code="language1",
        project__code="project1",
    ).directory.child_dirs.first()

    # Check that subdir0 pootle_path matches no vfolder.
    path = subdir0.pootle_path

    assert (None, path) == extract_vfolder_from_path(path)

    # Check that vfoldertreeitem pootle_path returns a vfolder and a clean path.
    vfolder_item = {
        'name': 'vfolder0',
        'location': subdir0.pootle_path,
        'priority': 4,
        'is_public': True,
        'filter_rules': subdir0.child_stores.first().name,
    }
    vfolder0 = VirtualFolder(**vfolder_item)
    vfolder0.save()

    path = subdir0.vf_treeitems.first().pootle_path

    assert (vfolder0, subdir0.pootle_path) == extract_vfolder_from_path(path)

    # Check that the right vfolder is matched and returned.
    subdir1_first_store = subdir0.child_dirs.first().child_stores.first()

    vfolder_location = subdir0.parent.pootle_path
    filter_path = subdir1_first_store.pootle_path.replace(vfolder_location, "")

    vfolder_item.update({
        'location': vfolder_location,
        'priority': 2,
        'filter_rules': filter_path,
    })
    vfolder1 = VirtualFolder(**vfolder_item)
    vfolder1.save()

    path = subdir0.vf_treeitems.first().pootle_path

    assert (vfolder0, subdir0.pootle_path) == extract_vfolder_from_path(path)

    # Despite the virtual folders share the same name they have different
    # locations, but the VirtualFolderTreeItem pootle_path is unique, thus only
    # one exists.
    assert 1 == VirtualFolderTreeItem.objects.filter(pootle_path=path).count()
Example #17
0
def get_browser_context(request):
    """Returns a common context for browser pages.

    :param request: a :cls:`django.http.HttpRequest` object.
    """
    resource_obj = request.resource_obj
    resource_path = getattr(request, 'resource_path', '')

    filters = {}

    if (not isinstance(resource_obj, Store) and
        VirtualFolder.get_matching_for(request.pootle_path).count()):
        filters['sort'] = 'priority'

    url_action_continue = resource_obj.get_translate_url(state='incomplete',
                                                         **filters)
    url_action_fixcritical = resource_obj.get_critical_url(**filters)
    url_action_review = resource_obj.get_translate_url(state='suggestions',
                                                       **filters)
    url_action_view_all = resource_obj.get_translate_url(state='all')

    return {
        'page': 'browse',

        'pootle_path': request.pootle_path,
        'resource_obj': resource_obj,
        'resource_path': resource_path,
        'resource_path_parts': get_path_parts(resource_path),

        'translation_states': get_translation_states(resource_obj),
        'check_categories': get_qualitycheck_schema(resource_obj),

        'url_action_continue': url_action_continue,
        'url_action_fixcritical': url_action_fixcritical,
        'url_action_review': url_action_review,
        'url_action_view_all': url_action_view_all,
    }
Example #18
0
    def handle(self, *args, **options):
        """Add virtual folders from file."""

        if not args:
            raise CommandError("You forgot to provide the mandatory filename.")

        try:
            inputfile = open(args[0], "r")
            vfolders = json.load(inputfile)
            inputfile.close()
        except IOError as e:
            raise CommandError(e)
        except ValueError as e:
            raise CommandError("Please check if the JSON file is malformed. "
                               "Original error:\n%s" % e)

        added_count = 0
        updated_count = 0
        errored_count = 0

        for vfolder_item in vfolders:
            vfolder_item['name'] = vfolder_item['name'].lower()

            # Put all the files for each virtual folder as a list and save it
            # as its filter rules.
            try:
                vfolder_item['filter_rules'] = ','.join(
                    vfolder_item['filters']['files'])
            except KeyError:
                vfolder_item['filter_rules'] = ''

            if 'filters' in vfolder_item:
                del vfolder_item['filters']

            # Now create or update the virtual folder.
            try:
                # Retrieve the virtual folder if it exists.
                vfolder = VirtualFolder.objects.get(
                    name=vfolder_item['name'],
                    location=vfolder_item['location'],
                )
            except VirtualFolder.DoesNotExist:
                # If the virtual folder doesn't exist yet then create it.
                try:
                    vfolder = VirtualFolder(**vfolder_item)
                    vfolder.save()
                except ValidationError as e:
                    errored_count += 1
                    logging.error(e.message)
                else:
                    added_count += 1
            else:
                # Update the already existing virtual folder.
                changed = False

                if vfolder.filter_rules != vfolder_item['filter_rules']:
                    vfolder.filter_rules = vfolder_item['filter_rules']
                    changed = True
                    logging.info(
                        "Filter rules for virtual folder '%s' will "
                        "be changed.", vfolder.name)

                if ('priority' in vfolder_item
                        and vfolder.priority != vfolder_item['priority']):

                    vfolder.priority = vfolder_item['priority']
                    changed = True
                    logging.info(
                        "Priority for virtual folder '%s' will be "
                        "changed to %f.", vfolder.name, vfolder.priority)

                if ('is_browsable' in vfolder_item and
                        vfolder.is_browsable != vfolder_item['is_browsable']):

                    vfolder.is_browsable = vfolder_item['is_browsable']
                    changed = True
                    logging.info(
                        "is_browsable status for virtual folder '%s' "
                        "will be changed.", vfolder.name)

                if ('description' in vfolder_item and vfolder.description.raw
                        != vfolder_item['description']):

                    vfolder.description = vfolder_item['description']
                    changed = True
                    logging.info(
                        "Description for virtual folder '%s' will be "
                        "changed.", vfolder.name)

                if changed:
                    try:
                        vfolder.save()
                    except ValidationError as e:
                        errored_count += 1
                        logging.error(e.message)
                    else:
                        updated_count += 1

        logging.info(
            "\nErrored: %d\nAdded: %d\nUpdated: %d\nUnchanged: %d",
            errored_count, added_count, updated_count,
            len(vfolders) - errored_count - added_count - updated_count)
Example #19
0
    def handle(self, **options):
        """Add virtual folders from file."""

        try:
            with open(options["vfolder"][0], "r") as inputfile:
                vfolders = json.load(inputfile)
        except IOError as e:
            raise CommandError(e)
        except ValueError as e:
            raise CommandError("Please check if the JSON file is malformed. " "Original error:\n%s" % e)

        for vfolder_item in vfolders:
            try:
                temp = ",".join(vfolder_item["filters"]["files"])
                if not temp:
                    raise ValueError
            except (KeyError, ValueError):
                raise CommandError("Virtual folder '%s' has no filtering " "rules." % vfolder_item["name"])

        self.stdout.write("Importing virtual folders...")

        added_count = 0
        updated_count = 0
        errored_count = 0

        for vfolder_item in vfolders:
            vfolder_item["name"] = vfolder_item["name"].strip().lower()

            # Put all the files for each virtual folder as a list and save it
            # as its filter rules.
            languages, projects, new_rules = self.parse_vfolder_rules(
                vfolder_item["location"].strip(), vfolder_item["filters"]["files"]
            )

            vfolder_item["filter_rules"] = new_rules

            if "filters" in vfolder_item:
                del vfolder_item["filters"]

            # Now create or update the virtual folder.
            try:
                # Retrieve the virtual folder if it exists.
                vfolder = VirtualFolder.objects.get(name=vfolder_item["name"])
            except VirtualFolder.DoesNotExist:
                # If the virtual folder doesn't exist yet then create it.
                try:
                    self.stdout.write(u"Adding new virtual folder %s..." % vfolder_item["name"])
                    vfolder_item["all_projects"] = not projects
                    vfolder_item["all_languages"] = not languages
                    vfolder = VirtualFolder(**vfolder_item)
                    vfolder.save()
                except ValidationError as e:
                    errored_count += 1
                    self.stdout.write("FAILED")
                    self.stderr.write(e)
                else:
                    if projects:
                        vfolder.projects.add(*Project.objects.filter(code__in=projects))
                    if languages:
                        vfolder.languages.add(*Language.objects.filter(code__in=languages))
                    self.stdout.write("DONE")
                    added_count += 1
            else:
                # Update the already existing virtual folder.
                changed = False

                if not projects:
                    vfolder.all_projects = True
                    changed = True
                    logging.debug("'All projects' for virtual folder '%s' " "will be changed.", vfolder.name)

                if not languages:
                    vfolder.all_languages = True
                    changed = True
                    logging.debug("'All languages' for virtual folder '%s' " "will be changed.", vfolder.name)

                if projects:
                    vfolder.projects.set(*Project.objects.filter(code__in=projects))
                if languages:
                    vfolder.languages.set(*Language.objects.filter(code__in=languages))

                if vfolder.filter_rules != vfolder_item["filter_rules"]:
                    vfolder.filter_rules = vfolder_item["filter_rules"]
                    changed = True
                    logging.debug("Filter rules for virtual folder '%s' will " "be changed.", vfolder.name)

                if "priority" in vfolder_item and vfolder.priority != vfolder_item["priority"]:

                    vfolder.priority = vfolder_item["priority"]
                    changed = True
                    logging.debug(
                        "Priority for virtual folder '%s' will be " "changed to %f.", vfolder.name, vfolder.priority
                    )

                if "is_public" in vfolder_item and vfolder.is_public != vfolder_item["is_public"]:

                    vfolder.is_public = vfolder_item["is_public"]
                    changed = True
                    logging.debug("is_public status for virtual folder " "'%s' will be changed.", vfolder.name)

                if "description" in vfolder_item and vfolder.description.raw != vfolder_item["description"]:

                    vfolder.description = vfolder_item["description"]
                    changed = True
                    logging.debug("Description for virtual folder '%s' will " "be changed.", vfolder.name)

                if changed:
                    try:
                        self.stdout.write(u"Updating virtual folder %s..." % vfolder_item["name"])
                        vfolder.save()
                    except ValidationError as e:
                        errored_count += 1
                        self.stdout.write("FAILED")
                        self.stderr.write(e)
                    else:
                        self.stdout.write("DONE")
                        updated_count += 1

        self.stdout.write(
            "\nErrored: %d\nAdded: %d\n"
            "Updated: %d\nUnchanged: %d"
            % (errored_count, added_count, updated_count, len(vfolders) - errored_count - added_count - updated_count)
        )
Example #20
0
def overview(request, translation_project, dir_path, filename=None):
    project = translation_project.project
    language = translation_project.language

    directory = request.directory
    store = request.store
    is_admin = check_permission('administrate', request)

    # TODO: cleanup and refactor, retrieve from cache
    try:
        ann_virtual_path = 'announcements/projects/' + project.code
        announcement = StaticPage.objects.live(request.user).get(
            virtual_path=ann_virtual_path,
        )
    except StaticPage.DoesNotExist:
        announcement = None

    has_announcement = announcement is not None
    has_sidebar = has_announcement
    is_sidebar_open = True
    stored_mtime = None
    new_mtime = None
    cookie_data = {}

    if SIDEBAR_COOKIE_NAME in request.COOKIES:
        json_str = unquote(request.COOKIES[SIDEBAR_COOKIE_NAME])
        cookie_data = json.loads(json_str)

        if 'isOpen' in cookie_data:
            is_sidebar_open = cookie_data['isOpen']

        if project.code in cookie_data:
            stored_mtime = cookie_data[project.code]

    if has_announcement:
        ann_mtime = dateformat.format(announcement.modified_on, 'U')
        if ann_mtime != stored_mtime:
            is_sidebar_open = True
            new_mtime = ann_mtime

    ctx = get_overview_context(request)

    # TODO improve plugin logic
    if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated():
        from import_export.views import handle_upload_form

        ctx.update(handle_upload_form(request))

        has_download = (check_permission('translate', request) or
                        check_permission('suggest', request))
        ctx.update({
            'display_download': has_download,
        })
        has_sidebar = True

    stats = request.resource_obj.get_stats()

    if store is None:
        table_fields = ['name', 'progress', 'total', 'need-translation',
                        'suggestions', 'critical', 'last-updated', 'activity']
        ctx.update({
            'table': {
                'id': 'tp',
                'fields': table_fields,
                'headings': get_table_headings(table_fields),
                'parent': get_parent(directory),
                'items': get_children(directory),
            }
        })

        vfolders = get_vfolders(directory)
        if len(vfolders) > 0:
            table_fields = ['name', 'priority', 'progress', 'total',
                            'need-translation', 'suggestions', 'critical',
                            'activity']
            ctx.update({
                'vfolders': {
                    'id': 'vfolders',
                    'fields': table_fields,
                    'headings': get_table_headings(table_fields),
                    'items': get_vfolders(directory, all_vfolders=is_admin),
                },
            })

            #FIXME: set vfolders stats in the resource, don't inject them here.
            stats['vfolders'] = VirtualFolder.get_stats_for(
                directory.pootle_path,
                all_vfolders=is_admin
            )

    ctx.update({
        'translation_project': translation_project,
        'project': project,
        'language': language,
        'stats': jsonify(stats),
        'is_admin': is_admin,

        'browser_extends': 'translation_projects/base.html',

        'announcement': announcement,
        'is_sidebar_open': is_sidebar_open,
        'has_sidebar': has_sidebar,
    })

    response = render(request, 'browser/overview.html', ctx)

    if new_mtime is not None:
        cookie_data[project.code] = new_mtime
        cookie_data = quote(json.dumps(cookie_data))
        response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data)

    return response
Example #21
0
    def handle(self, *args, **options):
        """Add virtual folders from file."""

        if not args:
            raise CommandError("You forgot to provide the mandatory filename.")

        try:
            inputfile = open(args[0], "r")
            vfolders = json.load(inputfile)
            inputfile.close()
        except IOError as e:
            raise CommandError(e)
        except ValueError as e:
            raise CommandError("Please check if the JSON file is malformed. "
                               "Original error:\n%s" %e)

        added_count = 0
        updated_count = 0
        errored_count = 0

        for vfolder_item in vfolders:
            vfolder_item['name'] = vfolder_item['name'].lower()

            # Put all the files for each virtual folder as a list and save it
            # as its filter rules.
            try:
                vfolder_item['filter_rules'] = ','.join(vfolder_item['filters']['files'])
            except KeyError:
                vfolder_item['filter_rules'] = ''

            if 'filters' in vfolder_item:
                del vfolder_item['filters']

            # Now create or update the virtual folder.
            try:
                # Retrieve the virtual folder if it exists.
                vfolder = VirtualFolder.objects.get(
                    name=vfolder_item['name'],
                    location=vfolder_item['location'],
                )
            except VirtualFolder.DoesNotExist:
                # If the virtual folder doesn't exist yet then create it.
                try:
                    vfolder = VirtualFolder(**vfolder_item)
                    vfolder.save()
                except ValidationError as e:
                    errored_count += 1
                    logging.error(e.message)
                else:
                    added_count += 1
            else:
                # Update the already existing virtual folder.
                changed = False

                if vfolder.filter_rules != vfolder_item['filter_rules']:
                    vfolder.filter_rules = vfolder_item['filter_rules']
                    changed = True
                    logging.info("Filter rules for virtual folder '%s' will "
                                 "be changed.", vfolder.name)

                if ('priority' in vfolder_item and
                    vfolder.priority != vfolder_item['priority']):

                    vfolder.priority = vfolder_item['priority']
                    changed = True
                    logging.info("Priority for virtual folder '%s' will be "
                                 "changed to %f.", vfolder.name,
                                 vfolder.priority)

                if ('is_browsable' in vfolder_item and
                    vfolder.is_browsable != vfolder_item['is_browsable']):

                    vfolder.is_browsable = vfolder_item['is_browsable']
                    changed = True
                    logging.info("is_browsable status for virtual folder '%s' "
                                 "will be changed.", vfolder.name)

                if ('description' in vfolder_item and
                    vfolder.description.raw != vfolder_item['description']):

                    vfolder.description = vfolder_item['description']
                    changed = True
                    logging.info("Description for virtual folder '%s' will be "
                                 "changed.", vfolder.name)

                if changed:
                    try:
                        vfolder.save()
                    except ValidationError as e:
                        errored_count += 1
                        logging.error(e.message)
                    else:
                        updated_count += 1

        logging.info("\nErrored: %d\nAdded: %d\nUpdated: %d\nUnchanged: %d",
                     errored_count, added_count, updated_count,
                     len(vfolders)-errored_count-added_count-updated_count)
Example #22
0
    def handle(self, **options):
        """Add virtual folders from file."""

        try:
            with open(options['vfolder'][0], "r") as inputfile:
                vfolders = json.load(inputfile)
        except IOError as e:
            raise CommandError(e)
        except ValueError as e:
            raise CommandError("Please check if the JSON file is malformed. "
                               "Original error:\n%s" % e)

        for vfolder_item in vfolders:
            try:
                temp = ','.join(vfolder_item['filters']['files'])
                if not temp:
                    raise ValueError
            except (KeyError, ValueError):
                raise CommandError("Virtual folder '%s' has no filtering "
                                   "rules." % vfolder_item['name'])

        self.stdout.write("Importing virtual folders...")

        added_count = 0
        updated_count = 0
        errored_count = 0

        for vfolder_item in vfolders:
            vfolder_item['name'] = vfolder_item['name'].lower()

            # Put all the files for each virtual folder as a list and save it
            # as its filter rules.
            vfolder_item['filter_rules'] = ','.join(
                vfolder_item['filters']['files'])

            if 'filters' in vfolder_item:
                del vfolder_item['filters']

            # Now create or update the virtual folder.
            try:
                # Retrieve the virtual folder if it exists.
                vfolder = VirtualFolder.objects.get(
                    name=vfolder_item['name'],
                    location=vfolder_item['location'],
                )
            except VirtualFolder.DoesNotExist:
                # If the virtual folder doesn't exist yet then create it.
                try:
                    self.stdout.write(u'Adding new virtual folder %s...' %
                                      vfolder_item['name'])
                    vfolder = VirtualFolder(**vfolder_item)
                    vfolder.save()
                except ValidationError as e:
                    errored_count += 1
                    self.stdout.write('FAILED')
                    self.stderr.write(e)
                else:
                    self.stdout.write('DONE')
                    added_count += 1
            else:
                # Update the already existing virtual folder.
                changed = False

                if vfolder.filter_rules != vfolder_item['filter_rules']:
                    vfolder.filter_rules = vfolder_item['filter_rules']
                    changed = True
                    logging.debug(
                        "Filter rules for virtual folder '%s' will "
                        "be changed.", vfolder.name)

                if ('priority' in vfolder_item
                        and vfolder.priority != vfolder_item['priority']):

                    vfolder.priority = vfolder_item['priority']
                    changed = True
                    logging.debug(
                        "Priority for virtual folder '%s' will be "
                        "changed to %f.", vfolder.name, vfolder.priority)

                if ('is_public' in vfolder_item
                        and vfolder.is_public != vfolder_item['is_public']):

                    vfolder.is_public = vfolder_item['is_public']
                    changed = True
                    logging.debug(
                        "is_public status for virtual folder "
                        "'%s' will be changed.", vfolder.name)

                if ('description' in vfolder_item and vfolder.description.raw
                        != vfolder_item['description']):

                    vfolder.description = vfolder_item['description']
                    changed = True
                    logging.debug(
                        "Description for virtual folder '%s' will "
                        "be changed.", vfolder.name)

                if changed:
                    try:
                        self.stdout.write(u'Updating virtual folder %s...' %
                                          vfolder_item['name'])
                        vfolder.save()
                    except ValidationError as e:
                        errored_count += 1
                        self.stdout.write('FAILED')
                        self.stderr.write(e)
                    else:
                        self.stdout.write('DONE')
                        updated_count += 1

        self.stdout.write(
            "\nErrored: %d\nAdded: %d\n"
            "Updated: %d\nUnchanged: %d" %
            (errored_count, added_count, updated_count,
             len(vfolders) - errored_count - added_count - updated_count))
Example #23
0
    def handle(self, **options):
        """Add virtual folders from file."""

        try:
            with open(options['vfolder'][0], "r") as inputfile:
                vfolders = json.load(inputfile)
        except IOError as e:
            raise CommandError(e)
        except ValueError as e:
            raise CommandError("Please check if the JSON file is malformed. "
                               "Original error:\n%s" % e)

        for vfolder_item in vfolders:
            try:
                temp = ','.join(vfolder_item['filters']['files'])
                if not temp:
                    raise ValueError
            except (KeyError, ValueError):
                raise CommandError("Virtual folder '%s' has no filtering "
                                   "rules." % vfolder_item['name'])

        self.stdout.write("Importing virtual folders...")

        added_count = 0
        updated_count = 0
        errored_count = 0

        for vfolder_item in vfolders:
            vfolder_item['name'] = vfolder_item['name'].lower()

            # Put all the files for each virtual folder as a list and save it
            # as its filter rules.
            vfolder_item['filter_rules'] = ','.join(
                vfolder_item['filters']['files'])

            if 'filters' in vfolder_item:
                del vfolder_item['filters']

            # Now create or update the virtual folder.
            try:
                # Retrieve the virtual folder if it exists.
                vfolder = VirtualFolder.objects.get(
                    name=vfolder_item['name'],
                    location=vfolder_item['location'],
                )
            except VirtualFolder.DoesNotExist:
                # If the virtual folder doesn't exist yet then create it.
                try:
                    self.stdout.write(u'Adding new virtual folder %s...' %
                                      vfolder_item['name'])
                    vfolder = VirtualFolder(**vfolder_item)
                    vfolder.save()
                except ValidationError as e:
                    errored_count += 1
                    self.stdout.write('FAILED')
                    self.stderr.write(e)
                else:
                    self.stdout.write('DONE')
                    added_count += 1
            else:
                # Update the already existing virtual folder.
                changed = False

                if vfolder.filter_rules != vfolder_item['filter_rules']:
                    vfolder.filter_rules = vfolder_item['filter_rules']
                    changed = True
                    logging.debug("Filter rules for virtual folder '%s' will "
                                  "be changed.", vfolder.name)

                if ('priority' in vfolder_item and
                    vfolder.priority != vfolder_item['priority']):

                    vfolder.priority = vfolder_item['priority']
                    changed = True
                    logging.debug("Priority for virtual folder '%s' will be "
                                  "changed to %f.", vfolder.name,
                                  vfolder.priority)

                if ('is_public' in vfolder_item and
                    vfolder.is_public != vfolder_item['is_public']):

                    vfolder.is_public = vfolder_item['is_public']
                    changed = True
                    logging.debug("is_public status for virtual folder "
                                  "'%s' will be changed.", vfolder.name)

                if ('description' in vfolder_item and
                    vfolder.description.raw != vfolder_item['description']):

                    vfolder.description = vfolder_item['description']
                    changed = True
                    logging.debug("Description for virtual folder '%s' will "
                                  "be changed.", vfolder.name)

                if changed:
                    try:
                        self.stdout.write(u'Updating virtual folder %s...' %
                                          vfolder_item['name'])
                        vfolder.save()
                    except ValidationError as e:
                        errored_count += 1
                        self.stdout.write('FAILED')
                        self.stderr.write(e)
                    else:
                        self.stdout.write('DONE')
                        updated_count += 1

        self.stdout.write("\nErrored: %d\nAdded: %d\n"
                          "Updated: %d\nUnchanged: %d" %
                          (errored_count, added_count, updated_count,
                           len(vfolders) - errored_count - added_count -
                           updated_count))
Example #24
0
def overview(request, translation_project, dir_path, filename=None):
    project = translation_project.project
    language = translation_project.language

    directory = request.directory
    store = request.store
    is_admin = check_permission('administrate', request)

    ctx, cookie_data = get_sidebar_announcements_context(request, project.code,
                                                         language.code)

    ctx.update(get_overview_context(request))

    # TODO improve plugin logic
    if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated():
        from import_export.views import handle_upload_form

        ctx.update(handle_upload_form(request))

        has_download = (check_permission('translate', request) or
                        check_permission('suggest', request))
        ctx.update({
            'display_download': has_download,
            'has_sidebar': True,
        })

    stats = request.resource_obj.get_stats()

    if store is None:
        table_fields = ['name', 'progress', 'total', 'need-translation',
                        'suggestions', 'critical', 'last-updated', 'activity']
        ctx.update({
            'table': {
                'id': 'tp',
                'fields': table_fields,
                'headings': get_table_headings(table_fields),
                'items': get_children(directory),
            }
        })

        vfolders = get_vfolders(directory, all_vfolders=is_admin)
        if len(vfolders) > 0:
            table_fields = ['name', 'priority', 'progress', 'total',
                            'need-translation', 'suggestions', 'critical',
                            'activity']
            ctx.update({
                'vfolders': {
                    'id': 'vfolders',
                    'fields': table_fields,
                    'headings': get_table_headings(table_fields),
                    'items': vfolders,
                },
            })

            #FIXME: set vfolders stats in the resource, don't inject them here.
            stats['vfolders'] = VirtualFolder.get_stats_for(
                directory.pootle_path,
                all_vfolders=is_admin
            )

    ctx.update({
        'parent': get_parent(directory if store is None else store),
        'translation_project': translation_project,
        'project': project,
        'language': language,
        'stats': jsonify(stats),
        'is_admin': is_admin,

        'browser_extends': 'translation_projects/base.html',
    })

    response = render(request, 'browser/overview.html', ctx)

    if cookie_data:
        response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data)

    return response