Skip to content

Commit

Permalink
get_locations_without_deleted_tweet method for acikyazilimagi#64
Browse files Browse the repository at this point in the history
  • Loading branch information
yarliganfatih committed Feb 7, 2023
1 parent bec8e6c commit edf49f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions applications/tweets/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"]
Expand All @@ -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)
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions trquake/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
ZEKAI_USERNAME = env("ZEKAI_USERNAME")
ZEKAI_PASSWORD = env("ZEKAI_PASSWORD")

TWITTER_BEARER_TOKEN = env("TWITTER_BEARER_TOKEN")

LOGGING = {
"version": 1,
Expand Down

0 comments on commit edf49f6

Please sign in to comment.