def run(self, opts, args, api): message, language = args context = opts.context name = opts.name hash = Message.hash(message) query = 'message.hash = %s' args = [hash] if context is not None: query += ' AND message.context = %s' args.append(context) else: query += ' AND message.context IS NULL' if name is not None: query += ' AND message.name = %s' args.append(name) else: query += ' AND message.name IS NULL' query += ' AND language = %s' args.append(language) model = api.models.model('draco') transaction = model.transaction() join = (Translation, 'translation', MessageTranslationRelationship, 'INNER') result = transaction.select(Translation, query, args, join=join) if result: transaction.delete(result[0]) transaction.commit() else: self.error('translation does not exist') self.exit(1)
def run(self, opts, args, api): message = args[0] context = opts.context or None name = opts.name or None model = api.models.model('draco') transaction = model.transaction() hash = Message.hash(message) query = 'hash = %s' args = [hash] if context is not None: query += ' AND context = %s' args.append(context) else: query += ' AND context IS NULL' if name is not None: query += ' AND name = %s' args.append(name) else: query += ' AND name IS NULL' result = transaction.select(Message, query, args) if result: transaction.delete(result[0]) transaction.commit() else: self.error('message does not exist') self.exit(1)
def translate(self, message, languages, context=None, name=None): """Translate `message'.""" hash = Message.hash(message) cacheid = (hash, context, name) + tuple(languages) translation = self.m_cache.get(cacheid) if translation: return translation # All translations are looked up where the corresponding message # matches `message' or `name', and that match any of the languages # specified in `languages'. If no results are found, `message' itself # is returned. If results are found, they are ordered by applying the # criteria mentioned below and the first entry is returned: # 1. Translations where the message name matched. # 2. Translations where the message id matched. # 3. Translations that have a language earlier in the `languages' list. # 4. Translations where the context matched. transaction = self.m_models.model('draco').transaction('shared') where = '(message.hash = %s' args = [hash] if name: where += ' OR message.name = %s' args.append(name) where += ')' array = ','.join(['%s'] * len(languages)) where += ' AND language IN (%s)' % array args += languages # Ordering on columns that can contain NULL values: testing whether a # NULL value is equal to something always returns NULL, and therefore # we need to weed those out in the ordering test. order = [] if name is not None: order.append('message.name = %s AND NOT name IS NULL DESC') args.append(name) else: order.append('message.name IS NULL DESC') order.append('message.hash = %s DESC') args.append(hash) order += ['language=%s DESC'] * len(languages) args += languages if context: order.append('message.context = %s AND NOT context IS NULL DESC') args.append(context) order = ','.join(order) join = (Translation, 'translation', MessageTranslationRelationship, 'INNER') result = transaction.select(Translation, where, args, order=order, join=join) if result: translation = result[0]['translation'] else: translation = message self.m_cache.add(cacheid, translation) return translation
def run(self, opts, args, api): message = args[0] name = opts.name or None context = opts.context or None model = api.models.model('draco') transaction = model.transaction() hash = Message.hash(message) message = Message(hash=hash, context=context, name=name, message=message) try: transaction.insert(message) transaction.commit() except ModelError, err: self.error(err) self.exit(1)
def run(self, opts, args, api): message, language, translation = args context = opts.context name = opts.name model = api.models.model('draco') transaction = model.transaction() result = transaction.select(Language, 'language=%s', (language,)) if not result: self.error('language does not exist: %s' % language) self.exit(1) hash = Message.hash(message) query = 'hash = %s' args = [hash] if context is not None: query += ' AND context = %s' args.append(context) else: query += ' AND context IS NULL' if name is not None: query += ' AND name = %s' args.append(name) else: query += ' AND name IS NULL' result = transaction.select(Message, query, tuple(args)) if not result: self.error('message does not exist') self.exit(1) else: message = result[0] trobj = message.translation(language) if trobj: trobj['translation'] = translation else: trobj = Translation(translation=translation, orig_hash=hash) transaction.insert(trobj) relationship = MessageTranslationRelationship() relationship.set_role('message', message) relationship.set_role('translation', trobj) relationship['language'] = language transaction.insert(relationship) transaction.commit()