def get_widgets_url(self): ''' Returns absolute URL for widgets. ''' return get_site_url( reverse('widgets', kwargs={'project': self.slug}) )
def export_stats(request, project, subproject): """ Exports stats in JSON format. """ subprj = get_subproject(request, project, subproject) try: indent = int(request.GET["indent"]) except: indent = None response = [] for trans in subprj.translation_set.all(): response.append( { "code": trans.language.code, "name": trans.language.name, "total": trans.total, "last_change": trans.get_last_change(), "last_author": trans.get_last_author(False), "translated": trans.translated, "translated_percent": trans.get_translated_percent(), "fuzzy": trans.fuzzy, "fuzzy_percent": trans.get_fuzzy_percent(), "failing": trans.get_failing_checks(), "failing_percent": trans.get_failing_checks_percent(), "url": trans.get_share_url(), "url_translate": get_site_url(trans.get_absolute_url()), } ) return HttpResponse(json.dumps(response, default=json_dt_handler, indent=indent), mimetype="application/json")
def get_share_url(self): ''' Returns absolute URL usable for sharing. ''' return get_site_url( reverse('engage', kwargs={'project': self.project.slug}) )
def export_stats(request, project, subproject): ''' Exports stats in JSON format. ''' subprj = get_subproject(request, project, subproject) response = [] for trans in subprj.translation_set.all(): response.append({ 'code': trans.language.code, 'name': trans.language.name, 'total': trans.total, 'last_change': trans.get_last_change(), 'last_author': trans.get_last_author(False), 'translated': trans.translated, 'translated_percent': trans.get_translated_percent(), 'fuzzy': trans.fuzzy, 'fuzzy_percent': trans.get_fuzzy_percent(), 'failing': trans.get_failing_checks(), 'failing_percent': trans.get_failing_checks_percent(), 'url': trans.get_share_url(), 'url_translate': get_site_url(trans.get_absolute_url()), }) return HttpResponse( json.dumps(response, default=dt_handler), mimetype='application/json' )
def download_dictionary_ttkit(export_format, prj, lang, words): ''' Translate-toolkit builder for dictionary downloads. ''' # Use translate-toolkit for other formats if export_format == 'po': # Construct store from translate.storage.po import pofile store = pofile() # Export parameters content_type = 'text/x-po' extension = 'po' has_lang = False # Set po file header store.updateheader( add=True, language=lang.code, x_generator='Weblate %s' % weblate.VERSION, project_id_version='%s (%s)' % (lang.name, prj.name), language_team='%s <%s>' % ( lang.name, get_site_url(reverse( 'show_dictionary', kwargs={'project': prj.slug, 'lang': lang.code} )), ) ) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters content_type = 'application/x-tbx' extension = 'tbx' has_lang = True # Setup response and headers response = HttpResponse(content_type='%s; charset=utf-8' % content_type) filename = 'glossary-%s-%s.%s' % (prj.slug, lang.code, extension) response['Content-Disposition'] = 'attachment; filename=%s' % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response store.savefile(response) return response
def download_dictionary_ttkit(export_format, prj, lang, words): ''' Translate-toolkit builder for dictionary downloads. ''' # Use translate-toolkit for other formats if export_format == 'po': # Construct store from translate.storage.po import pofile store = pofile() # Export parameters mimetype = 'text/x-po' extension = 'po' has_lang = False # Set po file header store.updateheader( add=True, language=lang.code, x_generator='Weblate %s' % weblate.VERSION, project_id_version='%s (%s)' % (lang.name, prj.name), language_team='%s <%s>' % ( lang.name, get_site_url(reverse( 'show_dictionary', kwargs={'project': prj.slug, 'lang': lang.code} )), ) ) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters mimetype = 'application/x-tbx' extension = 'tbx' has_lang = True # Setup response and headers response = HttpResponse(mimetype='%s; charset=utf-8' % mimetype) filename = 'glossary-%s-%s.%s' % (prj.slug, lang.code, extension) response['Content-Disposition'] = 'attachment; filename=%s' % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response store.savefile(response) return response
def get_share_url(self): ''' Returns absolute URL usable for sharing. ''' return get_site_url( reverse('engage-lang', kwargs={ 'project': self.subproject.project.slug, 'lang': self.language.code }))
def download_dictionary_ttkit(export_format, prj, lang, words): """ Translate-toolkit builder for dictionary downloads. """ # Use translate-toolkit for other formats if export_format == "po": # Construct store from translate.storage.po import pofile store = pofile() # Export parameters mimetype = "text/x-po" extension = "po" has_lang = False # Set po file header store.updateheader( add=True, language=lang.code, x_generator="Weblate %s" % weblate.VERSION, project_id_version="%s (%s)" % (lang.name, prj.name), language_team="%s <%s>" % (lang.name, get_site_url(reverse("show_dictionary", kwargs={"project": prj.slug, "lang": lang.code}))), ) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters mimetype = "application/x-tbx" extension = "tbx" has_lang = True # Setup response and headers response = HttpResponse(mimetype="%s; charset=utf-8" % mimetype) filename = "glossary-%s-%s.%s" % (prj.slug, lang.code, extension) response["Content-Disposition"] = "attachment; filename=%s" % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response store.savefile(response) return response
def get_share_url(self): ''' Returns absolute URL usable for sharing. ''' return get_site_url( reverse( 'engage-lang', kwargs={ 'project': self.subproject.project.slug, 'lang': self.language.code } ) )
def get_widgets_url(self): ''' Returns absolute URL for widgets. ''' return get_site_url( '%s?lang=%s' % ( reverse( 'widgets', kwargs={ 'project': self.subproject.project.slug, } ), self.language.code, ) )
def weblate_context(request): """ Context processor to inject various useful variables into context. """ return { "version": weblate.VERSION, "weblate_url": URL_BASE % weblate.VERSION, "donate_url": URL_DONATE % weblate.VERSION, "site_title": appsettings.SITE_TITLE, "site_url": get_site_url(), "offer_hosting": appsettings.OFFER_HOSTING, "demo_server": appsettings.DEMO_SERVER, "current_date": datetime.utcnow().strftime("%Y-%m-%d"), "current_year": datetime.utcnow().strftime("%Y"), "current_month": datetime.utcnow().strftime("%m"), "current_url": request.get_full_path(), "mt_enabled": appsettings.MACHINE_TRANSLATION_ENABLED, "hooks_enabled": appsettings.ENABLE_HOOKS, "registration_open": appsettings.REGISTRATION_OPEN, }
def export_stats(request, project, subproject): ''' Exports stats in JSON format. ''' subprj = get_subproject(request, project, subproject) try: indent = int(request.GET['indent']) except: indent = None response = [] for trans in subprj.translation_set.all(): response.append({ 'code': trans.language.code, 'name': trans.language.name, 'total': trans.total, 'last_change': trans.get_last_change(), 'last_author': trans.get_last_author(False), 'translated': trans.translated, 'translated_percent': trans.get_translated_percent(), 'fuzzy': trans.fuzzy, 'fuzzy_percent': trans.get_fuzzy_percent(), 'failing': trans.get_failing_checks(), 'failing_percent': trans.get_failing_checks_percent(), 'url': trans.get_share_url(), 'url_translate': get_site_url(trans.get_absolute_url()), }) return HttpResponse( json.dumps( response, default=json_dt_handler, indent=indent, ), mimetype='application/json' )
def send_notification_email( language, email, notification, translation_obj, context=None, headers=None, from_email=None ): """ Renders and sends notification email. """ cur_language = django_translation.get_language() if context is None: context = {} if headers is None: headers = {} try: weblate.logger.info("sending notification %s on %s to %s", notification, translation_obj.__unicode__(), email) # Load user language django_translation.activate(language) # Template names subject_template = "mail/%s_subject.txt" % notification body_template = "mail/%s.txt" % notification html_body_template = "mail/%s.html" % notification # Adjust context site = Site.objects.get_current() context["translation"] = translation_obj context["current_site"] = site.domain context["site"] = site context["translation_url"] = get_site_url(translation_obj.get_absolute_url()) context["subject_template"] = subject_template # Render subject subject = render_to_string(subject_template, context) # Render body body = render_to_string(body_template, context) html_body = render_to_string(html_body_template, context) # Define headers headers["Auto-Submitted"] = "auto-generated" headers["X-AutoGenerated"] = "yes" headers["Precedence"] = "bulk" headers["X-Mailer"] = "Weblate %s" % weblate.VERSION if email == "ADMINS": # Special handling for ADMINS mail_admins(subject.strip(), body, html_message=html_body) else: # Create message email = EmailMultiAlternatives( settings.EMAIL_SUBJECT_PREFIX + subject.strip(), body, to=[email], headers=headers, from_email=from_email, ) email.attach_alternative(html_body, "text/html") # Send it out email.send(fail_silently=False) finally: django_translation.activate(cur_language)
def update_unit(self, unit, request, user=None): ''' Updates backend file and unit. ''' if user is None: user = request.user # Save with lock acquired with self.subproject.git_lock: src = unit.get_source_plurals()[0] add = False pounit, add = self.store.find_unit(unit.context, src) # Bail out if we have not found anything if pounit is None: return False, None # Check for changes if (not add and unit.target == pounit.get_target() and unit.fuzzy == pounit.is_fuzzy()): return False, pounit # Store translations if unit.is_plural(): pounit.set_target(unit.get_target_plurals()) else: pounit.set_target(unit.target) # Update fuzzy flag pounit.mark_fuzzy(unit.fuzzy) # Optionally add unit to translation file if add: self.store.add_unit(pounit) # We need to update backend now author = self.get_author_name(user) # Update po file header po_revision_date = ( datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring() ) # Prepare headers to update headers = { 'add': True, 'last_translator': author, 'plural_forms': self.language.get_plural_form(), 'language': self.language_code, 'PO_Revision_Date': po_revision_date, 'x_generator': 'Weblate %s' % weblate.VERSION } # Optionally store language team with link to website if self.subproject.project.set_translation_team: headers['language_team'] = '%s <%s>' % ( self.language.name, get_site_url(self.get_absolute_url()), ) # Optionally store email for reporting bugs in source report_source_bugs = self.subproject.report_source_bugs if report_source_bugs != '': headers['report_msgid_bugs_to'] = report_source_bugs # Update genric headers self.store.update_header( **headers ) # commit possible previous changes (by other author) self.commit_pending(request, author) # save translation changes self.store.save() # commit Git repo if needed self.git_commit(request, author, timezone.now(), sync=True) return True, pounit
def widgets(request, project): obj = get_project(request, project) # Parse possible language selection form = EnageLanguageForm(obj, request.GET) lang = None if form.is_valid() and form.cleaned_data['lang'] != '': lang = Language.objects.get(code=form.cleaned_data['lang']) if lang is None: engage_base = reverse('engage', kwargs={'project': obj.slug}) else: engage_base = reverse('engage-lang', kwargs={ 'project': obj.slug, 'lang': lang.code }) engage_url = get_site_url(engage_base) engage_url_track = '%s?utm_source=widget' % engage_url widget_base_url = get_site_url( reverse('widgets', kwargs={'project': obj.slug})) widget_list = [] for widget_name in WIDGETS: widget_class = WIDGETS[widget_name] color_list = [] for color in widget_class.colors: if lang is None: color_url = reverse('widget-image', kwargs={ 'project': obj.slug, 'widget': widget_name, 'color': color, }) else: color_url = reverse('widget-image-lang', kwargs={ 'project': obj.slug, 'widget': widget_name, 'color': color, 'lang': lang.code }) color_list.append({ 'name': color, 'url': get_site_url(color_url), }) widget_list.append({ 'name': widget_name, 'colors': color_list, }) return render_to_response( 'widgets.html', RequestContext( request, { 'engage_url': engage_url, 'engage_url_track': engage_url_track, 'widget_list': widget_list, 'widget_base_url': widget_base_url, 'object': obj, 'image_src': widget_list[0]['colors'][0]['url'], 'form': form, }))
def send_notification_email(language, email, notification, translation_obj=None, context=None, headers=None, user=None, info=None): ''' Renders and sends notification email. ''' cur_language = django_translation.get_language() if context is None: context = {} if headers is None: headers = {} try: if info is None: info = translation_obj.__unicode__() weblate.logger.info( 'sending notification %s on %s to %s', notification, info, email ) # Load user language if language is not None: django_translation.activate(language) # Template names subject_template = 'mail/{}_subject.txt'.format(notification) body_template = 'mail/{}.txt'.format(notification) html_body_template = 'mail/{}.html'.format(notification) # Adjust context site = Site.objects.get_current() context['current_site'] = site.domain context['site'] = site if translation_obj is not None: context['translation'] = translation_obj context['translation_url'] = get_site_url( translation_obj.get_absolute_url() ) context['subject_template'] = subject_template # Render subject subject = render_to_string(subject_template, context).strip() # Render body body = render_to_string(body_template, context) html_body = render_to_string(html_body_template, context) # Define headers headers['Auto-Submitted'] = 'auto-generated' headers['X-AutoGenerated'] = 'yes' headers['Precedence'] = 'bulk' headers['X-Mailer'] = 'Weblate {}'.format(weblate.VERSION) # Reply to header if user is not None: headers['Reply-To'] = user.email # List of recipients if email == 'ADMINS': emails = [a[1] for a in settings.ADMINS] else: emails = [email] # Create message email = EmailMultiAlternatives( settings.EMAIL_SUBJECT_PREFIX + subject, body, to=emails, headers=headers, ) email.attach_alternative( html_body, 'text/html' ) # Send it out email.send(fail_silently=False) finally: django_translation.activate(cur_language)
def send_notification_email(language, email, notification, translation_obj, context=None, headers=None, from_email=None): ''' Renders and sends notification email. ''' cur_language = django_translation.get_language() if context is None: context = {} if headers is None: headers = {} try: logger.info( 'sending notification %s on %s to %s', notification, translation_obj.__unicode__(), email ) # Load user language django_translation.activate(language) # Template names subject_template = 'mail/%s_subject.txt' % notification body_template = 'mail/%s.txt' % notification html_body_template = 'mail/%s.html' % notification # Adjust context domain = Site.objects.get_current().domain context['translation'] = translation_obj context['current_site'] = domain context['translation_url'] = get_site_url( translation_obj.get_absolute_url() ) context['subject_template'] = subject_template # Render subject subject = render_to_string(subject_template, context) # Render body body = render_to_string(body_template, context) html_body = render_to_string(html_body_template, context) # Define headers headers['Auto-Submitted'] = 'auto-generated' headers['X-AutoGenerated'] = 'yes' headers['Precedence'] = 'bulk' headers['X-Mailer'] = 'Weblate %s' % weblate.VERSION if email == 'ADMINS': # Special handling for ADMINS mail_admins( subject.strip(), body, html_message=html_body ) else: # Create message email = EmailMultiAlternatives( settings.EMAIL_SUBJECT_PREFIX + subject.strip(), body, to=[email], headers=headers, from_email=from_email, ) email.attach_alternative( html_body, 'text/html' ) # Send it out email.send(fail_silently=False) finally: django_translation.activate(cur_language)
def send_notification_email(language, email, notification, translation_obj, context=None, headers=None, from_email=None): ''' Renders and sends notification email. ''' cur_language = django_translation.get_language() if context is None: context = {} if headers is None: headers = {} try: weblate.logger.info('sending notification %s on %s to %s', notification, translation_obj.__unicode__(), email) # Load user language django_translation.activate(language) # Template names subject_template = 'mail/%s_subject.txt' % notification body_template = 'mail/%s.txt' % notification html_body_template = 'mail/%s.html' % notification # Adjust context site = Site.objects.get_current() context['translation'] = translation_obj context['current_site'] = site.domain context['site'] = site context['translation_url'] = get_site_url( translation_obj.get_absolute_url()) context['subject_template'] = subject_template # Render subject subject = render_to_string(subject_template, context) # Render body body = render_to_string(body_template, context) html_body = render_to_string(html_body_template, context) # Define headers headers['Auto-Submitted'] = 'auto-generated' headers['X-AutoGenerated'] = 'yes' headers['Precedence'] = 'bulk' headers['X-Mailer'] = 'Weblate %s' % weblate.VERSION if email == 'ADMINS': # Special handling for ADMINS mail_admins(subject.strip(), body, html_message=html_body) else: # Create message email = EmailMultiAlternatives( settings.EMAIL_SUBJECT_PREFIX + subject.strip(), body, to=[email], headers=headers, from_email=from_email, ) email.attach_alternative(html_body, 'text/html') # Send it out email.send(fail_silently=False) finally: django_translation.activate(cur_language)
def update_unit(self, unit, request, user=None): ''' Updates backend file and unit. ''' if user is None: user = request.user # Save with lock acquired with self.subproject.git_lock: src = unit.get_source_plurals()[0] add = False pounit, add = self.store.find_unit(unit.context, src) # Bail out if we have not found anything if pounit is None: return False, None # Check for changes if (unit.target == pounit.get_target() and unit.fuzzy == pounit.is_fuzzy()): return False, pounit # Store translations if unit.is_plural(): pounit.set_target(unit.get_target_plurals()) else: pounit.set_target(unit.target) # Update fuzzy flag pounit.mark_fuzzy(unit.fuzzy) # Optionally add unit to translation file if add: self.store.add_unit(pounit) # We need to update backend now author = self.get_author_name(user) # Update po file header po_revision_date = (datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring()) # Prepare headers to update headers = { 'add': True, 'last_translator': author, 'plural_forms': self.language.get_plural_form(), 'language': self.language_code, 'PO_Revision_Date': po_revision_date, 'x_generator': 'Weblate %s' % weblate.VERSION } # Optionally store language team with link to website if self.subproject.project.set_translation_team: headers['language_team'] = '%s <%s>' % ( self.language.name, get_site_url(self.get_absolute_url()), ) # Optionally store email for reporting bugs in source report_source_bugs = self.subproject.report_source_bugs if report_source_bugs != '': headers['report_msgid_bugs_to'] = report_source_bugs # Update genric headers self.store.update_header(**headers) # commit possible previous changes (by other author) self.commit_pending(request, author) # save translation changes self.store.save() # commit Git repo if needed self.git_commit(request, author, timezone.now(), sync=True) return True, pounit
def title(request): return { 'site_title': appsettings.SITE_TITLE, 'site_url': get_site_url(), 'offer_hosting': appsettings.OFFER_HOSTING, }
def widgets(request, project): obj = get_project(request, project) # Parse possible language selection form = EnageLanguageForm(obj, request.GET) lang = None if form.is_valid() and form.cleaned_data['lang'] != '': lang = Language.objects.get(code=form.cleaned_data['lang']) if lang is None: engage_base = reverse('engage', kwargs={'project': obj.slug}) else: engage_base = reverse( 'engage-lang', kwargs={'project': obj.slug, 'lang': lang.code} ) engage_url = get_site_url(engage_base) engage_url_track = '%s?utm_source=widget' % engage_url widget_base_url = get_site_url( reverse('widgets', kwargs={'project': obj.slug}) ) widget_list = [] for widget_name in WIDGETS: widget_class = WIDGETS[widget_name] color_list = [] for color in widget_class.colors: if lang is None: color_url = reverse( 'widget-image', kwargs={ 'project': obj.slug, 'widget': widget_name, 'color': color, } ) else: color_url = reverse( 'widget-image-lang', kwargs={ 'project': obj.slug, 'widget': widget_name, 'color': color, 'lang': lang.code } ) color_list.append({ 'name': color, 'url': get_site_url(color_url), }) widget_list.append({ 'name': widget_name, 'colors': color_list, }) return render_to_response('widgets.html', RequestContext(request, { 'engage_url': engage_url, 'engage_url_track': engage_url_track, 'widget_list': widget_list, 'widget_base_url': widget_base_url, 'object': obj, 'image_src': widget_list[0]['colors'][0]['url'], 'form': form, }))