Ejemplo n.º 1
0
def template_html(request, template_id):
    template = get_object_or_404(Template, template_id=template_id)

    if not template.html_file:
        raise Http404

    simplified = True

    login_content = ""
    include_login = request.GET.get('include_login', False)
    if include_login:
        login_content = render_to_string(
            'newsletters/login.txt', context_instance=RequestContext(request))

    jumplink_content = ""
    jump_links = request.GET.get('jump_links', 1)
    if jump_links:
        jumplink_content = render_to_string(
            'newsletters/jumplinks.txt',
            locals(),
            context_instance=RequestContext(request))

    art_content = ""
    articles = request.GET.get('articles', 1)
    articles_days = request.GET.get('article_days', 60)
    if articles:
        articles_list, articles_content = newsletter_articles_list(
            request, articles_days, simplified)

    news_content = ""
    news = request.GET.get('news', 1)
    news_days = request.GET.get('news_days', 30)
    if news:
        news_list, news_content = newsletter_news_list(request, news_days,
                                                       simplified)

    jobs_content = ""
    jobs = request.GET.get('jobs', 1)
    jobs_days = request.GET.get('jobs_days', 30)
    if jobs:
        jobs_list, jobs_content = newsletter_jobs_list(request, jobs_days,
                                                       simplified)

    pages_content = ""
    pages = request.GET.get('pages', 0)
    pages_days = request.GET.get('pages_days', 7)
    if pages:
        pages_list, pages_content = newsletter_pages_list(
            request, pages_days, simplified)

    events = request.GET.get('events', 1)
    events_type = request.GET.get('events_type')
    start_y, start_m, start_d = request.GET.get(
        'event_start_dt', str(datetime.date.today())).split('-')
    event_start_dt = datetime.date(int(start_y), int(start_m), int(start_d))
    end_y, end_m, end_d = request.GET.get(
        'event_end_dt',
        str(datetime.date.today() + datetime.timedelta(days=90))).split('-')
    event_end_dt = datetime.date(int(end_y), int(end_m), int(end_d))
    if events:
        events_list = Event.objects.filter(
            start_dt__lt=event_end_dt,
            end_dt__gt=event_start_dt,
            status_detail='active',
            status=True,
            allow_anonymous_view=True)
        if events_type:
            events_list = events_list.filter(type__pk=events_type)
            events_type = Type.objects.filter(pk=events_type)[0]
        events_list = events_list.order_by('start_dt')

    text = DTemplate(apply_template_media(template))
    context = RequestContext(
        request,
        {
            'jumplink_content': jumplink_content,
            'login_content': login_content,
            "art_content": articles_content,  # legacy usage in templates
            "articles_content": articles_content,
            "articles_list": articles_list,
            "jobs_content": jobs_content,
            "jobs_list": jobs_list,
            "news_content": news_content,
            "news_list": news_list,
            "pages_content": pages_content,
            "pages_list": pages_content,
            "events": events_list,  # legacy usage in templates
            "events_list": events_list,
            "events_type": events_type
        })

    response = HttpResponse(text.render(context))
    response['Content-Disposition'] = 'attachment; file=page.html'

    return response
Ejemplo n.º 2
0
    def save(self, request):
        from django.template.loader import render_to_string
        from django.template import RequestContext
        from emails.models import Email
        from newsletters.utils import newsletter_articles_list, newsletter_news_list, \
                            newsletter_pages_list, newsletter_jobs_list
        from site_settings.utils import get_setting
        
        # converted from function newsletters_generate_processor
        opening_text = render_to_string('newsletters/opening_text.txt', 
                                        context_instance=RequestContext(request))
        simplified = self.cleaned_data['format']
        try:
            simplified = int(simplified)
        except:
            simplified = 0
        
        # articles
        art_content = ""
        if self.cleaned_data['articles']:
            articles_days = self.cleaned_data['articles_days']
            art_content = newsletter_articles_list(request, articles_days, simplified)
            
        # calendar events    
        event_content = ""
        if self.cleaned_data['events']:
            pass
        
        # news
        news_content = ""
        if self.cleaned_data['news']:
            news_days = self.cleaned_data['news_days']
            news_content = newsletter_news_list(request, news_days, simplified)
            
        
        # jobs
        job_content = ""
        if self.cleaned_data['jobs']:
            jobs_days = self.cleaned_data['jobs_days']
            job_content = newsletter_jobs_list(request, jobs_days, simplified)
                
        # pages
        page_content = ""
        if self.cleaned_data['pages']:
            pages_days = self.cleaned_data['pages_days']
            page_content = newsletter_pages_list(request, pages_days, simplified)
        
        # jumplink
        jumplink_content = ""
        if self.cleaned_data['jump_links']:
            jumplink_content = render_to_string('newsletters/jumplinks.txt', locals(), 
                                        context_instance=RequestContext(request))
            
        # login block
        login_content = ""
        if self.cleaned_data['include_login']:
            login_content = render_to_string('newsletters/login.txt',  
                                        context_instance=RequestContext(request))
            
        # rss list
        
        
        email_d = {}
        # store all content in email_d["[content]"]
        # will be used to replace [content] in the template
        email_d["[content]"] = opening_text
        
        # get the newsletter template now
        template = 'newsletters/templates/%s' % (self.cleaned_data['template'])
        email_d['template_path_name'] = template
        
        #check if we have [jumplink] in the email template, if not, 
        #include the jumplinks at the top of the newsletter
        template_content = render_to_string(template)
        if jumplink_content:
            if template_content.find("[jumplinks]") == -1:
                email_d["[content]"] += jumplink_content
                
        email_d["[content]"] += "%s%s%s%s%s%s" % (login_content, event_content, art_content,
                                news_content, job_content, page_content)
        
        
        email_d["[jumplinks]"] = jumplink_content
        email_d["[articles]"] = art_content
        email_d["[calendarevents]"] = event_content
        email_d["[events]"] = event_content
        email_d["[jobs]"] = job_content
        email_d["[contentmanagers]"] = page_content
        email_d["[pages]"] = page_content
        email_d["[releases]"] = news_content
        email_d["[news]"] = news_content
        
        email_d["[sitewebmaster]"] = get_setting('site', "global", "sitewebmaster")
        email_d["[sitedisplayname]"] = get_setting('site', "global", "sitedisplayname")
        
        today = datetime.date.today()
        email_d["[monthsubmitted]"] = today.strftime("%B") # June
        email_d["[yearsubmitted]"] = today.strftime("%Y")  # 2010
        email_d["[unsubscribeurl]"] = "[unsubscribeurl]"    
        
        email_d["[currentweekdayname]"] = today.strftime("%A")    # Wednesday
        email_d["[currentday]"] = today.strftime("%d")                       
        email_d["[currentmonthname]"] = today.strftime("%B")
        
        
        
        email = Email()
        is_valid = email.template_body(email_d)
        email.sender_display = "%s %s" % (request.user.first_name, request.user.last_name)
        email.sender = request.user.email
        email.reply_to = request.user.email
        email.recipient = request.user.email
        #email.send_to_email2 
        email.content_type = 'text/html'
        
        personalize_subject_first_name = self.cleaned_data['personalize_subject_first_name']
        personalize_subject_last_name = self.cleaned_data['personalize_subject_last_name']
        
        email.subject = self.cleaned_data['subject']
        if personalize_subject_first_name and personalize_subject_last_name:
            email.subject = "[firstname] [lastname], " + email.subject
        elif personalize_subject_first_name:
            email.subject = "[firstname], " + email.subject
        elif personalize_subject_last_name:
            email.subject = "[lastname], " + email.subject
        email.status = 1
        email.status_detail = 'active'
        email.category = 'marketing'
        
        email.save(request.user)
        
        # action object - these 3 already included on the form: member_only, group and send_to_emails
        now = datetime.datetime.now()
        self.instance.email = email
        self.instance.name = email.subject
        self.instance.type = 'Distribution E-mail'
        self.instance.name = email.subject
        self.instance.description = '%s Electronic Newsletter: generated %s' % \
                            (get_setting('site', "global", "sitedisplayname"), 
                             now.strftime('%d-%b-%y %I:%M:%S %p'))
        self.instance.category = 'marketing'
        self.instance.due_dt = now
        try:
            entity = (request.user.get_profile()).entity
        except:
            entity = None
        if entity:
            self.instance.entity = entity
        self.instance.status = 1
        self.instance.status_detail = 'open'
        self.instance.save(request.user)
        
        return self.instance