def merengue_messagebox(context):
     request = context['request']
     portal_messages = messages.get_messages(request)
     if not portal_messages:
         if 'form' in context:
             form = context['form']
             if getattr(form, 'errors', []):
                 if form.non_field_errors():
                     send_error(request, form.non_field_errors())
                 send_error(request, _('Form filled has errors. Please correct'))
                 portal_messages = messages.get_messages(request)
     return {'portal_messages': portal_messages}
Пример #2
0
 def merengue_messagebox(context):
     request = context['request']
     portal_messages = messages.get_messages(request)
     if not portal_messages:
         if 'form' in context:
             form = context['form']
             if getattr(form, 'errors', []):
                 if form.non_field_errors():
                     send_error(request, form.non_field_errors())
                 send_error(request,
                            _('Form filled has errors. Please correct'))
                 portal_messages = messages.get_messages(request)
     return {'portal_messages': portal_messages}
Пример #3
0
    def save(self, request, content, contact_form):
        data = self.cleaned_data
        if contact_form.subject_fixed:
            subject = contact_form.subject
        else:
            subject = data['subject']

        if contact_form.sender_email:
            from_mail = data['sender_email']
        else:
            if request.user.is_authenticated():
                from_mail = request.user.email
            else:
                from_mail = settings.DEFAULT_FROM_EMAIL

        opts_to_save = {}
        opts_to_send = SortedDict()
        files = {}
        for opt in contact_form.opts.all():
            name = opt.name or 'option_%s' % opt.id
            val = data.get(name, _('Unset'))
            if val is None:
                val = ''
            label_field = transmeta.get_real_fieldname('label', settings.LANGUAGE_CODE)
            label = defaultfilters.slugify(getattr(opt, label_field))
            if opt.field_type == 'file':
                files[label] = val
            else:
                opts_to_send[opt.label] = val
                opts_to_save[label] = val
        contactuser = {}
        opts_to_save[u'subject'] = subject
        if request.user.is_authenticated():
            opts_to_save[u'user'] = request.user.username
            contactuser[u'name'] = request.user.get_full_name()
            contactuser[u'username'] = request.user.username
        else:
            opts_to_save[u'user'] = '******'
            contactuser[u'name'] = _('Anonymous')
            contactuser[u'username'] = '******'

        opts_to_save[u'mailfrom'] = from_mail
        sent = SentContactForm(contact_form=contact_form,
                                sent_msg=simplejson.dumps(opts_to_save,
                                                            cls=DateTimeAwareJSONEncoder))
        if request.user.is_authenticated():
            sent.sender = request.user
        sent.save()

        html_content = render_to_string('contactform/email.html',
                                        {'opts': opts_to_send,
                                            'site_domain': Site.objects.get_current().domain,
                                            'content': content,
                                            'contact_form': contact_form,
                                            'contactuser': contactuser},
            context_instance=RequestContext(request))

        def mailstr_to_list(mailstr):
            return map(unicode.strip, mailstr.split(','))

        to_mail = mailstr_to_list(contact_form.email)
        bcc_mail = mailstr_to_list(contact_form.bcc)

        attachs = [(f.name, f.read(), f.content_type) for f in files.values() if f]
        plain_content = defaultfilters.striptags(html_content)
        email = EmailMultiAlternatives(subject, plain_content, from_mail,
                                        to_mail, bcc=bcc_mail,
                                        attachments=attachs)
        email.attach_alternative(html_content, "text/html")
        try:
            email.send()
        except SMTPException, e:
            send_error(request, _('Mail Server Error: %s') % e)
            return None
Пример #4
0
def action(request,
           repository_name,
           path,
           base_template=FILEBROWSER_BASE_TEMPLATE,
           url_prefix=None):
    """ process rename, delete, etc. actions in files and directories """
    repository = get_object_or_404(Repository, name=repository_name)
    if 'renameform' in request.POST:
        action = 'confirmrename'
    elif 'deleteform' in request.POST:
        action = 'confirmdelete'
    elif 'rename' in request.POST:
        action = 'rename'
        old_elems = []
    else:
        action = 'delete'
    elems = []
    for k, v in request.POST.items():
        if (k.startswith('dir_') or k.startswith('file_')
                or k.startswith('elem_') or k.startswith('doc_')):
            if k.startswith('doc_'):
                try:
                    title = Document.objects.get(slug=v).title
                except:
                    title = v
                elems.append({'id': v, 'title': title})
            elif k.startswith('file_'):
                post_id = k[len('file_'):]
                new_file = {}
                new_file['id'] = v
                new_file['name'] = v
                new_file['title'] = request.POST.get('title_%s' % post_id,
                                                     '').encode('utf8')
                new_file['description'] = request.POST.get(
                    'description_%s' % post_id, '').encode('utf8')
                elems.append(new_file)
            else:
                elems.append({'id': v, 'title': v})
            if action == 'rename':
                if k.startswith('dir_'):
                    old_elems.append(request.POST['olddir_%s' %
                                                  k[len('dir_'):]])
                elif k.startswith('file_'):
                    old_elems.append(request.POST['oldfile_%s' %
                                                  k[len('file_'):]])
                else:
                    old_elems.append(request.POST['olddoc_%s' %
                                                  k[len('doc_'):]])
    if not elems:
        send_error(request, _("You didn't selected any element"))
        return HttpResponseRedirect(
            filebrowser_reverse(request,
                                "filebrowser_dir_listing",
                                kwargs={
                                    'repository_name': repository.name,
                                    'path': path
                                },
                                url_prefix=url_prefix))
    if action in ['confirmrename', 'confirmdelete']:
        if action == 'confirmrename':
            template = 'filebrowser/rename.html'
        else:
            template = 'filebrowser/delete.html'
        basepath = repository.get_absolute_path(path)
        basepath = basepath.encode('utf-8').decode('utf-8')
        files = []
        dirs = []
        docs = []
        for e in elems:
            file = join(basepath, e['id'])
            if isdir(file):
                dirs.append(e)
            elif isfile(file):
                mfile = file + '.metadata'
                if exists(mfile):
                    mf = open(mfile, 'r')
                    mdata = mf.read()
                    mf.close()
                    mdata = mdata.split('\n\n')
                    e['title'] = mdata[0]
                    e['description'] = '\n\n'.join(mdata[1:])
                files.append(e)
            else:
                docs.append(e)
        pathdirs, pathfiles, parents, dirname = repository.list_directory(path)
        return render_to_response(template, {
            'repository': repository,
            'base_template': base_template,
            'path': path,
            'files': files,
            'parents': parents,
            'dirs': dirs,
            'docs': docs,
            'url_prefix': url_prefix,
        },
                                  context_instance=RequestContext(request))
    elif action == 'rename':
        repository.rename_elems(path, elems, old_elems)
        send_info(request, _('Elements renamed successfully'))
        return HttpResponseRedirect(
            filebrowser_reverse(request,
                                "filebrowser_dir_listing",
                                kwargs={
                                    'repository_name': repository.name,
                                    'path': path
                                },
                                url_prefix=url_prefix))
    elif action == 'delete':
        repository.delete_elems(path, elems)
        send_info(request, _('Elements deleted successfully'))
        return HttpResponseRedirect(
            filebrowser_reverse(request,
                                "filebrowser_dir_listing",
                                kwargs={
                                    'repository_name': repository.name,
                                    'path': path
                                },
                                url_prefix=url_prefix))
    else:
        return Http404
Пример #5
0
    def save(self, request, content, contact_form):
        data = self.cleaned_data
        if contact_form.subject_fixed:
            subject = contact_form.subject
        else:
            subject = data['subject']

        if contact_form.sender_email:
            from_mail = data['sender_email']
        else:
            if request.user.is_authenticated():
                from_mail = request.user.email
            else:
                from_mail = settings.DEFAULT_FROM_EMAIL

        opts_to_save = {}
        opts_to_send = SortedDict()
        files = {}
        for opt in contact_form.opts.all():
            name = opt.name or 'option_%s' % opt.id
            val = data.get(name, _('Unset'))
            if val is None:
                val = ''
            label_field = transmeta.get_real_fieldname('label',
                                                       settings.LANGUAGE_CODE)
            label = defaultfilters.slugify(getattr(opt, label_field))
            if opt.field_type == 'file':
                files[label] = val
            else:
                opts_to_send[opt.label] = val
                opts_to_save[label] = val
        contactuser = {}
        opts_to_save[u'subject'] = subject
        if request.user.is_authenticated():
            opts_to_save[u'user'] = request.user.username
            contactuser[u'name'] = request.user.get_full_name()
            contactuser[u'username'] = request.user.username
        else:
            opts_to_save[u'user'] = '******'
            contactuser[u'name'] = _('Anonymous')
            contactuser[u'username'] = '******'

        opts_to_save[u'mailfrom'] = from_mail
        sent = SentContactForm(contact_form=contact_form,
                               sent_msg=simplejson.dumps(
                                   opts_to_save, cls=DateTimeAwareJSONEncoder))
        if request.user.is_authenticated():
            sent.sender = request.user
        sent.save()

        html_content = render_to_string(
            'contactform/email.html', {
                'opts': opts_to_send,
                'site_domain': Site.objects.get_current().domain,
                'content': content,
                'contact_form': contact_form,
                'contactuser': contactuser
            },
            context_instance=RequestContext(request))

        def mailstr_to_list(mailstr):
            return map(unicode.strip, mailstr.split(','))

        to_mail = mailstr_to_list(contact_form.email)
        bcc_mail = mailstr_to_list(contact_form.bcc)

        attachs = [(f.name, f.read(), f.content_type) for f in files.values()
                   if f]
        plain_content = defaultfilters.striptags(html_content)
        email = EmailMultiAlternatives(subject,
                                       plain_content,
                                       from_mail,
                                       to_mail,
                                       bcc=bcc_mail,
                                       attachments=attachs)
        email.attach_alternative(html_content, "text/html")
        try:
            email.send()
        except SMTPException, e:
            send_error(request, _('Mail Server Error: %s') % e)
            return None
Пример #6
0
def action(request, repository_name, path,
           base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None):
    """ process rename, delete, etc. actions in files and directories """
    repository = get_object_or_404(Repository, name=repository_name)
    if 'renameform' in request.POST:
        action = 'confirmrename'
    elif 'deleteform' in request.POST:
        action = 'confirmdelete'
    elif 'rename' in request.POST:
        action = 'rename'
        old_elems = []
    else:
        action = 'delete'
    elems = []
    for k, v in request.POST.items():
        if (k.startswith('dir_') or k.startswith('file_') or
            k.startswith('elem_') or k.startswith('doc_')):
            if k.startswith('doc_'):
                try:
                    title = Document.objects.get(slug=v).title
                except:
                    title = v
                elems.append({'id': v, 'title': title})
            elif k.startswith('file_'):
                post_id = k[len('file_'):]
                new_file = {}
                new_file['id'] = v
                new_file['name'] = v
                new_file['title'] = request.POST.get('title_%s' % post_id, '').encode('utf8')
                new_file['description'] = request.POST.get('description_%s' % post_id, '').encode('utf8')
                elems.append(new_file)
            else:
                elems.append({'id': v, 'title': v})
            if action == 'rename':
                if k.startswith('dir_'):
                    old_elems.append(request.POST['olddir_%s' % k[len('dir_'):]])
                elif k.startswith('file_'):
                    old_elems.append(request.POST['oldfile_%s' % k[len('file_'):]])
                else:
                    old_elems.append(request.POST['olddoc_%s' % k[len('doc_'):]])
    if not elems:
        send_error(request, _("You didn't selected any element"))
        return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing",
                        kwargs={'repository_name': repository.name,
                                'path': path},
                        url_prefix=url_prefix))
    if action in ['confirmrename', 'confirmdelete']:
        if action == 'confirmrename':
            template = 'filebrowser/rename.html'
        else:
            template = 'filebrowser/delete.html'
        basepath = repository.get_absolute_path(path)
        basepath = basepath.encode('utf-8').decode('utf-8')
        files = []
        dirs = []
        docs = []
        for e in elems:
            file = join(basepath, e['id'])
            if isdir(file):
                dirs.append(e)
            elif isfile(file):
                mfile = file + '.metadata'
                if exists(mfile):
                    mf = open(mfile, 'r')
                    mdata = mf.read()
                    mf.close()
                    mdata = mdata.split('\n\n')
                    e['title'] = mdata[0]
                    e['description'] = '\n\n'.join(mdata[1:])
                files.append(e)
            else:
                docs.append(e)
        pathdirs, pathfiles, parents, dirname = repository.list_directory(path)
        return render_to_response(template,
                                  {'repository': repository,
                                   'base_template': base_template,
                                   'path': path,
                                   'files': files,
                                   'parents': parents,
                                   'dirs': dirs,
                                   'docs': docs,
                                   'url_prefix': url_prefix,
                                  },
                                  context_instance=RequestContext(request))
    elif action == 'rename':
        repository.rename_elems(path, elems, old_elems)
        send_info(request, _('Elements renamed successfully'))
        return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing",
                        kwargs={'repository_name': repository.name,
                                'path': path},
                        url_prefix=url_prefix))
    elif action == 'delete':
        repository.delete_elems(path, elems)
        send_info(request, _('Elements deleted successfully'))
        return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing",
                        kwargs={'repository_name': repository.name,
                                'path': path},
                        url_prefix=url_prefix))
    else:
        return Http404