diff --git a/README.md b/README.md index 68807f1..3d6fd8c 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ CELERY_BROKER_URL=trquake-redis ZEKAI_USERNAME= # zekai.io kullanıcı adı ZEKAI_PASSWORD= # zekai.io şifre DEFAULT_ADMIN_PASSWORD= # ilk oluşturulan admin kullanıcısı için şifre +TWITTER_BEARER_TOKEN= # Twitter Developer Portal üzerinden alınmalı ``` Django Secret key oluşturmak için: diff --git a/applications/tweets/models.py b/applications/tweets/models.py index 5457cc3..9bc8709 100644 --- a/applications/tweets/models.py +++ b/applications/tweets/models.py @@ -1,5 +1,8 @@ from django.db import models from django.contrib.postgres.fields import ArrayField +import requests +from django.conf import settings +from django.db.models import Prefetch class Tweet(models.Model): @@ -13,6 +16,18 @@ class Tweet(models.Model): hashtags = ArrayField(base_field=models.CharField(max_length=255), null=True) user_account_created_at = models.DateTimeField(null=True, blank=True) media = models.CharField(max_length=512, null=True) + + @property + def is_deleted(self): + url = f"https://api.twitter.com/2/tweets/{self.tweet_id}" + payload={} + headers = { + 'Authorization': f'Bearer {settings.TWITTER_BEARER_TOKEN}' + } + response = requests.request("GET", url, headers=headers, data=payload) + data = response.text + + return data.errors[0].type == "https://api.twitter.com/2/problems/resource-not-found" class Meta: ordering = ["-id"] @@ -33,6 +48,16 @@ class Address(models.Model): class Meta: ordering = ["-id"] +class LocationManager(models.Manager): + @property + def get_locations_without_deleted_tweet(self): + return self.prefetch_related( + Prefetch( + "address__tweet", + queryset=Tweet.objects.exclude(is_deleted=True), + to_attr="tweets" + ) + ).filter(address__tweet__in=F("tweets")).distinct() class Location(models.Model): address = models.ForeignKey("tweets.Address", on_delete=models.CASCADE) @@ -44,6 +69,7 @@ class Location(models.Model): southwest_lat = models.FloatField(default=0.0) southwest_lng = models.FloatField(default=0.0) is_approved = models.BooleanField(default=False) + objects = LocationManager() @property def loc(self): diff --git a/trquake/settings/base.py b/trquake/settings/base.py index e23c00e..b8a046a 100644 --- a/trquake/settings/base.py +++ b/trquake/settings/base.py @@ -148,6 +148,7 @@ ZEKAI_USERNAME = env("ZEKAI_USERNAME") ZEKAI_PASSWORD = env("ZEKAI_PASSWORD") +TWITTER_BEARER_TOKEN = env("TWITTER_BEARER_TOKEN") LOGGING = { "version": 1,