Beispiel #1
0
def intercept_requests_to_statics(url, *a, **k):
    """
    Utility function to use with mock.path.
    Blocks all requests through `requests.get` except for those to local static files.
    In that case makes `requests.get` return file content without going through `httplib`
    """
    logging.info("intercept: %s", url)
    if url.startswith(settings.STATIC_URL):
        r = requests.Response()
        try:
            path = url[len(settings.STATIC_URL):]
            path = path.split('?')[0]  # FIXME use urlparse
            file_path = find_static_file(path)
            logging.info("path:%r", path)
            logging.info("file+path:%r", file_path)
            if file_path and os.path.isfile(file_path):
                with open(file_path) as f:
                    r._content = f.read()
                    r.status_code = 200
                    logging.info("response: %s", r._content)
                    logging.info("response: %s", r.content)
            else:
                r.status_code = 404
        except Exception, e:
            r.status_code = 500
            r.reason = e.message
            import traceback
            r._content = traceback.format_exc()
        finally:
Beispiel #2
0
def intercept_requests_to_statics(url, *a, **k):
    """
    Utility function to use with mock.path.
    Blocks all requests through `requests.get` except for those to local static files.
    In that case makes `requests.get` return file content without going through `httplib`
    """
    logging.info("intercept: %s", url)
    if url.startswith(settings.STATIC_URL):
        r = requests.Response()
        try:
            path = url[len(settings.STATIC_URL):]
            path = path.split('?')[0]  # FIXME use urlparse
            file_path = find_static_file(path)
            logging.info("path:%r", path)
            logging.info("file+path:%r", file_path)
            if file_path and os.path.isfile(file_path):
                with open(file_path) as f:
                    r._content = f.read()
                    r.status_code = 200
                    logging.info("response: %s", r._content)
                    logging.info("response: %s", r.content)
            else:
                r.status_code = 404
        except Exception, e:
            r.status_code = 500
            r.reason = e.message
            import traceback
            r._content = traceback.format_exc()
        finally:
Beispiel #3
0
def get_static_path(path: str) -> Path:
    """
    Resolves a path commonly passed to `{% static %}` into a filesystem path
    """
    static_file_path = find_static_file(path)
    if static_file_path is None:
        raise FileNotFoundError(path)
    return Path(static_file_path)
Beispiel #4
0
def encode_static(path, encoding='base64', file_type='image'):
    """
    a template tag that returns a encoded string representation of a staticfile
    Usage::
        {% encode_static path [encoding] %}
    Examples::
        <img src="{% encode_static 'path/to/img.png' %}">
    """
    file_path = find_static_file(path)
    ext = file_path.split('.')[-1]
    file_str = get_file_data(file_path).encode(encoding)
    return u"data:{0}/{1};{2},{3}".format(file_type, ext, encoding, file_str)
Beispiel #5
0
def fetch_resources(uri, rel):
    if uri.startswith(settings.STATIC_URL):
        uri = uri[len(settings.STATIC_URL):]
        path = find_static_file(uri)
        if path is None:
            raise Exception('Error retrieving static file ' +
                            '"{0}" during pdf generation.'.format(uri))
    else:
        print ('Warning: path {0} is not a staticfile'.format(uri))
        path = uri

    return path
def raw_static(path):
    """
    a template tag that returns a raw staticfile
    Usage::
        {% raw_static path %}
    Examples::
        <style>{% raw_static path/to/style.css %}</style>
    """
    if path.startswith(settings.STATIC_URL):
        # remove static_url if its included in the path
        path = path.replace(settings.STATIC_URL, '')
    file_path = find_static_file(path)
    return get_file_data(file_path)
def image_encode_static(path, encoding='base64', file_type='image'):
    """
    a template tag that returns a encoded string representation of a staticfile
    Usage::
        {% encode_static path [encoding] %}
    Examples::
        <img src="{% encode_static 'path/to/img.png' %}">
    """
    print(path)
    print('------------------------')
    # file_path = path
    file_path = find_static_file(path)
    ext = file_path.split('.')[-1]
    file_str = image_to_data_url(file_path)
    # file_str = base64.urlsafe_b64encode(get_file_data(file_path))
    return "data:{0}/{1};{2},{3}".format(file_type, ext, encoding, file_str)
Beispiel #8
0
def typebar(request):
    timerange = request.GET.get('timerange', None)

    msg = 'Statis/typebar '
    logdict = {}
    params = []

    if timerange is not None:
        params.append('timerange='+timerange)
        msg += 'timerange({timerange}) '
        logdict['timerange'] = timerange

    logger.debug(_(msg, **logdict))

    today = datetime.today()
    fil = 'images/' + today.strftime('%Y-%m-%d') + '-typebar-' + hash6(params) + '.svg'
    if find_static_file(fil) is not None:
        return render(request, 'img.html', {'imgpath': fil})

    ts = te = None
    if timerange is not None:
        ts = timerange.split(',')[0]
        te = timerange.split(',')[1]
        ts = None if len(ts) == 0 else ts
        te = None if len(te) == 0 else te

    query = Q()

    if timerange is not None:
        if ts is not None:
            edate = datetime.today() - timedelta(days=int(ts))
            query &= Q(release_date__lte=edate)
        if te is not None:
            sdate = datetime.today() - timedelta(days=int(te))
            query &= Q(release_date__gte=sdate)

    tp = ['剧情', '喜剧', '动作', '爱情', '科幻', '动画', '悬疑',
          '惊悚', '恐怖', '犯罪', '同性', '音乐', '歌舞', '传记',
          '历史', '战争', '西部', '奇幻', '冒险', '灾难', '武侠',
          '情色', '纪录片']
    tpq = Q()
    for t in tp:
        tpq |= Q(tag__tag=t)

    query &= tpq

    lS = Score.objects.filter(id__exact=OuterRef('id')).order_by('-score_date')
    query &= Q(score__score_date=Subquery(lS.values('score_date')[:1]))

    qS = MT.objects.filter(query).values('id', 'tag__tag', 'score__score',
                                         'score__votes')

    logger.debug(str(qS.query))

    df = pd.DataFrame(qS).rename(columns={'tag__tag': 'tag',
                                          'score__score':'score',
                                          'score__votes':'votes'})

    if len(df) == 0:
        return render(request, 'img.html', {'imgpath': None})

    df = df.groupby('tag', sort=False).agg(size=('id', np.size),
                                      avgscore=('score', np.mean),
                                      avgvotes=('votes', np.mean))
    df = df.sort_values('size', ascending=False)[:20]

    # plot
    plt.style.use('ggplot')

    fontpath = '/usr/share/fonts/source-han-serif/SourceHanSerifSC-Regular.otf'
    font = mfm.FontProperties(fname=fontpath)

    fig, axs = plt.subplots(1, 2, squeeze=False)
    fig.set_size_inches(12, 4)

    ax0, ax1 = axs[0]
    ax0.bar(df.index, df['size'], color=colorMap(df.index))
    ax0.set_xticklabels(df.index, fontproperties=font, rotation='vertical')
    ax0twin = ax0.twinx()
    ax0twin.plot(df.index, df['avgscore'], marker='.')
    ax0twin.set_ylim([2, 10])

    ax1.bar(df.index, df['avgvotes'], color=colorMap(df.index))
    ax1.set_xticklabels(df.index, fontproperties=font, rotation='vertical')
    absfil = settings.STATICFILES_DIRS[0] + fil
    fig.savefig(absfil)
    plt.close(fig)

    return render(request, 'img.html', {'imgpath': fil})
Beispiel #9
0
def hottest(request):
    days = request.GET.get('days', 3)
    rdays =request.GET.get('rdays', None)
    counts = request.GET.get('counts', 30)

    params = []
    logdict = {}
    msg = 'Statistic[hottest]: '
    if days is not None:
        params.append('days='+days)
        msg += 'days({days}) '
        logdict['days'] = days
    if rdays is not None:
        params.append('rdays='+rdays)
        msg += 'rdays({rdays}) '
        logdict['rdays'] = rdays
    if counts is not None:
        params.append('counts='+str(counts))
        msg += 'counts({counts}) '
        logdict['counts'] = counts

    logger.debug(_(msg, **logdict))

    today = datetime.today()
    fil = 'images/' + today.strftime('%Y-%m-%d') + '-hottest-' + hash6(params) + '.svg'
    if find_static_file(fil) is not None:
        return render(request, 'img.html', {'imgpath': fil})
        #return FileResponse(open(absfil, 'rb'))

    sdate = today - timedelta(days=700)
    query = Q(id__release_date__gte=sdate)
    if rdays is not None:
        edate = today - timedelta(days=int(rdays))
        query &= Q(id__release_date__lte=edate)

    qS = Score.objects.filter(query).values('id', 'score_date', 'score', 'votes',
                                            'id__title', 'id__release_date')

    logger.debug(str(qS.query))

    df = pd.DataFrame(qS)
    df['score_date'] = pd.to_datetime(df['score_date'])
    df['id__release_date'] = pd.to_datetime(df['id__release_date'])

    sdate = datetime.today() - timedelta(days=int(days))
    df = df.groupby(by='id', sort=False).filter(lambda x:
                                x['score_date'].max() >= sdate
                                           and len(x) > 1)
    df = df.groupby(by='id', sort=False).apply(calvdiff, days=days)
    if len(df) == 0:
        return render(request, 'img.html', {'imgpath': None})

    df = df.sort_values('vavg', ascending=False)
    df = df[:counts]

    # plot
    plt.style.use('ggplot')

    fig, ax = plt.subplots()
    fig.set_size_inches(9, 6)
    fig.patch.set_visible(False)

    df['vavg'].plot.barh(ax=ax, color=colorMap(df['title']))
    #ax.get_yaxis().set_visible(False)
    ax.set_yticklabels([i for i in range(1, counts + 1)])
    ax.invert_yaxis()
    #ax.patch.set_visible(False)
    ax.legend().set_visible(False)

    fontpath = '/usr/share/fonts/source-han-serif/SourceHanSerifSC-Regular.otf'
    font = mfm.FontProperties(fname=fontpath)
    for idx in range(counts):
        disCoord = ax.transData.transform((df.iloc[idx].vavg, idx))
        disCoord[0] += 5
        inv = ax.transData.inverted()
        ax.annotate('{}({})'.format(df.iloc[idx].title, df.iloc[idx].score),
                    inv.transform(disCoord),
                    url='https://movie.douban.com/subject/'+str(df.index[idx])+'/',
                    fontproperties=font, va='center')
        '''
        ax.text(df.iloc[idx].vavg + 30, idx,
                '{}({})'.format(df.iloc[idx].title, df.iloc[idx].score),
                url='https://movie.douban.com/subject/' + str(df.index[idx]) + '/',
                fontproperties=font,
                va='center')
        '''

    absfil = settings.STATICFILES_DIRS[0] + fil
    fig.savefig(absfil)
    plt.close(fig)

    return render(request, 'img.html', {'imgpath': fil})
Beispiel #10
0
def celebrity(request):
    names = request.GET.get('names')

    params = []
    logdict = {}
    msg = 'Statistic[celebrity]: '
    if names is not None:
        params.append(names.strip())
        msg += 'names({names}) '
        logdict['names'] = names

    logger.debug(_(msg, **logdict))

    today = datetime.today()
    fil = 'images/' + today.strftime('%Y-%m-%d') + '-celebrity-' + \
                hash6(params) + '.svg'
    if find_static_file(fil) is not None:
        return render(request, 'img.html', {'imgpath': fil})

    query = Q()
    for s in names.split(','):
        s = s.strip()
        sq = Q()
        for n in s.split(' '):
            sq &= Q(celebrity_id__name__icontains=n)

        query |= sq

    lS = Score.objects.filter(id__exact=OuterRef('subject_id')).order_by('-score_date')
    query &= Q(subject_id__score__score_date=Subquery(lS.values('score_date')[:1]))
    query &= Q(subject_id__release_date__year__gt=1000)

    qS = Par.objects.filter(query).annotate(cid=F('celebrity_id'),
                                        sid=F('subject_id'),
                                        name=F('celebrity_id__name'),
                                        title=F('subject_id__title'),
                                        rdate=F('subject_id__release_date'),
                                        score=F('subject_id__score__score'),
                                        ).values('cid', 'name', 'sid', 'title',
                                                'rdate', 'score', 'role')

    logger.debug(str(qS.query))

    df = pd.DataFrame(qS)

    df['rdate'] = pd.to_datetime(df['rdate'])
    gps = df.groupby(['cid', 'role'], sort=False)

    nplot = len(gps)
    if nplot > 8:
        nplot = 8

    nrows = math.ceil(nplot / 2)
    ncols = 2 if nrows > 1 else nplot

    plt.style.use('ggplot')

    fig, axs = plt.subplots(nrows, ncols, squeeze=False)
    fig.set_size_inches(ncols * 6, nrows * 4)

    fontpath = '/usr/share/fonts/source-han-serif/SourceHanSerifSC-Regular.otf'
    font = mfm.FontProperties(fname=fontpath)

    roleDict = {'director': '导演', 'actor': '演员', 'scriptwriter': '编剧'}
    gen = flatten2D(axs)
    try:
        for crtuple, group in gps:
            ax = next(gen)
            group = group.sort_values('rdate')
            group['avgscore'] = group['score'].expanding().mean()
            logger.debug(group.loc[:, ['name', 'role', 'rdate', 'score', 'avgscore']])
            ax.plot(group['rdate'], group['avgscore'], marker='.',
                    label='avgscore')
            ax.plot(group['rdate'], group['score'], marker='.', label='score')
            formatter = DateFormatter('%Y')
            ax.xaxis.set_major_formatter(formatter)
            ax.legend(loc='lower right')
            ax.set_ylim([2, 10])
            ax.get_xaxis().axis_date()
            txt = group.iloc[0]['name'] + '(' + roleDict[group.iloc[0]['role']] + ')'
            ax.text(0.05, 0.9, txt, transform=ax.transAxes,
                        fontproperties=font)
    except StopIteration:
        pass
    finally:
        try:
            while(1):
                ax = next(gen)
                ax.set_visible(False)
        except StopIteration:
            pass

    absfil = settings.STATICFILES_DIRS[0] + fil
    fig.savefig(absfil)
    plt.close(fig)

    return render(request, 'img.html', {'imgpath': fil})