Exemple #1
0
def process(template, page):
    """处理页面链接,并且将回车符转为<br>"""
    t = loader.get_template(template)
    content = r.sub(r'<a href="/wiki/\1">\1</a>', page.content)
    content = re.sub(r'[\n\r]+', '<br>', content)
    c = Context({'pagename': page.pagename, 'content': content})
    return HttpResponse(t.render(c))
Exemple #2
0
def process(template, page):
    """处理页面链接,并且将回车符转为<br>"""
    t = loader.get_template(template)
    content = r.sub(r'<a href="/wiki/\1">\1</a>', page.content)
    content = re.sub(r'[\n\r]+', '<br>', content)
    c = Context({'pagename':page.pagename, 'content':content})
    return HttpResponse(t.render(c))
def run_tests(verbosity=0, standalone=False):
    # Register our custom template loader.
    old_template_loaders = loader.template_source_loaders
    loader.template_source_loaders = [test_template_loader]

    failed_tests = []
    tests = TEMPLATE_TESTS.items()
    tests.sort()
    for name, vals in tests:
        if 'LANGUAGE_CODE' in vals[1]:
            activate(vals[1]['LANGUAGE_CODE'])
        try:
            output = loader.get_template(name).render(template.Context(vals[1]))
        except Exception, e:
            if e.__class__ == vals[2]:
                if verbosity:
                    print "Template test: %s -- Passed" % name
            else:
                if verbosity:
                    print "Template test: %s -- FAILED. Got %s, exception: %s" % (name, e.__class__, e)
                failed_tests.append(name)
            continue
        if 'LANGUAGE_CODE' in vals[1]:
            deactivate()
        if output == vals[2]:
            if verbosity:
                print "Template test: %s -- Passed" % name
        else:
            if verbosity:
                print "Template test: %s -- FAILED. Expected %r, got %r" % (name, vals[2], output)
            failed_tests.append(name)
def server_error(request):
    """
    500 error handler.

    Templates: `500`
    Context: None
    """
    t = loader.get_template('500')
    return httpwrappers.HttpResponseServerError(t.render(Context()))
def output(request, filename):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename

    t = loader.get_template('csv')
    c = Context({
        'data': address,
    })
    response.write(t.render(c))
    return response
def page_not_found(request):
    """
    Default 404 handler, which looks for the requested URL in the redirects
    table, redirects if found, and displays 404 page if not redirected.

    Templates: `404`
    Context: None
    """
    t = loader.get_template('404')
    return httpwrappers.HttpResponseNotFound(t.render(Context()))
Exemple #7
0
def output(request):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s' % 'address.csv'
    t = loader.get_template('csv')
    objs = addresses.get_list()
    d = []
    for o in objs:
        d.append((o.name, o.gender, o.telphone, o.mobile, o.room))
    c = Context({
        'data': d,
    })
    response.write(t.render(c))
    return response
Exemple #8
0
def output(request):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s' % 'address.csv'
    t = loader.get_template('csv')
    objs = addresses.get_list()
    d = []
    for o in objs:
        d.append((o.name, o.gender, o.telphone, o.mobile, o.room))
    c = Context({
        'data': d,
    })
    response.write(t.render(c))
    return response
Exemple #9
0
def login(request):
    try:
        user = users.get_object(username__exact=request.POST["username"])
        if not user.check_password(request.POST["password"]):
            raise users.UserDoesNotExist
        # set the session's user active
        request.session[users.SESSION_KEY] = user.id
        try:
            nextpage = request.GET["next"]
        except KeyError:
            nextpage = "/gappy/"
        return HttpResponseRedirect(nextpage)
    except KeyError:
        return render_to_response("gappy/login")
    except users.UserDoesNotExist:
        t = loader.get_template("gappy/login")
        c = Context({"error":"Some login information was wrong."})
        return HttpResponse(t.render(c))
 def save(self, domain_override=None):
     "Calculates a new password randomly and sends it to the user"
     from django.core.mail import send_mail
     from django.models.core import sites
     new_pass = users.make_random_password()
     self.user_cache.set_password(new_pass)
     self.user_cache.save()
     if not domain_override:
         current_site = sites.get_current()
         site_name = current_site.name
         domain = current_site.domain
     else:
         site_name = domain = domain_override
     t = loader.get_template('registration/password_reset_email')
     c = {
         'new_password': new_pass,
         'email': self.user_cache.email,
         'domain': domain,
         'site_name': site_name,
         'user': self.user_cache,
     }
     send_mail('Password reset on %s' % site_name, t.render(c), None, [self.user_cache.email])
Exemple #11
0
    def get_feed(self, url=None):
        """
        Returns a feedgenerator.DefaultFeed object, fully populated, for
        this feed. Raises FeedDoesNotExist for invalid parameters.
        """
        if url:
            try:
                obj = self.get_object(url.split('/'))
            except (AttributeError, ObjectDoesNotExist):
                raise FeedDoesNotExist
        else:
            obj = None

        current_site = sites.get_current()
        link = self.__get_dynamic_attr('link', obj)
        link = add_domain(current_site.domain, link)

        feed = self.feed_type(
            title = self.__get_dynamic_attr('title', obj),
            link = link,
            description = self.__get_dynamic_attr('description', obj),
            language = LANGUAGE_CODE.decode(),
            feed_url = add_domain(current_site, self.feed_url),
            author_name = self.__get_dynamic_attr('author_name', obj),
            author_link = self.__get_dynamic_attr('author_link', obj),
            author_email = self.__get_dynamic_attr('author_email', obj),
        )

        try:
            title_template = loader.get_template('feeds/%s_title' % self.slug)
        except TemplateDoesNotExist:
            title_template = Template('{{ obj }}')
        try:
            description_template = loader.get_template('feeds/%s_description' % self.slug)
        except TemplateDoesNotExist:
            description_template = Template('{{ obj }}')

        for item in self.__get_dynamic_attr('items', obj):
            link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
            enc = None
            enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
            if enc_url:
                enc = feedgenerator.Enclosure(
                    url = enc_url.decode('utf-8'),
                    length = str(self.__get_dynamic_attr('item_enclosure_length', item)).decode('utf-8'),
                    mime_type = self.__get_dynamic_attr('item_enclosure_mime_type', item).decode('utf-8'),
                )
            author_name = self.__get_dynamic_attr('item_author_name', item)
            if author_name is not None:
                author_email = self.__get_dynamic_attr('item_author_email', item)
                author_link = self.__get_dynamic_attr('item_author_link', item)
            else:
                author_email = author_link = None
            feed.add_item(
                title = title_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'),
                link = link,
                description = description_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'),
                unique_id = link,
                enclosure = enc,
                pubdate = self.__get_dynamic_attr('item_pubdate', item),
                author_name = author_name,
                author_email = author_email,
                author_link = author_link,
            )
        return feed
Exemple #12
0
def index(request):
    t = loader.get_template("gappy/index")
    c = Context({})
    return HttpResponse(t.render(c))