Skip to content

Commit

Permalink
Merge pull request #7 from CodeForAfrica/ft-create-list#179920259
Browse files Browse the repository at this point in the history
Ft create list#179920259
  • Loading branch information
esirK committed Nov 4, 2021
2 parents d548c54 + a79ec87 commit 667ef8d
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Required
TWOOPSTRACKER_DATABASE_URL=
TWOOPSTRACKER_SECRET_KEY=
TWOOPSTRACKER_CONSUMER_KEY=
TWOOPSTRACKER_CONSUMER_SECRET=
TWOOPSTRACKER_ACCESS_TOKEN=
TWOOPSTRACKER_ACCESS_TOKEN_SECRET=

# End of Required

Expand Down
1 change: 1 addition & 0 deletions requirements-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ djangorestframework==3.12.4
environs[django]==9.3.4
greenlet==1.1.2
gunicorn[gevent, setproctitle]==20.1.0
tweepy==4.2.0
7 changes: 7 additions & 0 deletions twoopstracker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# Twitter API credentials
TWOOPSTRACKER_CONSUMER_KEY = env.str("TWOOPSTRACKER_CONSUMER_KEY", "")
TWOOPSTRACKER_CONSUMER_SECRET = env.str("TWOOPSTRACKER_CONSUMER_SECRET", "")
TWOOPSTRACKER_ACCESS_TOKEN = env.str("TWOOPSTRACKER_ACCESS_TOKEN", "")
TWOOPSTRACKER_ACCESS_TOKEN_SECRET = env.str("TWOOPSTRACKER_ACCESS_TOKEN_SECRET", "")

# CORS
CORS_ALLOW_ALL_ORIGINS = True

Expand Down
1 change: 1 addition & 0 deletions twoopstracker/twitterclient/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .twitter_client import TwitterClient # noqa
Empty file.
6 changes: 6 additions & 0 deletions twoopstracker/twitterclient/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TwitterclientConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "twoopstracker.twitterclient"
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions twoopstracker/twitterclient/twitter_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging

import tweepy
from django.conf import settings

logger = logging.getLogger(__name__)


class TwitterClient:
def __init__(self):
self.auth = tweepy.OAuthHandler(
settings.TWOOPSTRACKER_CONSUMER_KEY, settings.TWOOPSTRACKER_CONSUMER_SECRET
)
self.auth.set_access_token(
settings.TWOOPSTRACKER_ACCESS_TOKEN,
settings.TWOOPSTRACKER_ACCESS_TOKEN_SECRET,
)
self.api = tweepy.API(self.auth)

try:
username = self.api.verify_credentials().screen_name
logger.info("@" + username + " is authenticated")
except tweepy.errors.TweepyException as e:
logger.error(e)

def get_user(self, user, key="screen_name"):
try:
if key == "screen_name":
return self.api.get_user(screen_name=user)
elif key == "id":
return self.api.get_user(user_id=user)
except tweepy.errors.TweepyException as e:
logger.error(e)
return None
Empty file.
9 changes: 8 additions & 1 deletion twoopstracker/twoops/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from django.contrib import admin

from twoopstracker.twoops.models import Tweet, TwitterAccount
from twoopstracker.twoops.models import (
Tweet,
TwitterAccount,
TwitterAccountsList,
UserProfile,
)

admin.site.register(
[
Tweet,
TwitterAccount,
UserProfile,
TwitterAccountsList,
]
)
35 changes: 35 additions & 0 deletions twoopstracker/twoops/migrations/0003_userprofile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.8 on 2021-11-01 10:28

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("authentication", "0001_user_model"),
("twoops", "0002_twitter_account"),
]

operations = [
migrations.CreateModel(
name="UserProfile",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
primary_key=True,
serialize=False,
to="authentication.user",
),
),
],
options={
"get_latest_by": "updated_at",
"abstract": False,
},
),
]
56 changes: 56 additions & 0 deletions twoopstracker/twoops/migrations/0004_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Generated by Django 3.2.8 on 2021-11-01 10:34

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("twoops", "0003_userprofile"),
]

operations = [
migrations.CreateModel(
name="TwitterAccountsList",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"name",
models.CharField(help_text="Name of Twitter List", max_length=255),
),
(
"slug",
models.CharField(help_text="Twitter List Slug", max_length=255),
),
(
"accounts",
models.ManyToManyField(
related_name="lists", to="twoops.TwitterAccount"
),
),
(
"owner",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="twoops.userprofile",
),
),
("is_private", models.BooleanField(default=False)),
],
options={
"get_latest_by": "updated_at",
"abstract": False,
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.8 on 2021-11-03 16:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("twoops", "0004_list"),
]

operations = [
migrations.AlterField(
model_name="twitteraccount",
name="favourites_count",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="twitteraccount",
name="followers_count",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="twitteraccount",
name="friends_count",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="twitteraccount",
name="statuses_count",
field=models.IntegerField(default=0),
),
]
37 changes: 33 additions & 4 deletions twoopstracker/twoops/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -43,10 +44,10 @@ class TwitterAccount(TimestampedModelMixin):
protected = models.BooleanField(default=False)
location = models.CharField(max_length=255)
description = models.TextField()
followers_count = models.IntegerField()
friends_count = models.IntegerField()
favourites_count = models.IntegerField()
statuses_count = models.IntegerField()
followers_count = models.IntegerField(default=0)
friends_count = models.IntegerField(default=0)
favourites_count = models.IntegerField(default=0)
statuses_count = models.IntegerField(default=0)
profile_image_url = models.URLField(max_length=255)
deleted = models.BooleanField(
default=False,
Expand All @@ -55,3 +56,31 @@ class TwitterAccount(TimestampedModelMixin):

def __str__(self):
return self.screen_name


class UserProfile(TimestampedModelMixin):
"""
User Profile model
"""

user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True
)

def __str__(self):
return self.user.username


class TwitterAccountsList(TimestampedModelMixin):
"""
List model
"""

name = models.CharField(max_length=255, help_text=_("Name of Twitter List"))
slug = models.CharField(max_length=255, help_text=_("Twitter List Slug"))
owner = models.ForeignKey("UserProfile", on_delete=models.CASCADE)
accounts = models.ManyToManyField("TwitterAccount", related_name="lists")
is_private = models.BooleanField(default=False)

def __str__(self):
return self.name
27 changes: 26 additions & 1 deletion twoopstracker/twoops/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from twoopstracker.twoops.models import Tweet, TwitterAccount
from twoopstracker.twoops.models import Tweet, TwitterAccount, TwitterAccountsList


class TwitterAccountSerializer(serializers.ModelSerializer):
Expand All @@ -14,3 +14,28 @@ class Meta:
model = Tweet
fields = "__all__"
depth = 1


class TwitterAccountListSerializer(serializers.ModelSerializer):
def get_accounts(self, obj):
accounts = obj.accounts.all()
data = []
for account in accounts:
data.append(
{
"name": account.name,
"account_id": account.account_id,
"screen_name": account.screen_name,
}
)

return data

class Meta:
model = TwitterAccountsList
fields = ["id", "name", "owner", "is_private", "accounts"]

def to_representation(self, instance):
data = super().to_representation(instance)
data["accounts"] = self.get_accounts(instance)
return data
4 changes: 3 additions & 1 deletion twoopstracker/twoops/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.urls import path

from .views import TweetsView
from .views import AccountsList, SingleTwitterList, TweetsView

urlpatterns = [
path("tweets/", TweetsView.as_view(), name="tweets"),
path("lists/", AccountsList.as_view(), name="accounts_list"),
path("lists/<pk>", SingleTwitterList.as_view(), name="single_account_list"),
]

0 comments on commit 667ef8d

Please sign in to comment.