Skip to content

Instantly share code, notes, and snippets.

@db0company
Created March 18, 2020 15:49
Show Gist options
  • Save db0company/16d38d66111fa1790a11c8b3625fcdc0 to your computer and use it in GitHub Desktop.
Save db0company/16d38d66111fa1790a11c8b3625fcdc0 to your computer and use it in GitHub Desktop.
# Previous code
def fallback_translation_button(self, field_name):
language, translation = self.get_translation(field_name, return_language=True)
return translationURL(translation, from_language=language, with_wrapper=True)
def fallback_translation_button(self, field_name):
language, translation = self.get_translation(field_name, return_language=True)
return translationURL(translation, from_language=language, with_wrapper=True)
t_skill_affects = property(lambda _s: _s.get_skill_affects(get_language()))
skill_affects_sentence = property(lambda _s: _s.get_skill_affects_sentence(language='en'))
t_skill_affects_sentence = property(lambda _s: _s.get_skill_affects_sentence())
japanese_skill_affects_sentence = property(lambda _s: _s.get_skill_affects_sentence(language='ja'))
skill_sentence = property(lambda _s: _s.get_skill_sentence(language='en'))
t_skill_sentence = property(lambda _s: _s.get_skill_sentence())
japanese_skill_sentence = property(lambda _s: _s.get_skill_sentence(language='ja'))
skill_sentences = property(lambda _s: _s.get_skill_sentences(language='en'))
t_skill_sentences = property(lambda _s: _s.get_skill_sentences())
japanese_skill_sentences = property(lambda _s: _s.get_skill_sentences(language='ja'))
@property
def display_skill_name_for_language(self):
language, translation = self.get_translation('skill_name', return_language=True)
if language != get_language():
return translationURL(translation, from_language=language, with_wrapper=True)
return translation
@property
def adisplay_skill(self):
if not self.skill_id:
return None
skills_to_display = OrderedDict([
('language', {
'name_sentence': (self.t_skill_name, self.t_skill_sentence),
'sentences': lambda: self.t_skill_sentences,
'affects': self.t_skill_affects_sentence,
'icon': 'translate',
'image': (
staticImageURL(VERSIONS['WW']['image'], folder='language')
if get_language() in GAMES['ALLSTARS']['languages'] else None
),
}),
] + ([
('english', {
'name_sentence': (self.skill_name or self.japanese_skill_name, self.skill_sentence),
'sentences': lambda: self.skill_sentences,
'affects': self.skill_affects_sentence,
'image': staticImageURL(VERSIONS['WW']['image'], folder='language'),
}),
] if get_language() not in GAMES['ALLSTARS']['languages'] else []) + [
('japanese', {
'name_sentence': (self.japanese_skill_name, self.japanese_skill_sentence),
'sentences': lambda: self.japanese_skill_sentences,
'affects': self.japanese_skill_affects_sentence,
'image': staticImageURL(VERSIONS['JP']['image'], folder='language'),
}),
])
if 'english' in skills_to_display and skills_to_display['language']['name_sentence'] == skills_to_display['english']['name_sentence']:
skills_to_display['language']['image'] = skills_to_display['english']['image']
del(skills_to_display['english'])
print skills_to_display['language']['image']
if skills_to_display['japanese']['name_sentence'] == skills_to_display['language']['name_sentence']:
skills_to_display['language']['image'] = skills_to_display['japanese']['image']
del(skills_to_display['japanese'])
return u"""
<div class="skill-per-card text-center">
<div class="row">
{skills_to_display}
</div>
<p class="change-level" style="display: none;">
<span class="btn-group">
<a href="#changeSkillLevelDecrease" class="btn btn-{attribute} btn-xs disabled">-</a>
<span class="btn btn-{attribute} btn-xs disabled skill-level">
</span>
<a href="#changeSkillLevelIncrease" class="btn btn-{attribute} btn-xs">+</a>
</span>
</p>
</div>
""".format(
skills_to_display=u''.join([
u"""
<div class="col-md-{size}">
<p class="skill skill-{cls} {muted}" data-sentences='{sentences}'>
{image}{icon}<strong class="name">{name}</strong><br>
<span class="sentence">{sentence}</span><br>
{affects} <span class="badge progress-bar-{attribute}">{chance}</span>
</p>
</div>
""".format(
size=int(12 / len(skills_to_display)),
cls=cls,
muted='text-muted' if i > 0 else '',
sentences=jsv(skill['sentences']()),
name=(skill['name_sentence'][0] if cls != 'language' else self.display_skill_name_for_language) or '',
sentence=skill['name_sentence'][1],
image=u'<img src="{}" alt="{}" width="30" style="min-width: unset;"> '.format(skill['image'], cls) if skill.get('image', None) else '',
icon=u'<i class="flaticon-{}"></i> '.format(skill['icon'], cls) if skill.get('icon', None) and not skill.get('image', None) else '',
affects=(
u'<span class="badge progress-bar-{attribute}">{affects}</span>'.format(
attribute=self.attribute,
affects=skill['affects'],
) if skill['affects'] else ''),
attribute=self.attribute,
chance=self.skill_chance,
) for i, (cls, skill) in enumerate(skills_to_display.items())
]),
attribute=self.attribute,
)
##############################################
# New code
@property
def displayed_skills(self):
skills = []
language = get_language()
was_my_language_displayed = False
def _get_translation_url(f, short=False, extra_kwargs={}):
language, translation = f(fallback=True, return_language=True, **extra_kwargs)
if not translation:
return None
return translationURL(
translation, from_language=language, with_wrapper=True, one_line=short,
sentence=u'<i class="flaticon-translate"></i>' if short else False,
)
# Display skill per versions if available in version
for version_name, languages in GAMES['ALLSTARS']['languages_per_version'].items():
is_released = getattr(self, u'{}release_date'.format(VERSIONS[version_name]['prefix']), None)
if is_released:
if language in languages:
was_my_language_displayed = True
displayed_language = language
else:
displayed_language = languages[0]
skills.append({
'language': displayed_language,
'image': staticImageURL(VERSIONS[version_name]['image'], folder='language'),
'name': self.get_skill_name(language=displayed_language, fallback=False),
'sentence': self.get_skill_sentence(language=displayed_language, fallback=False),
'sentences': self.get_skill_sentences(language=displayed_language),
'affects': self.get_skill_affects_sentence(language=displayed_language, fallback=False),
})
skills.sort(key=lambda s: s['language'] != language)
# Display translated skill if translation exists
if not was_my_language_displayed:
skills.insert(0, {
'language': language,
'name': (self.get_skill_name(language=language, fallback=False)
or _get_translation_url(self.get_skill_name)),
'sentence': (self.get_skill_sentence(language=language, fallback=False)
or _get_translation_url(self.get_skill_sentence)),
'icon': 'translate',
'sentences': (
self.get_skill_sentences(language=language, fallback=False)
or { level: _get_translation_url(
self.get_skill_sentence, extra_kwargs={ 'at_level': level })
for level in range(1, self.SKILL_MAX_LEVEL + 1) }),
'affects': (self.get_skill_affects_sentence(language=language, fallback=False)
or _get_translation_url(self.get_skill_affects_sentence, short=True)),
})
return skills
@property
def display_skill(self):
skills = self.displayed_skills
return u"""
<div class="skill-per-card text-center">
<div class="row">
{skills}
</div>
<p class="change-level" style="display: none;">
<span class="btn-group">
<a href="#changeSkillLevelDecrease" class="btn btn-{attribute} btn-xs disabled">-</a>
<span class="btn btn-{attribute} btn-xs disabled skill-level">
</span>
<a href="#changeSkillLevelIncrease" class="btn btn-{attribute} btn-xs">+</a>
</span>
</p>
</div>
""".format(
skills=u''.join([
u"""
<div class="col-md-{size}">
<p class="skill {muted}" data-sentences='{sentences}'>
{image}{icon}<strong class="name">{name}</strong><br>
<span class="sentence">{sentence}</span><br>
{affects} <span class="badge progress-bar-{attribute}">{chance}</span>
</p>
</div>
""".format(
size=int(12 / len(skills)),
muted='text-muted' if i > 0 else '',
sentences=jsv(skill.get('sentences', {})),
name=skill.get('name', None) or '',
sentence=skill.get('sentence', ''),
image=u'<img src="{}" alt="version" width="30" style="min-width: unset;"> '.format(
skill['image']) if skill.get('image', None) else '',
icon=u'<i class="flaticon-{}"></i> '.format(
skill['icon']) if skill.get('icon', None) else '',
affects=(
u'<span class="badge progress-bar-{attribute}">{affects}</span>'.format(
attribute=self.attribute,
affects=skill['affects'],
) if skill.get('affects', '') else ''),
attribute=self.attribute,
chance=self.skill_chance,
) for i, skill in enumerate(skills)
]),
attribute=self.attribute,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment