예제 #1
0
    def endElement(self, name):
        if not self._gadget_added:
            #add index to the translation list
            value = self.addIndex(self._accumulator)

        if name == 'Name':
            if value:
                raise TemplateParseException(_("ERROR: The element Name cannot be translated"))
            self._name = self._accumulator[0]
            return
        if name == 'DisplayName':
            self._displayName = self._accumulator[0]
            return
        if name == 'Organization':
            self.processOrganization(self._accumulator)
            return
        if name == 'Vendor':
            if value:
                raise TemplateParseException(_("ERROR: The element Vendor cannot be translated"))
            self._vendor = self._accumulator[0]
            return
        if name == 'Version':
            if value:
                raise TemplateParseException(_("ERROR: The element Version cannot be translated"))
            self.processVersion(self._accumulator)
            return
        if name == 'Author':
            self._author = self._accumulator[0]
            return
        if name == 'Description':
            self._description = string.join(self._accumulator, "")
            return
        if name == 'Mail':
            self._mail = self._accumulator[0]
            return
        if name == 'ImageURI':
            if self._accumulator == []:
                self._imageURI = ''
            else:
                self._imageURI = urlparse.urljoin(self._uri, self._accumulator[0])
            return
        if name == 'iPhoneImageURI':
            if self._accumulator == []:
                self._iPhoneImageURI = ''
            else:
                self._iPhoneImageURI = urlparse.urljoin(self._uri, self._accumulator[0])
            return
        if name == 'WikiURI':
            if self._accumulator == []:
                self._wikiURI = ''
            else:
                self._wikiURI = self._accumulator[0]
            return

        if name in ('Tab', 'Position', 'Rendering', 'Preference', 'Property', 'IncludedResources', 'Resource', 'Capability', 'Param'):
            return

        if name == 'Catalog.ResourceDescription':

            if self._gadget_added:
                return

            required_fields = []
            if self._name == '':
                required_fields.append('name')
            if self._vendor == '':
                required_fields.append('vendor')
            if self._version == '':
                required_fields.append('version')
            if self._author == '':
                required_fields.append('author')
            if self._description == '':
                required_fields.append('description')
            if self._mail == '':
                required_fields.append('mail')
            if self._imageURI == '':
                required_fields.append('imageURI')
            if self._wikiURI == '':
                required_fields.append('wikiURI')

            if len(required_fields) > 0:
                raise TemplateParseException(_("ERROR: The following fields are missing in resource description: %(fields)s") % {'fields': required_fields})

            gadget = CatalogueResource()

            gadget.short_name = self._name
            gadget.display_name = self._displayName
            gadget.vendor = self._vendor
            gadget.version = self._version
            gadget.author = self._author
            gadget.description = self._description
            gadget.mail = self._mail
            gadget.image_uri = self._imageURI
            gadget.iphone_image_uri = self._iPhoneImageURI
            gadget.wiki_page_uri = self._wikiURI
            gadget.template_uri = self._uri
            gadget.creation_date = datetime.today()
            gadget.popularity = '0.0'
            gadget.fromWGT = self.fromWGT
            if self._is_mashup:
                gadget.type = 1
            else:
                gadget.type = 0

            # Checking certification status
            gadget.certification = get_certification_status(self._user)

            gadget.creator = self._user

            self._gadget = gadget

            if self.save and self._is_mashup:
                published_mashup = create_published_workspace_from_template(self._xml, gadget, self._user)
                gadget.template_uri = published_mashup.get_uri()

            if self.save:
                gadget.save()

            if self.save:
                # A gadget belongs to many organizations
                for organization in self._organization_list:
                    gadget.organization.add(organization)

                # TODO: process the resources
                # workaround to add default tags
                if self._is_mashup:
                    from catalogue.utils import tag_resource
                    tag_resource(self._user, 'mashup', gadget)

                # Copy all UserTag and UserVote entry from previous version
                resource_versions = get_all_resource_versions(self._vendor, self._name)

                resource_version = map(int, gadget.version.split('.'))
                previous_versions = [v for v in resource_versions if v < resource_version]
                if len(previous_versions) > 0:
                    previous_version_string = '.'.join(map(str, max(previous_versions)))
                    previousVersion = CatalogueResource.objects.get(vendor=self._vendor, short_name=self._name, version=previous_version_string)

                    previousUserTags = UserTag.objects.filter(idResource=previousVersion)

                    for previousUserTag in previousUserTags:
                        newUserTag, created = UserTag.objects.get_or_create(tag=previousUserTag.tag, idUser=previousUserTag.idUser, idResource=gadget)

                    previousUserVotes = UserVote.objects.filter(idResource=previousVersion)

                    for previousUserVote in previousUserVotes:
                        newUserVote = UserVote(idUser=previousUserVote.idUser, vote=previousUserVote.vote, idResource=gadget)
                        newUserVote.save()

                    update_resource_popularity(gadget)

            self._gadget_added = True

        elif self._gadget_added and name == "msg":
            if not self.current_text in self.translatable_list:
                #message not used in the catalogue
                return
            if self.current_lang == self.default_lang:
                self.translated_list.append(self.current_text)

            table_ = self._gadget.__class__.__module__ + "." + self._gadget.__class__.__name__
            trans = Translation(text_id=self.current_text, element_id=self._gadget.id, table=table_, language=self.current_lang, value=self._accumulator[0], default=(self.current_lang == self.default_lang))
            trans.save()
        elif self._gadget_added and name == "Translation":

            if self.current_lang == self.default_lang:
                self.missing_translations = []

                for ind in self.translatable_list:
                    if not ind in self.translated_list:
                        self.missing_translations.append(ind)
                    else:
                        self.translated_list.remove(ind)

                if len(self.missing_translations) > 0:
                    raise TemplateParseException(_("ERROR: the following translation indexes need a default value: " + ', '.join(self.missing_translations)))

                # No triggered as not used indexes are currently filtered
                if len(self.translated_list) > 0:
                    raise TemplateParseException(_("ERROR: the following translation indexes are not used: " + str(self.translated_list)))

        elif self._gadget_added and (name == "Translations"):
            if len(self.lang_list) > 0 and not self.default_lang in self.lang_list:
                raise TemplateParseException(_("ERROR: There isn't a Translation element with the default language (%(default_lang)s) translations") % {'default_lang': self.default_lang})
        elif self._gadget_added:
            return
        else:
            raise TemplateParseException(_("ERROR: missing Resource description field at Resource element! Check schema!"))
예제 #2
0
    gadgetVersions.sort()
    previousVersion = GadgetResource.objects.get(vendor=gadget['vendor'], short_name=gadget['short_name'], version=gadgetVersions[0])
    for i in range(1, len(gadgetVersions)):
        currentVersion = GadgetResource.objects.get(vendor=gadget['vendor'], short_name=gadget['short_name'], version=gadgetVersions[i])

        # Update UserTags
        previousUserTags = UserTag.objects.filter(idResource=previousVersion)

        for previousUserTag in previousUserTags:
            try:
                UserTag.objects.get(tag=previousUserTag.tag, idUser=previousUserTag.idUser, idResource=currentVersion)
            except:
                newUserTag = UserTag(tag=previousUserTag.tag, idUser=previousUserTag.idUser, idResource=currentVersion)
                newUserTag.save()

        # Update UserVotes
        previousUserVotes = UserVote.objects.filter(idResource=previousVersion)

        for previousUserVote in previousUserVotes:
            try:
                UserVote.objects.get(idUser=previousUserVote.idUser, idResource=currentVersion)
            except:
                newUserVote = UserVote(idUser=previousUserVote.idUser, vote=previousUserVote.vote, idResource=currentVersion)
                newUserVote.save()

        # Update popularity
        update_gadget_popularity(currentVersion)

        previousVersion = currentVersion