示例#1
0
def migrate_versions():
    XXX_this_hasnt_been_updated_for_having_multiple_git_dirs
    git_output = subprocess.check_output([GIT_PROGRAM, 'log'],
                                         cwd=models.GIT_DIR)
    commits = git_output.split('\n\ncommit ')
    commits[0] = commits[0][len('commit '):]
    print 'beginning loop'
    d = {}
    versions = [x.v for x in models.Version.objects.all()]
    for i, commit in enumerate(commits):
        (v, author, datestr, blank, changem) = commit.splitlines()
        if v in versions:
            continue
        fname = changem.split()[-1]
        changekind = changem.split()[0]
        if changekind == 'Reformat':
            continue
        date = datetime.strptime(' '.join(datestr.split()[1:-1]),
                                 '%a %b %d %H:%M:%S %Y')

        if not os.path.exists(os.path.join(
                models.GIT_DIR, fname)):  #file introduced accidentally
            continue

        url = 'http://%s' % fname
        try:
            article = models.Article.objects.get(url=url)
        except models.Article.DoesNotExist:
            url += '/'
            try:
                article = models.Article.objects.get(url=url)
            except models.Article.DoesNotExist:
                url = url[:-1]
                article = models.Article(url=url,
                                         last_update=date,
                                         last_check=date)
                if not article.publication(
                ):  #blogs aren't actually reasonable
                    continue

                article.save()

        text = subprocess.check_output([GIT_PROGRAM, 'show', v + ':' + fname],
                                       cwd=models.GIT_DIR)
        text = text.decode('utf-8')
        (date2, title, byline) = text.splitlines()[:3]

        boring = False

        print '%d/%d' % (i, len(commits)), url, v, date, title, byline, boring
        v = models.Version(article=article,
                           v=v,
                           date=date,
                           title=title,
                           byline=byline,
                           boring=boring)
        try:
            v.save()
        except models.IntegrityError:
            pass
示例#2
0
def update_article(article):
    parsed_article = load_article(article.url)
    if parsed_article is None:
        return
    to_store = unicode(parsed_article).encode('utf8')
    t = datetime.now()

    v, boring, diff_info = add_to_git_repo(to_store,
                                           url_to_filename(article.url),
                                           article)
    if v:
        logger.info('Modifying! new blob: %s', v)
        v_row = models.Version(
            v=v,
            boring=boring,
            title=parsed_article.title,
            byline=parsed_article.byline,
            date=t,
            article=article,
        )
        v_row.diff_info = diff_info
        v_row.save()
        if not boring:
            article.last_update = t
            article.save()
示例#3
0
def update_article(article):
    parsed_article = load_article(article.url)
    if parsed_article is None:
        return
    to_store = unicode(parsed_article).encode('utf8')
    t = datetime.now()
    logger.debug('Article parsed; trying to store')
    v, boring, diff_info = add_to_git_repo(to_store, article.filename(),
                                           article)
    if v:
        logger.info('Modifying! new blob: %s', v)
        v_row = models.Version(
            v=v,
            boring=boring,
            title=parsed_article.title,
            byline=parsed_article.byline,
            date=t,
            article=article,
        )
        v_row.diff_info = diff_info
        v_row.save()

        if not boring:
            article.last_update = t
            article.save()

            # Notify slack
            team = models.SlackBotTeam.objects.get()
            standalone = models.StandaloneArticle.objects.get(url=article.url)
            if standalone:
                user = standalone.added_by
                link = '/article-history/%s' % article.url
                link = '%s%s' % (settings.WEBAPP_URL, link)
                bot_text = 'Veo cambios en %s (%s)' % (article.url, link)

                if not user.startswith('web_frontend'):
                    Client = SlackClient(team.bot_access_token)
                    dm = Client.api_call(method='im.open', user=user)
                    channel = dm.get('channel').get('id')
                    channel = channel if channel else user
                    Client.api_call(method='chat.postMessage',
                                    channel=channel,
                                    text=bot_text)