Changeset - c5289f39bb3c
[Not reviewed]
0 9 0
Ben Sturmfels (bsturmfels) - 2 months ago 2024-07-22 08:39:00
ben@sturm.com.au
Fix flake8 warnings
9 files changed with 37 insertions and 31 deletions:
0 comments (0 inline, 0 general)
conservancy/blog/models.py
Show inline comments
 
from datetime import datetime, timedelta
 

	
 
from django.conf import settings
 
from django.db import models
 

	
 
from .. import bsoup
 
from ..staff.models import Person
 

	
 

	
 
class EntryTag(models.Model):
 
    """Tagging for blog entries"""
 

	
 
    label = models.CharField(max_length=100)
 
    slug = models.SlugField()
 

	
 
    class Meta:
 
        db_table = 'techblog_entrytag' # legacy
 

	
 
    def __str__(self):
 
        return self.label
 

	
 
    def get_absolute_url(self):
 
        return "/blog/?tag=%s" % self.slug
 

	
 
class Entry(models.Model, bsoup.SoupModelMixin):
 
    """Blog entry"""
 

	
 
    headline = models.CharField(max_length=200)
 
    slug = models.SlugField(unique_for_date='pub_date')
 
    summary = models.TextField(help_text="Use raw HTML.  Unlike in the press release model, this summary is not included at the beginning of the body when the entry is displayed.")
 
    body = models.TextField(help_text="Use raw HTML.  Include the full body of the post.")
 
    pub_date = models.DateTimeField()
 
    author = models.ForeignKey(Person, on_delete=models.PROTECT)
 
    tags = models.ManyToManyField(EntryTag, blank=True)
 

	
 
    date_created = models.DateTimeField(auto_now_add=True)
 
    date_last_modified = models.DateTimeField(auto_now=True)
 

	
 
    class Meta:
 
        db_table = 'techblog_entries' # legacy
 
        verbose_name_plural = 'entries'
 
        ordering = ('-pub_date',)
 
        get_latest_by = 'pub_date'
 

	
 
    SOUP_ATTRS = ['body']
 

	
 
    def __str__(self):
 
        return self.headline
 

	
 
    def get_absolute_url(self):
 
        return ("/blog/%s/%s/"
 
                % (self.pub_date.strftime("%Y/%b/%d").lower(),
 
                   self.slug))
 

	
 
    def is_recent(self):
 
        return self.pub_date > (datetime.now() - timedelta(days=30))
 
        # question: does datetime.now() do a syscall each time is it called?
 

	
 
    # Ping google blogs and technorati.  Taken from
 
    # http://blog.foozia.com/blog/2007/apr/21/ping-technorati-your-django-blog-using-xml-rpc/
 
    def save(self):
 
        if settings.DEBUG or True: # "or True" means it is disabled always
 
            super().save()
 
            return
 

	
 
        blog_name = 'Software Freedom Conservancy Blog'
 
        blog_url =  'http://www.sfconservancy.org/blog/'
 
        post_url = ('http://www.sfconservancy.org'
 
                    + self.get_absolute_url())
 

	
 
        import xmlrpc.client
 

	
 
        # Ping Technorati
 
        j = xmlrpc.client.Server('http://rpc.technorati.com/rpc/ping')
 
        reply = j.weblogUpdates.ping(blog_name, blog_url)
 
        j.weblogUpdates.ping(blog_name, blog_url)
 

	
 
        # Ping Google Blog Search
 
        j = xmlrpc.client.Server('http://blogsearch.google.com/ping/RPC2')
 
        reply = j.weblogUpdates.ping(blog_name, blog_url, post_url)
 
        j.weblogUpdates.ping(blog_name, blog_url, post_url)
 

	
 
        # Call any superclass's method
 
        super().save()
conservancy/blog/views.py
Show inline comments
 
from datetime import datetime
 
from functools import reduce
 

	
 
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
 
from django.shortcuts import get_object_or_404, render
 
from django.views.generic import ListView
 
from django.views.generic.dates import (
 
    DateDetailView,
 
    DayArchiveView,
 
    MonthArchiveView,
 
    YearArchiveView,
 
)
 

	
 
from ..staff.models import Person
 
from .models import Entry, EntryTag
 
from .models import EntryTag
 

	
 

	
 
def OR_filter(field_name, objs):
 
    from django.db.models import Q
 
    return reduce(lambda x, y: x | y,
 
                  [Q(**{field_name: x.id}) for x in objs])
 

	
 
def last_name(person):
 
    return person.formal_name.rpartition(' ')[2]
 

	
 
def custom_index(request, queryset, *args, **kwargs):
 
    """Blog list view that allows scrolling and also shows an index by
 
    year.
 
    """
 

	
 
    extra_context = kwargs.get('extra_context', {}).copy()
 
    date_field = kwargs['date_field']
 

	
 
    if not kwargs.get('allow_future', False):
 
        queryset = queryset.filter(**{'%s__lte' % date_field: datetime.now()})
 

	
 
    authors = []
 
    if 'author' in request.GET:
 
        authors = [get_object_or_404(Person, username=author)
 
                   for author in request.GET.getlist('author')]
 
        extra_context['authors'] = authors
 
        queryset = queryset.filter(OR_filter('author', authors))
 

	
 
    tags = []
 
    if 'tag' in request.GET:
 
        tags = [get_object_or_404(EntryTag, slug=tag)
 
                for tag in request.GET.getlist('tag')]
 
        extra_context['tags'] = tags
 
        queryset = queryset.filter(OR_filter('tags', tags))
 

	
 
    if authors or tags:
 
        query_string = '&'.join(['author=%s' % a.username for a in authors]
 
                                + ['tag=%s' % t.slug for t in tags])
 
        extra_context['query_string'] = query_string
 

	
 
    else:
 
        date_list = queryset.dates(date_field, 'year')
 
        extra_context['date_list'] = date_list
 

	
 
    paginate_by = kwargs.get('paginate_by', 6)  # Show 6 news items per page, by default
 
    paginator = Paginator(queryset, paginate_by)
 
    page = request.GET.get('page')
 

	
 
    try:
 
        blog_entries = paginator.page(page)
 
    except PageNotAnInteger:
 
        # If page is not an integer, deliver first page.
 
        blog_entries = paginator.page(1)
 
    except EmptyPage:
 
        # If page is out of range (e.g. 9999), deliver last page of results.
 
        blog_entries = paginator.page(paginator.num_pages)
 

	
 
    extra_context['blog_entries'] = blog_entries
 

	
 
    return render(request, 'blog/entry_list.html', extra_context)
 

	
 
def techblog_redirect(request):
 
    """Redirect from the old 'techblog' to the new blog
 
    """
 

	
 
    path = request.path[len('/technology'):]
 
    if path == '/blog/':
 
        path += "?author=bkuhn"
 

	
 
    return relative_redirect(request, path)
 

	
 
def query(request):
 
    """Page to query the blog based on authors and tags
 
    """
 

	
 
    if request.GET:
 
        d = request.GET.copy()
 
        if 'authors' in d.getlist('all'):
 
            d.setlist('author', []) # remove author queries
 
        if 'tags' in d.getlist('all'):
 
            d.setlist('tag', []) # remove tag queries
 
        d.setlist('all', []) # remove "all" from the query string
 

	
 
        base_url = '/blog/'
 
        if 'rss' in d:
 
            base_url = '/feeds/blog/'
 
            d.setlist('rss', []) # remove it
 

	
 
        query_string = d.urlencode()
 

	
 
        return relative_redirect(request, '{}{}{}'.format(base_url, '?' if query_string else '', query_string))
 

	
 
    else:
 
        authors = sorted(Person.objects.filter(currently_employed=True,
 
                                               entry__isnull=False).distinct(),
 
                         key=last_name)
 
        tags = EntryTag.objects.all().order_by('label')
 
        return render(request, 'blog/query.html', {'authors': authors, 'tags': tags})
 

	
 
def relative_redirect(request, path):
 
    from django import http
 

	
 
    host = request.get_host()
 

	
 
    url = "{}://{}{}".format(request.is_secure() and 'https' or 'http', host, path)
 
    return http.HttpResponseRedirect(url)
 

	
 
class BlogYearArchiveView(YearArchiveView):
 
    make_object_list = True
 
    allow_future = True
 
    extra_context = {}
 
    
 
    def get_context_data(self, **kwargs):
 
        context = super().get_context_data(**kwargs)
 
        context.update(self.extra_context)
 
        return context
 

	
 
class BlogMonthArchiveView(MonthArchiveView):
 
    allow_future = True
 
    extra_context = {}
 
    
 
    def get_context_data(self, **kwargs):
 
        context = super().get_context_data(**kwargs)
 
        context.update(self.extra_context)
 
        return context
 

	
 
class BlogDayArchiveView(DayArchiveView):
 
    allow_future = True
 
    extra_context = {}
 
    
 
    def get_context_data(self, **kwargs):
 
        context = super().get_context_data(**kwargs)
 
        context.update(self.extra_context)
 
        return context
 

	
 
class BlogDateDetailView(DateDetailView):
 
    allow_future = True
 
    extra_context = {}
 
    
 
    def get_context_data(self, **kwargs):
 
        context = super().get_context_data(**kwargs)
 
        context.update(self.extra_context)
 
        return context
conservancy/content/projects/policies/publish-policy.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
import argparse
 
import contextlib
 
import functools
 
import locale
 
import os
 
import pathlib
 
import shutil
 
import subprocess
 
import sys
 
import tempfile
 

	
 
try:
 
    import markdown
 
    from markdown.extensions import sane_lists as mdx_sane_lists
 
    from markdown.extensions import smarty as mdx_smarty
 
    from markdown.extensions import tables as mdx_tables
 
    from markdown.extensions import toc as mdx_toc
 
    markdown_import_success = True
 
except ImportError:
 
    if __name__ != '__main__':
 
        raise
 
    markdown_import_success = False
 

	
 
TEMPLATE_HEADER = """{% extends "base_projects.html" %}
 
{% block subtitle %}{% endblock %}
 
{% block submenuselection %}Policies{% endblock %}
 
{% block content %}
 

	
 
"""
 

	
 
TEMPLATE_FOOTER = """
 

	
 
{% endblock %}
 
"""
 

	
 
@contextlib.contextmanager
 
def run(cmd, encoding=None, ok_exitcodes=frozenset([0]), **kwargs):
 
    kwargs.setdefault('stdout', subprocess.PIPE)
 
    if encoding is None:
 
        mode = 'rb'
 
        no_data = b''
 
    else:
 
        mode = 'r'
 
        no_data = ''
 
    with contextlib.ExitStack() as exit_stack:
 
        proc = exit_stack.enter_context(subprocess.Popen(cmd, **kwargs))
 
        pipes = [exit_stack.enter_context(open(
 
                   getattr(proc, name).fileno(), mode, encoding=encoding, closefd=False))
 
                 for name in ['stdout', 'stderr']
 
                 if kwargs.get(name) is subprocess.PIPE]
 
        pipes = [
 
            exit_stack.enter_context(open(
 
                getattr(proc, name).fileno(), mode, encoding=encoding, closefd=False))
 
            for name in ['stdout', 'stderr']
 
            if kwargs.get(name) is subprocess.PIPE
 
        ]
 
        if pipes:
 
            yield (proc, *pipes)
 
        else:
 
            yield proc
 
        for pipe in pipes:
 
            for _ in iter(lambda: pipe.read(4096), no_data):
 
                pass
 
    if proc.returncode not in ok_exitcodes:
 
        raise subprocess.CalledProcessError(proc.returncode, cmd)
 

	
 
class GitPath:
 
    GIT_BIN = shutil.which('git')
 
    CLEAN_ENV = {k: v for k, v in os.environ.items() if not k.startswith('GIT_')}
 
    ANY_EXITCODE = range(-256, 257)
 
    IGNORE_ERRORS = {
 
        'ok_exitcodes': ANY_EXITCODE,
 
        'stderr': subprocess.DEVNULL,
 
    }
 
    STATUS_CLEAN_OR_UNMANAGED = frozenset(' ?')
 

	
 
    def __init__(self, path, encoding, env=None):
 
        self.path = path
 
        self.dir_path = path if path.is_dir() else path.parent
 
        self.encoding = encoding
 
        self.run_defaults = {
 
            'cwd': str(self.dir_path),
 
            'env': env,
 
        }
 

	
 
    @classmethod
 
    def can_run(cls):
 
        return cls.GIT_BIN is not None
 

	
 
    def _run(self, cmd, encoding=None, ok_exitcodes=frozenset([0]), **kwargs):
 
        return run(cmd, encoding, ok_exitcodes, **self.run_defaults, **kwargs)
 

	
 
    def _cache(orig_func):
 
        attr_name = '_cached_' + orig_func.__name__
 

	
 
        @functools.wraps(orig_func)
 
        def cache_wrapper(self):
 
            try:
 
                return getattr(self, attr_name)
 
            except AttributeError:
 
                setattr(self, attr_name, orig_func(self))
 
                return getattr(self, attr_name)
 
        return cache_wrapper
 

	
 
    @_cache
 
    def is_work_tree(self):
 
        with self._run([self.GIT_BIN, 'rev-parse', '--is-inside-work-tree'],
 
                       self.encoding, **self.IGNORE_ERRORS) as (_, stdout):
 
            return stdout.readline() == 'true\n'
 

	
 
    @_cache
 
    def status_lines(self):
 
        with self._run([self.GIT_BIN, 'status', '-z'],
 
                       self.encoding) as (_, stdout):
 
            return stdout.read().split('\0')
 

	
 
    @_cache
 
    def has_managed_modifications(self):
 
        return any(line and line[1] not in self.STATUS_CLEAN_OR_UNMANAGED
 
                   for line in self.status_lines())
 

	
 
    @_cache
 
    def has_staged_changes(self):
 
        return any(line and line[0] not in self.STATUS_CLEAN_OR_UNMANAGED
 
                   for line in self.status_lines())
 

	
 
    def commit_at(self, revision):
 
        with self._run([self.GIT_BIN, 'rev-parse', revision],
 
                       self.encoding) as (_, stdout):
 
            return stdout.readline().rstrip('\n') or None
 

	
 
    @_cache
 
    def upstream_commit(self):
 
        return self.commit_at('@{upstream}')
 

	
 
    @_cache
 
    def head_commit(self):
 
        return self.commit_at('HEAD')
 

	
 
    def in_sync_with_upstream(self):
 
        return self.upstream_commit() == self.head_commit()
 

	
 
    @_cache
 
    def last_commit(self):
 
        with self._run([self.GIT_BIN, 'log', '-n1', '--format=format:%H', self.path.name],
 
                       self.encoding, **self.IGNORE_ERRORS) as (_, stdout):
 
            return stdout.readline().rstrip('\n') or None
 

	
 
    def operate(self, subcmd, ok_exitcodes=frozenset([0])):
 
        with self._run([self.GIT_BIN, *subcmd], None, ok_exitcodes, stdout=None):
 
            pass
 

	
 

	
 
def add_parser_flag(argparser, dest, **kwargs):
 
    kwargs.update(dest=dest, default=None)
 
    switch_root = dest.replace('_', '-')
 
    switch = '--' + switch_root
 
    argparser.add_argument(switch, **kwargs, action='store_true')
 
    kwargs['help'] = "Do not do {}".format(switch)
 
    argparser.add_argument('--no-' + switch_root, **kwargs, action='store_false')
 

	
 
def parse_arguments(arglist):
 
    parser = argparse.ArgumentParser(
 
        epilog="""By default, the program will pull from Git if the output path
 
is a Git checkout with a tracking branch, and will commit and push if
 
that checkout is in sync with the tracking branch without any staged changes.
 
Setting any flag will always override the default behavior.
 
""",
 
    )
 

	
 
    parser.add_argument(
 
        '--encoding', '-E',
 
        default=locale.getpreferredencoding(),
 
        help="Encoding to use for all I/O. "
 
        "Default is your locale's encoding.",
 
    )
 
    parser.add_argument(
 
        '--revision', '-r',
 
        help="Revision string to version the published page. "
 
        "Default determined from the revision of the source file.",
 
    )
 
    add_parser_flag(
 
        parser, 'pull',
 
        help="Try to pull the remote tracking branch to make the checkout "
 
        "up-to-date before making changes"
 
    )
 
    add_parser_flag(
 
        parser, 'commit',
 
        help="Commit changes to the website repository",
 
    )
 
    parser.add_argument(
 
        '-m', dest='commit_message',
 
        default="Publish {filename} revision {revision}.",
 
        help="Message for any commit",
 
    )
 
    add_parser_flag(
 
        parser, 'push',
 
        help="Push to the remote tracking branch after committing changes",
 
    )
 
    parser.add_argument(
 
        'input_path', type=pathlib.Path,
 
        help="Path to the Conservancy policy Markdown source",
 
    )
 
    parser.add_argument(
 
        'output_path', type=pathlib.Path,
 
        nargs='?', default=pathlib.Path(__file__).parent,
 
        help="Path to the directory to write output files",
 
    )
 

	
 
    if not markdown_import_success:
 
        parser.error("""markdown module is not installed.
 
Try `apt install python3-markdown` or `python3 -m pip install --user Markdown`.""")
 

	
 
    args = parser.parse_args(arglist)
 
    args.git_output = GitPath(args.output_path, args.encoding)
 
    if args.pull or args.commit or args.push:
 
        if not args.git_output.can_run():
 
            parser.error("Git operation requested but `git` not found in PATH")
 
        elif not args.git_output.is_work_tree():
 
            parser.error("Git operation requested but {} is not a working path".format(
 
                args.output_path.as_posix()))
 
    if args.revision is None:
 
        try:
 
            args.revision = GitPath(args.input_path, args.encoding, GitPath.CLEAN_ENV).last_commit()
 
        except subprocess.CalledProcessError:
 
            pass
 
        if args.revision is None:
 
            parser.error("no --revision specified and not found from input path")
 
    args.output_link_path = args.git_output.dir_path / args.input_path.with_suffix('.html').name
 
    args.output_file_path = args.output_link_path.with_suffix('.{}.html'.format(args.revision))
 
    return args
 

	
 
class GitOperation:
 
    def __init__(self, args):
 
        self.args = args
 
        self.git_path = args.git_output
 
        self.exitcode = None
 
        self.on_work_tree = self.git_path.can_run() and self.git_path.is_work_tree()
 

	
 
    def run(self):
 
        arg_state = getattr(self.args, self.NAME)
 
        if arg_state is None:
 
            arg_state = self.should_run()
 
        if not arg_state:
 
            return
 
        try:
 
            self.exitcode = self.run_git() or 0
 
        except subprocess.CalledProcessError as error:
 
            self.exitcode = error.returncode
 

	
 

	
 
class GitPull(GitOperation):
 
    NAME = 'pull'
 

	
 
    def should_run(self):
 
        return self.on_work_tree and not self.git_path.has_staged_changes()
 

	
 
    def run_git(self):
 
        self.git_path.operate(['fetch', '--no-tags'])
 
        self.git_path.operate(['merge', '--ff-only'])
 

	
 

	
 
class GitCommit(GitOperation):
 
    NAME = 'commit'
 
    VERB = 'committed'
 

	
 
    def __init__(self, args):
 
        super().__init__(args)
 
        try:
 
            self._should_run = ((not self.git_path.has_staged_changes())
 
                                and self.git_path.in_sync_with_upstream())
 
        except subprocess.CalledProcessError:
 
            self._should_run = False
 

	
 
    def should_run(self):
 
        return self.on_work_tree and self._should_run
 

	
 
    def run_git(self):
 
        self.git_path.operate([
 
            'add', str(self.args.output_file_path), str(self.args.output_link_path),
 
        ])
 
        commit_message = self.args.commit_message.format(
 
            filename=self.args.output_link_path.name,
 
            revision=self.args.revision,
 
        )
 
        self.git_path.operate(['commit', '-m', commit_message])
 

	
 

	
 
class GitPush(GitCommit):
 
    NAME = 'push'
 
    VERB = 'pushed'
 

	
 
    def run_git(self):
 
        self.git_path.operate(['push'])
 

	
 

	
 
def write_output(args):
 
    converter = markdown.Markdown(
 
        extensions=[
 
            mdx_tables.TableExtension(),
 
            mdx_sane_lists.SaneListExtension(),
 
            mdx_smarty.SmartyExtension(),
 
            mdx_toc.TocExtension(),
 
        ],
 
        output_format='html5',
 
    )
 
    header = TEMPLATE_HEADER
 
    with args.input_path.open(encoding=args.encoding) as src_file:
 
        for line in src_file:
 
            if line.startswith('# '):
 
                subtitle = line[2:].replace('Software Freedom Conservancy', '').strip()
 
                header = header.replace(
 
                    '{% block subtitle %}',
 
                    '{{% block subtitle %}}{} - '.format(subtitle),
 
                )
 
                break
 
        src_file.seek(0)
 
        body = converter.convert(src_file.read())
 
    with tempfile.NamedTemporaryFile(
 
            'w',
 
            encoding=args.encoding,
 
            dir=args.git_output.dir_path.as_posix(),
 
            suffix='.html',
 
            delete=False,
 
    ) as tmp_out:
 
        try:
 
            tmp_out.write(header)
 
            tmp_out.write(body)
 
            tmp_out.write(TEMPLATE_FOOTER)
 
            tmp_out.flush()
 
            os.rename(tmp_out.name, str(args.output_file_path))
 
        except BaseException:
 
            os.unlink(tmp_out.name)
 
            raise
 
    if args.output_link_path.is_symlink():
 
        args.output_link_path.unlink()
 
    args.output_link_path.symlink_to(args.output_file_path.name)
 

	
 
def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
 
    args = parse_arguments(arglist)
 
    pull = GitPull(args)
 
    pull.run()
 
    if pull.exitcode:
 
        return pull.exitcode
 
    write_output(args)
 
    ops = [GitCommit(args), GitPush(args)]
 
    for op in ops:
 
        op.run()
 
        if op.exitcode != 0:
 
            exitcode = op.exitcode or 0
 
            break
 
    else:
 
        exitcode = 0
 
    print(args.input_path.name, "converted,",
 
          ", ".join(op.VERB if op.exitcode == 0 else "not " + op.VERB for op in ops),
 
          file=stdout)
 
    return exitcode
 

	
 
if __name__ == '__main__':
 
    exit(main())
 

	
conservancy/feeds.py
Show inline comments
 
from datetime import datetime
 
from functools import reduce
 
import itertools
 
import operator
 

	
 
from django.conf import settings
 
from django.contrib.syndication.views import Feed
 
from django.shortcuts import render
 
from django.utils.feedgenerator import Rss201rev2Feed
 

	
 
from .blog.models import Entry as BlogEntry
 
from .news.models import PressRelease
 

	
 

	
 
class ConservancyFeedBase(Feed):
 
    def copyright_holder(self): return "Software Freedom Conservancy"
 
    def copyright_holder(self):
 
        return "Software Freedom Conservancy"
 

	
 
    def license_no_html(self): return "Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License."
 
    def license_no_html(self):
 
        return "Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License."
 

	
 
    def item_copyright(self, item):
 
        year = 2008
 
        for attr in ('pub_date', 'date_created', 'date_last_modified'):
 
            if hasattr(item, attr):
 
                if hasattr(getattr(item, attr), 'year'):
 
                    year = getattr(getattr(item, attr), 'year')
 
                    break
 
        return 'Copyright (C) %d, %s.  %s' % (year, self.copyright_holder(), self.license_no_html())
 

	
 
    def item_extra_kwargs(self, item):
 
        year = 2008
 
        for attr in ('pub_date', 'date_created', 'date_last_modified'):
 
            if hasattr(item, attr):
 
                if hasattr(getattr(item, attr), 'year'):
 
                    year = getattr(getattr(item, attr), 'year')
 
                    break
 
        return { 'year' : year }
 

	
 
class PressReleaseFeed(Feed):
 
    get_absolute_url = '/feeds/news/'
 
    title = "Software Freedom Conservancy News"
 
    link = "/news/"
 
    description = ""
 

	
 
    def items(self):
 
        return PressRelease.objects.filter(pub_date__lte=datetime.now(),
 
                                           sites__id__exact=settings.SITE_ID).order_by('-pub_date')[:10]
 
    def item_title(self, item):
 
        return item.headline
 

	
 
    def item_description(self, item):
 
        return item.summary
 

	
 
    def item_pubdate(self, item):
 
        return item.pub_date
 

	
 
class OmnibusFeedType(Rss201rev2Feed):
 
    def root_attributes(self):
 
        attrs = super().root_attributes()
 
        attrs['xmlns:itunes'] = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
 
        attrs['xmlns:atom'] = 'http://www.w3.org/2005/Atom'
 
        attrs['xmlns:media'] = 'http://search.yahoo.com/mrss/'
 
        attrs['xmlns:dc'] = "http://purl.org/dc/elements/1.1/"
 
        return attrs
 

	
 
    def add_root_elements(self, handler):
 
        super().add_root_elements(handler)
 

	
 
    def add_item_elements(self, handler, item):
 
        super().add_item_elements(handler, item)
 
        # Block things that don't have an enclosure from iTunes in
 
        # case someone uploads this feed there.
 
        handler.addQuickElement("itunes:block", 'Yes')
 

	
 
class OmnibusFeed(ConservancyFeedBase):
 
    get_absolute_url = '/feeds/omnibus/'
 
    feed_type = OmnibusFeedType
 
    link ="/news/"
 
    title = "The Software Freedom Conservancy"
 
    description = "An aggregated feed of all RSS content available from the Software Freedom Conservancy, including both news items and blogs."
 
    title_template = "feeds/omnibus_title.html"
 
    description_template = "feeds/omnibus_description.html"
 
    author_email = "info@sfconservancy.org"
 
    author_link = "https://sfconservancy.org/"
 
    author_name = "Software Freedom Conservancy"
 

	
 
    def item_title(self, item):
 
        return item.headline
 

	
 
    def item_description(self, item):
 
        return item.summary
 

	
 
    def item_enclosure_mime_type(self): return "audio/mpeg"
 
    def item_enclosure_mime_type(self):
 
        return "audio/mpeg"
 

	
 
    def item_enclosure_url(self, item):
 
        if hasattr(item, 'mp3_path'):
 
            return "https://sfconservancy.org" + item.mp3_path
 
    def item_enclosure_length(self, item):
 
        if hasattr(item, 'mp3_path'):
 
            return item.mp3_length
 

	
 
    def item_pubdate(self, item):
 
        return item.pub_date
 

	
 
    def item_author_name(self, item):
 
        if item.omnibus_type == "blog":
 
            return item.author.formal_name
 
        else:
 
            return "Software Freedom Conservancy"
 

	
 
    def item_author_link(self, obj):
 
        return "https://sfconservancy.org"
 

	
 
    def item_author_email(self, item):
 
        if item.omnibus_type == "news":
 
            return "info@sfconservancy.org"
 
        elif hasattr(item, 'author'):
 
            return "%s@sfconservancy.org" % item.author
 
        else:
 
            return "info@sfconservancy.org"
 

	
 
    def item_pubdate(self, item):
 
        if item.omnibus_type == "event":
 
            return item.date_created
 
        else:
 
            return item.pub_date
 

	
 
    def item_link(self, item):
 
        return item.get_absolute_url()
 

	
 
# http://groups.google.ca/group/django-users/browse_thread/thread/d22e8a8f378cf0e2
 

	
 
    def items(self):
 
        blogs = BlogEntry.objects.filter(pub_date__lte=datetime.now()).order_by('-pub_date')[:25]
 
        for bb in blogs:
 
            bb.omnibus_type = "blog"
 
            bb.omnibus_feed_description_template = "feeds/blog_description.html"
 
            bb.omnibus_feed_title_template = "feeds/blog_title.html"
 

	
 
        news = PressRelease.objects.filter(pub_date__lte=datetime.now(),
 
                                           sites__id__exact=settings.SITE_ID).order_by('-pub_date')[:25]
 
        for nn in news:
 
            nn.omnibus_type = "news"
 
            nn.omnibus_feed_description_template = "feeds/news_description.html"
 
            nn.omnibus_feed_title_template = "feeds/news_title.html"
 

	
 
        a  = [ ii for ii in itertools.chain(blogs, news)]
 
        a.sort(key=operator.attrgetter('pub_date'), reverse=True)
 
        return a
 

	
 

	
 
    def item_extra_kwargs(self, item):
 
        return super().item_extra_kwargs(item)
 

	
 
class BlogFeed(ConservancyFeedBase):
 
    link = "/blog/"
 
    get_absolute_url = '/feeds/blog/'
 

	
 
    def get_object(self, request):
 
        return request
 

	
 
    def title(self, obj):
 
        answer = "The Software Freedom Conservancy Blog"
 

	
 
        GET = obj.GET
 
        tags = []
 
        if 'author' in GET:
 
            tags = GET.getlist('author')
 
        if 'tag' in GET:
 
            tags += GET.getlist('tag')
 

	
 
        if len(tags) == 1:
 
            answer += " (" + tags[0] + ")"
 
        elif len(tags) > 1:
 
            firstTime = True
 
            done = {}
 
            for tag in tags:
 
                if tag in done: continue
 
                if tag in done:
 
                    continue
 
                if firstTime:
 
                    answer += " ("
 
                    firstTime = False
 
                else:
 
                    answer += ", "
 
                answer += tag
 
                done[tag] = tag
 
            answer += ")"
 
        else:
 
            answer += "."
 
        return answer
 
        
 
    def description(self, obj):
 
        answer = "Blogs at the Software Freedom Conservancy"
 

	
 
        GET = obj.GET
 
        tags = []
 
        if 'author' in GET: tags = GET.getlist('author')
 
        if 'tag' in GET:    tags += GET.getlist('tag')
 
        if 'author' in GET:
 
            tags = GET.getlist('author')
 
        if 'tag' in GET:
 
            tags += GET.getlist('tag')
 

	
 
        done = {}
 
        if len(tags) == 1:
 
            answer += " tagged with " + tags[0]
 
        elif len(tags) > 1:
 
            firstTime = True
 
            for tag in tags:
 
                if tag in done: continue
 
                if tag in done:
 
                    continue
 
                if firstTime:
 
                    answer += " tagged with "
 
                    firstTime = False
 
                else:
 
                    answer += " or "
 
                answer += tag
 
                done[tag] = tag
 
        else:
 
            answer = "All blogs at the Software Freedom Conservancy"
 
        answer += "."
 

	
 
        return answer
 
        
 
    def item_title(self, item):
 
        return item.headline
 

	
 
    def item_description(self, item):
 
        return item.summary
 

	
 
    def item_author_name(self, item):
 
        return item.author.formal_name
 

	
 
    def item_author_email(self, item):
 
        return "%s@sfconservancy.org" % item.author
 

	
 
    def item_pubdate(self, item):
 
        return item.pub_date
 

	
 
    def items(self, obj):
 
        GET = obj.GET
 

	
 
        def OR_filter(field_name, subfield_name, objs):
 
            from django.db.models import Q
 
            return reduce(lambda x, y: x | y,
 
                          [Q(**{'{}__{}'.format(field_name, subfield_name): x})
 
                           for x in objs])
 

	
 
        queryset = BlogEntry.objects.filter(pub_date__lte=datetime.now())
 

	
 
        if 'author' in GET:
 
            authors = GET.getlist('author')
 
            queryset = queryset.filter(OR_filter('author', 'username', authors))
 

	
 
        if 'tag' in GET:
 
            tags = GET.getlist('tag')
 
            queryset = queryset.filter(OR_filter('tags', 'slug', tags))
 

	
 
        return queryset.order_by('-pub_date')[:10]
 

	
 

	
 
def view(request):
 
    """Listing of all available feeds
 
    """
 

	
 
    feeds = (PressReleaseFeed, BlogFeed, OmnibusFeed)
 
    return render(request, "feeds.html", {'feeds': feeds})
conservancy/news/models.py
Show inline comments
 
from datetime import datetime, timedelta
 

	
 
from django.conf import settings
 
from django.contrib.sites.models import Site
 
from django.db import models
 

	
 
from .. import bsoup
 
from ..events.models import Event
 
from ..staff.models import Person
 

	
 

	
 
class PressRelease(models.Model, bsoup.SoupModelMixin):
 
    """News release model"""
 

	
 
    headline = models.CharField(max_length=300)
 
    subhead = models.CharField(max_length=300, blank=True)
 
    slug = models.SlugField(unique_for_date="pub_date",
 
                            help_text=("automatically built from headline"))
 
    summary = models.TextField(help_text="First paragraph (raw HTML)")
 
    body = models.TextField(help_text="Remainder of post (raw HTML)",
 
                            blank=True)
 
    pub_date = models.DateTimeField("date [to be] published")
 
    sites = models.ManyToManyField(Site)
 

	
 
    date_last_modified = models.DateTimeField(auto_now=True)
 

	
 
    class Meta:
 
        ordering = ("-pub_date",)
 
        get_latest_by = "pub_date"
 

	
 
    SOUP_ATTRS = ['summary', 'body']
 

	
 
    def __str__(self):
 
        return self.headline
 

	
 
    def get_absolute_url(self):
 
        return "/news/{}/{}/".format(self.pub_date.strftime("%Y/%b/%d").lower(),
 
                                  self.slug)
 
        return "/news/{}/{}/".format(
 
            self.pub_date.strftime("%Y/%b/%d").lower(),
 
            self.slug,
 
        )
 

	
 
    def is_recent(self):
 
        return self.pub_date > (datetime.now() - timedelta(days=5))
 
        # question: does datetime.now() do a syscall each time is it called?
 

	
 
    def is_in_past_month(self):
 
        # This function is deprecated.  Use the date_within template
 
        # filter instead (example in conservancy/templates/frontpage.html)
 
        return self.pub_date > (datetime.now() - timedelta(days=30))
 

	
 
    def save(self):
 
        if settings.DEBUG or True:
 
            super().save()
 
            return
 

	
 
        blog_name = 'Software Freedom Conservancy News'
 
        blog_url = 'https://www.sfconservancy.org/news/'
 
        post_url = ('https://www.sfconservancy.org'
 
                    + self.get_absolute_url())
 

	
 
        import xmlrpc.client
 

	
 
        # Ping Technorati
 
        j = xmlrpc.client.Server('http://rpc.technorati.com/rpc/ping')
 
        reply = j.weblogUpdates.ping(blog_name, blog_url)
 
        j.weblogUpdates.ping(blog_name, blog_url)
 

	
 
        # Ping Google Blog Search
 
        j = xmlrpc.client.Server('http://blogsearch.google.com/ping/RPC2')
 
        reply = j.weblogUpdates.ping(blog_name, blog_url, post_url)
 
        j.weblogUpdates.ping(blog_name, blog_url, post_url)
 

	
 
        # Call any superclass's method
 
        super().save()
 

	
 
class ExternalArticleTag(models.Model):
 
    """A way to tag external articles"""
 

	
 
    label = models.CharField(max_length=100)
 

	
 
    date_created = models.DateField(auto_now_add=True)
 

	
 
    def __str__(self):
 
        return self.label
 

	
 
class PublicExternalArticleManager(models.Manager):
 
    def get_queryset(self):
 
        return super().get_queryset().filter(visible=True)
 

	
 
class ExternalArticle(models.Model):
 
    """A system for displaying Conservancy news mentions on the site.
 

	
 
    (Currently unused)
 
    """
 

	
 
    title = models.CharField(max_length=400)
 
    info = models.CharField(help_text="subscribers only? audio? pdf warning?",
 
                            blank=True, max_length=300)
 
    publication = models.CharField("source of article", max_length=300)
 
    # verify_exists removed https://docs.djangoproject.com/en/1.7/releases/1.4/
 
    url = models.URLField(blank=True)
 
    date = models.DateField()
 
    visible = models.BooleanField(help_text="Whether to display on website", default=True)
 

	
 
    tags = models.ManyToManyField(ExternalArticleTag, blank=True)
 
    people = models.ManyToManyField(Person, blank=True)
 
    event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.CASCADE)
 
    press_release = models.ForeignKey(PressRelease, null=True, blank=True, on_delete=models.CASCADE)
 

	
 
    date_created = models.DateField(auto_now_add=True)
 

	
 
    class Meta:
 
        ordering = ("-date_created",)
 
        get_latest_by = "date_created"
 

	
 
    def __str__(self):
 
        return "{} ({})".format(self.title, self.publication)
 

	
 
    objects = models.Manager()
 
    public = PublicExternalArticleManager()
 

	
conservancy/news/views.py
Show inline comments
 
from datetime import datetime
 

	
 
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
 
from django.http import HttpResponse
 
from django.shortcuts import render
 
from django.views.generic import ListView
 
from django.views.generic.dates import (
 
    DateDetailView,
 
    DayArchiveView,
 
    MonthArchiveView,
 
    YearArchiveView,
 
)
 

	
 
from ..events.models import Event
 
from .models import ExternalArticle, PressRelease
 
from .models import PressRelease
 

	
 

	
 
class NewsListView(ListView):
 
    extra_context = {}
 
    def get_context_data(self, **kwargs):
 
        context = super().get_context_data(**kwargs)
 
        # context['key'] = 'value'
 
        context.update(self.extra_context)
 
        return context
 
                                    
 
def listing(request, *args, **kwargs):
 
    news_queryset = PressRelease.objects.all()
 

	
 
#    if (not kwargs.has_key('allow_future')) or not kwargs['allow_future']:
 
    news_queryset = news_queryset.filter(**{'%s__lte' % kwargs['date_field']:
 
                          datetime.now()})
 
    news_queryset = news_queryset.filter(
 
        **{'%s__lte' % kwargs['date_field']: datetime.now()}
 
    )
 

	
 
    date_list = news_queryset.dates(kwargs['date_field'], 'year')
 

	
 
    paginate_by = kwargs.get('paginate_by', 6)  # Show 6 news items per page, by default
 
    paginator = Paginator(news_queryset, paginate_by)
 

	
 
    page = request.GET.get('page')
 
    try:
 
        news = paginator.page(page)
 
    except PageNotAnInteger:
 
        # If page is not an integer, deliver first page.
 
        news = paginator.page(1)
 
    except EmptyPage:
 
        # If page is out of range (e.g. 9999), deliver last page of results.
 
        news = paginator.page(paginator.num_pages)
 

	
 
    return render(request, 'news/pressrelease_list.html', {"news": news, "date_list" : date_list})
 

	
 
class NewsYearArchiveView(YearArchiveView):
 
    # queryset = Article.objects.all()
 
    # date_field = "pub_date"
 
    make_object_list = True
 
    allow_future = True
 

	
 
# def archive_year(request, **kwargs):
 
#     callable = NewsYearArchiveView.as_view(**kwargs)
 
#     return callable(request)
 

	
 
class NewsMonthArchiveView(MonthArchiveView):
 
    allow_future = True
 

	
 
# def archive_month(request, **kwargs):
 
#     # return HttpResponse("archive_month")
 
#     callable = NewsMonthArchiveView.as_view(**kwargs)
 
#     return callable(request)
 

	
 
class NewsDayArchiveView(DayArchiveView):
 
    allow_future = True
 

	
 
# def archive_day(request, **kwargs):
 
#     # return HttpResponse("archive_day")
 
#     callable = NewsDayArchiveView.as_view(**kwargs)
 
#     return callable(request)
 

	
 
class NewsDateDetailView(DateDetailView):
 
    # extra_context = {}
 
    allow_future = True
 
    # slug_url_kwarg = 'slug'
 

	
 
    # def get_context_data(self, **kwargs):
 
    #     context = super(NewsDateDetailView, self).get_context_data(**kwargs)
 
    #     context.update(self.extra_context)
 
    #     return context
 

	
 
# def object_detail(request, **kwargs):
 
#     # extra_context = {}
 
#     # extra_context['slug'] = kwargs['slug']
 
#     # del kwargs['slug']
 
#     # kwargs['extra_context'] = extra_context
 
#     # return HttpResponse("object_detail: " + str(kwargs))
 
#     # slug = kwargs['slug']
 
#     # del kwargs['slug']
 
#     callable = NewsDateDetailView.as_view(**kwargs)
 
#     return callable(request)
 

	
conservancy/podjango/models.py
Show inline comments
 
#  Copyright (C) 2008       Bradley M. Kuhn <bkuhn@ebb.org>
 
#  Copyright (C) 2006, 2007 Software Freedom Law Center, Inc.
 
#
 
# This software's license gives you freedom; you can copy, convey,
 
# propogate, redistribute and/or modify this program under the terms of
 
# the GNU Affero General Public License (AGPL) as published by the Free
 
# Software Foundation (FSF), either version 3 of the License, or (at your
 
# option) any later version of the AGPL published by the FSF.
 
#
 
# This program is distributed in the hope that it will be useful, but
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
 
# General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program in a file in the toplevel directory called
 
# "AGPLv3".  If not, see <http://www.gnu.org/licenses/>.
 
#
 
from datetime import datetime, timedelta
 

	
 
from django.db import models
 
from django.urls import reverse
 

	
 

	
 
class Podcast(models.Model):
 
    """An ongoing series of episodes."""
 
    title = models.CharField(max_length=50)
 
    slug = models.SlugField(unique=True)
 
    long_description = models.TextField(blank=True)
 
    icon = models.ImageField(null=True)
 

	
 
    def __str__(self):
 
        return self.title
 

	
 

	
 
class CastTag(models.Model):
 
    """Tagging for casts"""
 

	
 
    label = models.CharField(max_length=100)
 
    slug = models.SlugField()
 

	
 
    class Meta:
 
        db_table = 'cast_tags'  # legacy
 
        verbose_name = 'episode tag'
 
        verbose_name_plural = 'episode tags'
 

	
 

	
 
    def __str__(self):
 
        return self.label
 

	
 

	
 
class CastManager(models.Manager):
 
    def get_queryset(self):
 
        # Temporarily filter out old FaiF episodes we've imported.
 
        return super().get_queryset().filter(pub_date__year__gte=2024)
 

	
 

	
 
class Cast(models.Model):
 
    """A podcast episode."""
 

	
 
    title = models.CharField(max_length=200)
 
    slug = models.SlugField(unique=True)
 
    summary = models.TextField(help_text="Use raw HTML.  This summary is not included at the beginning of the body when the entry is displayed.  It used only for the material on the front page.")
 
    body = models.TextField(help_text="Use raw HTML.  Include the full body of any show notes or other information about this episode.  It will be labelled on the site as Show Notes.  It is included on the detail entry, and in the description data on the cast RSS feed.")
 
    pub_date = models.DateTimeField()
 
    podcast = models.ManyToManyField(Podcast)
 
    tags = models.ManyToManyField(CastTag, blank=True)
 
    ogg_path = models.CharField(
 
        max_length=300, blank=True,
 
        help_text="Local filename of the Ogg file, relative to webroot.  File should be uploaded into the static media area for casts.")
 
    mp3_path = models.CharField(
 
        max_length=300, blank=True,
 
        help_text="Local filename of the mp3 file, relative to webroot.  File should be uploaded into the static media area for casts.")
 
    ogg_length = models.IntegerField(blank=False, help_text="size in bytes of ogg file")
 
    mp3_length = models.IntegerField(blank=False, help_text="size in bytes of mp3 file")
 
    duration = models.CharField(max_length=8, blank=False, help_text="length in hh:mm:ss of mp3 file")
 
    date_created = models.DateTimeField(auto_now_add=True)
 
    date_last_modified = models.DateTimeField(auto_now=True)
 

	
 
    objects = CastManager()
 

	
 
    class Meta:
 
        db_table = 'casts_entries'  # legacy
 
        verbose_name = 'episode'
 
        verbose_name_plural = 'episodes'
 
        ordering = ('-pub_date',)
 
        get_latest_by = 'pub_date'
 

	
 
    def __str__(self):
 
        return self.title
 

	
 
    def month_str(self):
 
        return self.pub_date.strftime("%b").lower()
 

	
 
    def is_recent(self):
 
        return self.pub_date > (datetime.now() - timedelta(days=14))
 
        # question: does datetime.now() do a syscall each time is it called?
conservancy/settings/dev.py
Show inline comments
 
from .base import *
 
from .base import *  # NOQA
 

	
 
DEBUG = True
 
ALLOWED_HOSTS = ['*']
 

	
 
DATABASES = {
 
    'default': {
 
        'NAME': 'conservancy-website.sqlite3',
 
        'ENGINE': 'django.db.backends.sqlite3',
 
    }
 
}
 

	
 
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
 

	
 
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
conservancy/settings/prod.py
Show inline comments
 
import json
 

	
 
from django.core.exceptions import ImproperlyConfigured
 

	
 
from .base import *
 
from .base import *  # NOQA
 

	
 
DEBUG = False
 
ALLOWED_HOSTS = ['www.sfconservancy.org', 'sfconservancy.org']
 

	
 
ADMINS = [
 
    ('Bradley M. Kuhn', 'sysadmin@sfconservancy.org'),
 
    ('Ben Sturmfels', 'sysadmin+conservancy@sturm.com.au'),
 
]
 

	
 
MANAGERS = [
 
    ('Bradley M. Kuhn', 'sysadmin@sfconservancy.org'),
 
]
 

	
 
DATABASES = {
 
    'default': {
 
        'NAME': '/var/lib/www/database/conservancy-website.sqlite3',
 
        'ENGINE': 'django.db.backends.sqlite3',
 
    }
 
}
 

	
 
# Apache/mod_wsgi doesn't make it straightforward to pass environment variables
 
# to Django (can't use the Apache config).
 
with open(BASE_DIR.parent / 'secrets.json') as f:
 
with open(BASE_DIR.parent / 'secrets.json') as f:  # NOQA
 
    secrets = json.load(f)
 

	
 
def get_secret(secrets, setting):
 
    try:
 
        return secrets[setting]
 
    except KeyError:
 
        raise ImproperlyConfigured(f'Missing secret \'{setting}\'')
 

	
 
SECRET_KEY = get_secret(secrets, 'SECRET_KEY')
 

	
 
SESSION_COOKIE_SECURE = True
0 comments (0 inline, 0 general)