def do_restart(request):
    """
    * "test" for a django instance (this do a touch over settings.py for reload)
    * "apache"
    * "httpd"
    * "wsgi"
    * "restart_script <script_path_name>"
    """
    if get_user_can_translate(request.user):
        reload_method = get_auto_reload_method()
        reload_log = get_auto_reload_log()
        reload_time = get_auto_reload_time()
        command = "echo no script"
        if reload_method == 'test':
            command = 'touch "%s"' % os.path.join(settings.BASEDIR, 'settings.py')
        ## No RedHAT or similars
        elif reload_method == 'apache2':
            command = 'sudo apache2ctl restart'
        ## RedHAT, CentOS
        elif reload_method == 'httpd':
            command = 'sudo service httpd restart'

        elif reload_method.startswith('restart_script'):
            command = ' '.join(reload_method.split(" ")[1:])
        if os.path.exists(os.path.dirname(reload_log)):
            os.system("sleep 2 && %s &> %s & " % (command, reload_log))
        else:
            print('The AUTO_RELOAD_LOG directory do not exist')  # Just in case our stdout is logged somewhere
            os.system("sleep 2 && %s & " % command)
        return render_to_response('inlinetrans/response.html',
                                  {'message': reload_time},
                                  context_instance=RequestContext(request))
Example #2
0
def do_restart(request):
    """
    * "test" for a django instance (this do a touch over settings.py for reload)
    * "apache"
    * "httpd"
    * "wsgi"
    * "restart_script <script_path_name>"
    """
    if get_user_can_translate(request.user):
        reload_method = get_auto_reload_method()
        reload_log = get_auto_reload_log()
        reload_time = get_auto_reload_time()
        command = "echo no script"
        if reload_method == 'test':
            command = 'touch %s' % os.path.join(settings.BASEDIR,
                                                'settings.py')
        ## No RedHAT or similars
        elif reload_method == 'apache2':
            command = 'sudo apache2ctl restart'
        ## RedHAT, CentOS
        elif reload_method == 'httpd':
            command = 'sudo service httpd restart'

        elif reload_method.startswith('restart_script'):
            command = ' '.join(reload_method.split(" ")[1:])
        if os.path.exists(os.path.dirname(reload_log)):
            os.system("sleep 2 && %s &> %s & " % (command, reload_log))
        else:
            print('The AUTO_RELOAD_LOG directory do not exist'
                  )  # Just in case our stdout is logged somewhere
            os.system("sleep 2 && %s & " % command)
        return render_to_response('inlinetrans/response.html',
                                  {'message': reload_time},
                                  context_instance=RequestContext(request))
def set_new_translation(request):
    """
    Post to include a new translation for a msgid
    """
    if not get_user_can_translate(request.user):
        return HttpResponseForbidden(_('You have no permission to update translation catalogs'))
    if not request.POST:
        return HttpResponseBadRequest(render_to_response('inlinetrans/response.html',
                                      {'message': _('Invalid request method')},
                                      context_instance=RequestContext(request)))
    else:
        result = {'errors': True,
                  'question': False,
                  'message': _('Unknow error')}
        selected_pofile = None
        msgid = smart_str(request.POST['msgid'])
        msgstr = smart_str(request.POST['msgstr'])
        retry = smart_str(request.POST['retry'])
        lang = get_language()

        # We try to update the catalog
        if retry != 'false':
            root_path = os.path.dirname(os.path.normpath(os.sys.modules[settings.SETTINGS_MODULE].__file__))
            locale_path = os.path.dirname(os.path.normpath(os.sys.modules[settings.SETTINGS_MODULE].__file__))
            makemessages(lang, extensions=['.html'], root_path=root_path, locale_path=locale_path)

        pos = find_pos(lang, include_djangos=True)
        if pos:
            for file_po in pos:
                candidate = pofile(file_po)
                poentry = candidate.find(msgid)
                if poentry:
                    selected_pofile = candidate
                    poentry.msgstr = msgstr
                    if 'fuzzy' in poentry.flags:
                        poentry.flags.remove('fuzzy')
                    po_filename = file_po
                    break
            # We can not find the msgid in any of the catalogs
            if not selected_pofile:
                result['message'] = _('"%(msgid)s" not found in any catalog' % {'msgid': msgid})
                if retry == 'false':
                    result['question'] = _('Do you want to update the catalog (this could take longer) and try again?')
                return HttpResponse(simplejson.dumps(result), mimetype='text/plain')

            format_errors = validate_format(selected_pofile)
            if format_errors:
                result['message'] = format_errors
                return HttpResponse(simplejson.dumps(result), mimetype='text/plain')

            if poentry and not format_errors:
                try:
                    selected_pofile.metadata['Last-Translator'] = smart_str("%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email))
                    selected_pofile.metadata['X-Translated-Using'] = smart_str("inlinetrans %s" % inlinetrans.get_version(False))
                    selected_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except (UnicodeDecodeError, AttributeError):
                    pass
                selected_pofile.save()
                selected_pofile.save_as_mofile(po_filename.replace('.po', '.mo'))
                result['errors'] = False
                result['message'] = _('Catalog updated successfully')
            elif not poentry:
                result['message'] = _('PO entry not found')
    return HttpResponse(simplejson.dumps(result), mimetype='text/plain')
Example #4
0
def set_new_translation(request):
    """
    Post to include a new translation for a msgid
    """
    if not get_user_can_translate(request.user):
        return HttpResponseForbidden(
            _('You have no permission to update translation catalogs'))
    if not request.POST:
        return HttpResponseBadRequest(
            render_to_response('inlinetrans/response.html',
                               {'message': _('Invalid request method')},
                               context_instance=RequestContext(request)))
    else:
        result = {
            'errors': True,
            'question': False,
            'message': _('Unknow error')
        }
        selected_pofile = None
        msgid = smart_str(request.POST['msgid'])
        msgstr = smart_str(request.POST['msgstr'])
        retry = smart_str(request.POST['retry'])
        lang = get_language()

        # We try to update the catalog
        if retry != 'false':
            root_path = os.path.dirname(
                os.path.normpath(
                    os.sys.modules[settings.SETTINGS_MODULE].__file__))
            locale_path = os.path.dirname(
                os.path.normpath(
                    os.sys.modules[settings.SETTINGS_MODULE].__file__))
            makemessages(lang,
                         extensions=['.html'],
                         root_path=root_path,
                         locale_path=locale_path)

        pos = find_pos(lang, include_djangos=True)
        if pos:
            for file_po in pos:
                candidate = pofile(file_po)
                poentry = candidate.find(msgid)
                if poentry:
                    selected_pofile = candidate
                    poentry.msgstr = msgstr
                    if 'fuzzy' in poentry.flags:
                        poentry.flags.remove('fuzzy')
                    po_filename = file_po
                    break
            # We can not find the msgid in any of the catalogs
            if not selected_pofile:
                result['message'] = _('"%(msgid)s" not found in any catalog' %
                                      {'msgid': msgid})
                if retry == 'false':
                    result['question'] = _(
                        'Do you want to update the catalog (this could take longer) and try again?'
                    )
                return HttpResponse(simplejson.dumps(result),
                                    mimetype='text/plain')

            format_errors = validate_format(selected_pofile)
            if format_errors:
                result['message'] = format_errors
                return HttpResponse(simplejson.dumps(result),
                                    mimetype='text/plain')

            if poentry and not format_errors:
                try:
                    selected_pofile.metadata['Last-Translator'] = smart_str(
                        "%s %s <%s>" %
                        (request.user.first_name, request.user.last_name,
                         request.user.email))
                    selected_pofile.metadata['X-Translated-Using'] = smart_str(
                        "inlinetrans %s" % inlinetrans.get_version(False))
                    selected_pofile.metadata[
                        'PO-Revision-Date'] = datetime.datetime.now().strftime(
                            '%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                selected_pofile.save()
                selected_pofile.save_as_mofile(
                    po_filename.replace('.po', '.mo'))
                result['errors'] = False
                result['message'] = _('Catalog updated successfully')
            elif not poentry:
                result['message'] = _('PO entry not found')
    return HttpResponse(simplejson.dumps(result), mimetype='text/plain')