示例#1
0
def shortcut(request, content_type_id, object_id):
    "Redirect to an object's page based on a content-type ID and an object ID."
    # Look up the object, making sure it's got a get_absolute_url() function.
    try:
        content_type = contenttypes.get_object(pk=content_type_id)
        obj = content_type.get_object_for_this_type(pk=object_id)
    except ObjectDoesNotExist:
        raise Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
    try:
        absurl = obj.get_absolute_url()
    except AttributeError:
        raise Http404, "%s objects don't have get_absolute_url() methods" % content_type.name

    # Try to figure out the object's domain, so we can do a cross-site redirect
    # if necessary.

    # If the object actually defines a domain, we're done.
    if absurl.startswith('http://'):
        return httpwrappers.HttpResponseRedirect(absurl)

    object_domain = None

    # Next, look for an many-to-many relationship to sites
    if hasattr(obj, 'get_site_list'):
        site_list = obj.get_site_list()
        if site_list:
            object_domain = site_list[0].domain

    # Next, look for a many-to-one relationship to sites
    elif hasattr(obj, 'get_site'):
        try:
            object_domain = obj.get_site().domain
        except sites.SiteDoesNotExist:
            pass

    # Then, fall back to the current site (if possible)
    else:
        try:
            object_domain = sites.get_current().domain
        except sites.SiteDoesNotExist:
            # Finally, give up and use a URL without the domain name
            return httpwrappers.HttpResponseRedirect(obj.get_absolute_url())
    return httpwrappers.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url()))
示例#2
0
def login(request):
    "Displays the login form and handles the login action."
    manipulator = AuthenticationForm(request)
    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
    if request.POST:
        errors = manipulator.get_validation_errors(request.POST)
        if not errors:
            # Light security check -- make sure redirect_to isn't garbage.
            if not redirect_to or '://' in redirect_to or ' ' in redirect_to:
                redirect_to = '/accounts/profile/'
            request.session[users.SESSION_KEY] = manipulator.get_user_id()
            request.session.delete_test_cookie()
            return HttpResponseRedirect(redirect_to)
    else:
        errors = {}
    request.session.set_test_cookie()
    return render_to_response('registration/login', {
        'form': formfields.FormWrapper(manipulator, request.POST, errors),
        REDIRECT_FIELD_NAME: redirect_to,
        'site_name': sites.get_current().name,
    }, context_instance=DjangoContext(request))
示例#3
0
 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])
示例#4
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