Пример #1
0
def save_from_dribble(url):
    ui = UI.objects.filter(options__source_url=url).first()
    if ui is None:
        temp = get_dribble(url)
        if temp.get("images").__len__() > 0:
            user_temp = UserTemp.objects.filter(
                username=temp.get("user").get("username")).first()
            if user_temp is None:
                user_temp = UserTemp(
                    username=temp.get("user").get("username"),
                    display_name=temp.get("user").get("username"),
                    avatar_url=temp.get("user").get("avatar_url"),
                )
                user_temp.save()
            ui = UI(title=temp.get("title"),
                    description=temp.get("description"),
                    short_description=temp.get("short_description"),
                    options=temp.get("options"),
                    user_temp=user_temp,
                    user=user_temp.user)
            ui.save()
            for image in temp.get("images"):
                media = Media.objects.save_url(image.get("src"))
                if media is not None:
                    ui.medias.add(media)
                    print(MediaSerializer(media).data)
            for tag in temp.get("tags"):
                hash_tag = HashTag.objects.filter(slug=slugify(tag)).first()
                if hash_tag is None:
                    hash_tag = HashTag(title=tag.capitalize())
                    hash_tag.save()
                UITag.objects.create(hash_tag=hash_tag, ui=ui)
            ui.save()
    return ui
Пример #2
0
 def to_representation(self, instance):
     self.fields['media'] = MediaSerializer(read_only=True)
     self.fields['destination_start'] = DestinationSerializer(
         read_only=True)
     self.fields['destination_end'] = DestinationSerializer(read_only=True)
     self.fields['taxonomies'] = TaxonomySerializer(many=True,
                                                    read_only=True)
     return super(ScheduleSerializer, self).to_representation(instance)
Пример #3
0
def plant_universe_worker(k, n):
    out = fetch_by_hash_tag(k, n)
    items = out.get("results", [])
    pub = Publication.objects.get(pk=15)
    admin = User.objects.get(pk=1)
    for item in items:
        instance = Post.objects.filter(meta__ig_id=item.get("ig_id")).first()
        if instance is None and len(item.get("images", [])) > 0:
            user = sync_ig_user(item.get("user"))
            medias = []
            for img in item.get("images", []):
                media = Media.objects.save_url(img)
                medias.append(media.id)
                print(MediaSerializer(media).data.get("id"))
            meta = {
                "ig_id": item.get("ig_id"),
                "credit": item.get("user").get("username"),
                "medias": medias
            }
            new_post = Post.objects.create(
                title="Post by " + item.get("user").get("full_name")
                if item.get("user").get("full_name") else
                item.get("user").get("username"),
                description=item.get("caption")[:300]
                if item.get("caption") else None,
                meta=meta,
                primary_publication=pub,
                user=user if user is not None else admin,
                post_type="post",
                show_cms=True,
                status="POSTED")
            for tag in item.get("tags", []):
                if len(tag) < 100:
                    try:
                        term, is_created = Term.objects.get_or_create(
                            slug=tag, defaults={"title": tag})
                        pub_term, is_created = PublicationTerm.objects.get_or_create(
                            publication=pub,
                            term=term,
                            defaults={"taxonomy": "tag"})
                        new_post.terms.add(pub_term)
                        plants = plant_pub.posts.filter(terms__taxonomy="tag",
                                                        terms__term__slug=tag,
                                                        post_type="plant")
                        for plant in plants:
                            new_post.post_related.add(plant)
                    except Exception as e:
                        print(e)
                        pass
            # if item.get("comment_count"):
            #     print(get_comment(item.get("ig_id")))
    if out.get("next_id"):
        time.sleep(60)
        plant_universe_worker(k, out.get("next_id"))
Пример #4
0
 def handle(self, *args, **options):
     medias = Media.objects.filter(properties__width=None)
     for media in medias:
         req = requests.get(url=media.source_url,
                            headers={'User-Agent': 'Mozilla/5.0'},
                            allow_redirects=True)
         name = urlparse(media.source_url).path.split('/')[-1]
         if media.properties is None:
             media.properties = {}
         if req.status_code == 200:
             disposition = req.headers.get("Content-Disposition")
             if disposition:
                 test = disposition.split("=")
                 if len(test) > 1:
                     name = test[1].replace("\"", "")
             temp = NamedTemporaryFile(delete=True)
             temp.write(req.content)
             temp.flush()
             delete(media.path)
             media.path.save(name, File(temp))
             media.save()
             MediaSerializer.get_sizes(media, media)
         else:
             posts = Post.objects.filter(medias=media)
             for post in posts:
                 activities = Activity.objects.filter(
                     action_object_content_type_id=29,
                     action_object_object_id=str(post.id))
                 for activity in activities:
                     media.properties["status"] = "DELETED"
                     activity.db_status = -1
                     activity.save()
                     print(activity.id)
         media.properties["width"] = media.path.width
         media.properties["height"] = media.path.width
         media.properties["size"] = media.path.size
         media.save()
         print(media.id)
Пример #5
0
 def save(self, **kwargs):
     test = User.objects.filter(username=self.username).first()
     if test is None:
         test = User.objects.create_user(username=self.username,
                                         password="******")
         if not hasattr(test, 'profile'):
             profile = Profile.objects.create(user=test,
                                              nick=self.display_name)
         else:
             test.profile.nick = self.display_name
             test.profile.save()
             profile = test.profile
         if profile.media is None and self.avatar_url is not None:
             media = Media.objects.save_url(self.avatar_url)
             profile.media = media
             print(MediaSerializer(media).data)
             profile.save()
         test.save()
     self.user = test
     super(UserTemp, self).save(**kwargs)
Пример #6
0
 def to_representation(self, instance):
     self.fields['media'] = MediaSerializer(read_only=True)
     return super(ProfileSerializer, self).to_representation(instance)
Пример #7
0
 def to_representation(self, instance):
     self.fields['hash_tags'] = HashTagSerializer(read_only=True, many=True)
     self.fields['media'] = MediaSerializer(read_only=True)
     return super(BoardSerializer, self).to_representation(instance)
Пример #8
0
 def to_representation(self, instance):
     self.fields['term'] = TermSerializer(read_only=True)
     self.fields['media'] = MediaSerializer()
     return super(PubTermSerializer, self).to_representation(instance)
Пример #9
0
 def to_representation(self, instance):
     self.fields['media'] = MediaSerializer()
     return super(ThemeSerializer, self).to_representation(instance)
Пример #10
0
 def get_temp_media(self, instance):
     media = Media.objects.filter(posts__destination=instance).first()
     return MediaSerializer(media).data
Пример #11
0
 def to_representation(self, instance):
     self.fields['medias'] = MediaSerializer(many=True, read_only=True)
     self.fields['address'] = AddressSerializer(read_only=True)
     return super(DestinationSerializer, self).to_representation(instance)
Пример #12
0
 def to_representation(self, instance):
     self.fields['medias'] = MediaSerializer(many=True, read_only=True)
     return super(PostSerializer, self).to_representation(instance)