Esempio n. 1
0
def store_link_data(values):
    User = get_user_model()
    try:
        user = User.objects.get(pk=values['user.id'])
    except User.DoesNotExist:
        return None

    date = None
    if 'page.published_at' in values:
        date = dateparse(values['page.published_at'])
        if not is_aware(date):
            date = make_aware(date)

    upload_dir = settings.FORM_DATA_UPLOAD_DIR

    page_image_path = None
    if 'page.image.url' in values:
        r = requests.get(values['page.image.url'], stream=True)
        if r.ok:
            page_image_path = os.path.join(upload_dir, uuid4().hex)
            with open(page_image_path, 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

    author_image_path = None
    if 'author.image.url' in values:
        r = requests.get(values['author.image.url'], stream=True)
        if r.ok:
            author_image_path = os.path.join(upload_dir, uuid4().hex)
            with open(author_image_path, 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

    publisher_image_path = None
    if 'publisher.image.url' in values:
        r = requests.get(values['publisher.image.url'], stream=True)
        if r.ok:
            publisher_image_path = os.path.join(upload_dir, uuid4().hex)
            with open(publisher_image_path, 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

    author = None
    if 'author.name' in values:
        try:
            author = Author.objects.get(user=user, name=values['author.name'])
            if date and author.updated_at < date:
                pass
        except Author.DoesNotExist:
            author = Author(name=values['author.name'],
                            user=user,
                            updated_at=date or now())

            if author_image_path:
                im = Image.open(author_image_path)
                ext = guess_extension(Image.MIME[im.format])

                author.image_height = im.size[1]
                author.image_width = im.size[0]
                author.image.save(
                    os.path.basename(author_image_path) + ext,
                    File(open(author_image_path, 'rb')))

            author.save()

            for social_url in values.get('author.social', []):
                social = AuthorSocial(url=social_url, entity=author, user=user)
                social.save()

    publisher = None
    if 'publisher.name' in values:
        try:
            publisher = Publisher.objects.get(user=user,
                                              name=values['publisher.name'])
            if date and publisher.updated_at < date:
                pass
        except Publisher.DoesNotExist:
            publisher = Publisher(name=values['publisher.name'],
                                  user=user,
                                  updated_at=date or now())

            if publisher_image_path:
                im = Image.open(publisher_image_path)
                ext = guess_extension(Image.MIME[im.format])

                publisher.image_height = im.size[1]
                publisher.image_width = im.size[0]
                publisher.image.save(
                    os.path.basename(publisher_image_path) + ext,
                    File(open(publisher_image_path, 'rb')))

            publisher.save()

            for social_url in values.get('publisher.social', []):
                social = PublisherSocial(url=social_url,
                                         entity=publisher,
                                         user=user)
                social.save()

    link = Link(url=values.get('page.url'),
                title=values.get('page.title'),
                description=values.get('page.description'),
                text=values.get('page.body.text'),
                html=values.get('page.body.html'),
                author=author,
                publisher=publisher,
                user=user,
                published_at=date)

    if page_image_path:
        im = Image.open(page_image_path)
        ext = guess_extension(Image.MIME[im.format])

        link.image_height = im.size[1]
        link.image_width = im.size[0]
        link.image.save(
            os.path.basename(page_image_path) + ext,
            File(open(page_image_path, 'rb')))

    link.save()

    if 'page.keywords' in values:
        for name in values['page.keywords']:
            keyword, _ = LinkKeyword.objects.get_or_create(name=name)
            link.keywords.add(keyword)

    return link.id