Esempio n. 1
0
def form_add(request):
    """Page with form to add a theme."""
    if request.method == 'POST':
        form = ThemeFormAdd(request.POST, request.FILES)
        if form.is_valid():
            themefile = request.FILES['themefile']

            # get properties
            content = themefile.read().replace('\r\n', '\n')
            props = Theme.get_props(content)

            # add theme in database
            now = datetime.now()
            theme = Theme(visible=False,
                          name=props['name'],
                          version=props['weechat'],
                          desc=form.cleaned_data['description'],
                          author=form.cleaned_data['author'],
                          mail=form.cleaned_data['mail'],
                          added=now,
                          updated=now)

            # write theme in pending directory
            filename = files_path_join('themes', 'pending', theme.name)
            with open(filename, 'w') as _file:
                _file.write(content)

            # send e-mail
            try:
                subject = 'WeeChat: new theme %s' % theme.name
                body = (
                    ''
                    'Theme      : %s\n'
                    'Version    : %s\n'
                    'Description: %s\n'
                    'Author     : %s\n'
                    'Mail       : %s\n'
                    'Comment    :\n%s\n' %
                    (props['name'], props['weechat'],
                     form.cleaned_data['description'],
                     form.cleaned_data['author'], form.cleaned_data['mail'],
                     form.cleaned_data['comment']))
                sender = '%s <%s>' % (form.cleaned_data['author'],
                                      form.cleaned_data['mail'])
                email = EmailMessage(subject, body, sender,
                                     settings.THEMES_MAILTO)
                email.attach_file(filename)
                email.send()
            except:  # noqa: E722
                return HttpResponseRedirect('/themes/adderror/')

            # save theme in database
            theme.save()

            return HttpResponseRedirect('/themes/addok/')
    else:
        form = ThemeFormAdd()
    try:
        stable_version = Release.objects.get(version='stable').description
        release_stable = Release.objects.get(version=stable_version)
    except ObjectDoesNotExist:
        release_stable = None
    return render(
        request,
        'themes/add.html',
        {
            'release_stable': release_stable,
            'form': form,
        },
    )
Esempio n. 2
0
def form_add(request):
    """Page with form to add a theme."""
    if request.method == 'POST':
        form = ThemeFormAdd(request.POST, request.FILES)
        if form.is_valid():
            themefile = request.FILES['themefile']

            # get properties
            content = get_theme_content(themefile)
            props = Theme.get_props(content)

            # add theme in database
            now = datetime.now()
            theme = Theme(visible=False,
                          name=props['name'],
                          version=props['weechat'],
                          desc=form.cleaned_data['description'],
                          author=form.cleaned_data['author'],
                          mail=form.cleaned_data['mail'],
                          added=now,
                          updated=now)

            # write theme in pending directory
            filename = files_path_join('themes', 'pending', theme.name)
            with open(filename, 'w') as _file:
                _file.write(content)

            # send e-mail
            try:
                subject = f'WeeChat: new theme {theme.name}'
                sender = (f'{form.cleaned_data["author"]} '
                          f'<{form.cleaned_data["mail"]}>')
                body = (f'Theme      : {props["name"]}\n'
                        f'Version    : {props["weechat"]}\n'
                        f'Description: {form.cleaned_data["description"]}\n'
                        f'Author     : {sender}\n'
                        f'Comment    :\n{form.cleaned_data["comment"]}\n')
                email = EmailMessage(subject, body, sender,
                                     settings.THEMES_MAILTO)
                email.attach_file(filename)
                email.send()
            except:  # noqa: E722  pylint: disable=bare-except
                return HttpResponseRedirect('/themes/adderror/')

            # save theme in database
            theme.save()

            return HttpResponseRedirect('/themes/addok/')
    else:
        form = ThemeFormAdd()
    try:
        stable_version = Release.objects.get(version='stable').description
        release_stable = Release.objects.get(version=stable_version)
    except ObjectDoesNotExist:
        release_stable = None
    return render(
        request,
        'themes/add.html',
        {
            'release_stable': release_stable,
            'form': form,
        },
    )