Skip to content

Commit

Permalink
Merge pull request #59 from fisher60/feature/to-watch-list
Browse files Browse the repository at this point in the history
Feature/to watch list
  • Loading branch information
fisher60 authored Mar 9, 2022
2 parents d56ff91 + 5ac103c commit 27b17ab
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 17 deletions.
5 changes: 5 additions & 0 deletions friendo_site/friendo_site/graphql/schemas/mutation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ type Mutation {
modify_user(data: ModifyUserInput!): User
get_guild(data: GuildInput!): Guild
modify_guild(data: GuildInput!): Guild
get_user_watchlists(data: GetUserWatchListsInput!): [WatchList]
create_watchlist(data: CreateUserWatchListInput!): WatchList
modify_user_watchlist(data: ModifyUserWatchListInput!): WatchList
get_watchlist_from_id(data: GetWatchListFromIdInput!): WatchList
delete_watchlist_from_id(data: GetWatchListFromIdInput!): Boolean
}
44 changes: 41 additions & 3 deletions friendo_site/friendo_site/graphql/schemas/users.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ type User{
username: String!
bot_admin: Boolean!
notes: [String!]
discord_id: String
discord_id: BigInt
timezone_name: String
watch_lists: [WatchList]
}

input LoginInput {
Expand All @@ -14,11 +15,11 @@ input LoginInput {

input GetUserInput {
username: String
discord_id: String
discord_id: BigInt
}

input ModifyUserInput {
discord_id: String!
discord_id: BigInt!
timezone_name: String
}

Expand All @@ -27,3 +28,40 @@ type LoginPayload {
error: String
token: String
}

type WatchListTitle {
id: ID
name: String!
WatchList: WatchList!
}

type WatchList {
id: ID!
name: String!
titles: [WatchListTitle]
owners: [User!]!
}

input GetUserWatchListsInput {
discord_id: BigInt!
}

input GetWatchListFromIdInput {
watch_list_id: Int!
}

input CreateUserWatchListInput {
watchlist_name: String!
owner_discord_id: BigInt!
}

input ModifyUserWatchListInput {
watch_list_id: ID!
change_name: String
remove_user_discord_id: BigInt
}

input RemoveWatchListTitleInput {
watch_list: GetWatchListFromIdInput!
title_id: ID!
}
4 changes: 2 additions & 2 deletions friendo_site/friendo_site/graphql/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .query import query
from .users import user
from .users import user_type, watchlist_object
from .mutation import mutation

types = [query, mutation, user]
types = [query, mutation, user_type, watchlist_object]
16 changes: 15 additions & 1 deletion friendo_site/friendo_site/graphql/types/mutation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from ariadne import MutationType
from .users import resolve_login, get_user, modify_user
from .users import (
resolve_login,
get_user,
modify_user,
get_user_watchlists,
modify_user_watchlist,
create_user_watchlist,
get_user_watchlist_from_id,
delete_user_watchlist_from_id,
)
from .guilds import get_guild, modify_guild

mutation = MutationType()
Expand All @@ -8,3 +17,8 @@
mutation.set_field("get_guild", get_guild)
mutation.set_field("modify_guild", modify_guild)
mutation.set_field("modify_user", modify_user)
mutation.set_field("get_user_watchlists", get_user_watchlists)
mutation.set_field("modify_user_watchlist", modify_user_watchlist)
mutation.set_field("get_watchlist_from_id", get_user_watchlist_from_id)
mutation.set_field("delete_watchlist_from_id", delete_user_watchlist_from_id)
mutation.set_field("create_watchlist", create_user_watchlist)
2 changes: 1 addition & 1 deletion friendo_site/friendo_site/graphql/types/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

@query.field("allUsers")
@token_required
def resolve_all_users(root, info):
def resolve_all_users(*_):
return User.objects.all()
66 changes: 64 additions & 2 deletions friendo_site/friendo_site/graphql/types/users.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from ariadne import ObjectType
from django.contrib.auth import authenticate, login
from friendo_site.users.models import User, token_required
from friendo_site.users.models import User, token_required, WatchList

user = ObjectType("User")
user_type = ObjectType("User")

watchlist_object = ObjectType("WatchList")


def generate_user_auth_token(user=None):
Expand All @@ -11,6 +13,66 @@ def generate_user_auth_token(user=None):
return user.generate_token()


@watchlist_object.field("owners")
def resolve_watchlist_owners_field(obj, _):
return obj.owners.all()


@watchlist_object.field("titles")
def resolve_watchlist_titles_field(obj, _):
return obj.watchlisttitle_set.all()


@token_required
def get_user_watchlist_from_id(_, info, data):
return WatchList.objects.get(id=int(data.get("watch_list_id")))


@token_required
def delete_user_watchlist_from_id(_, info, data) -> bool:
"""
Attempt to get and delete a WatchList entry.
Returns whether the number of deleted entries is greater than 0.
"""
deleted_objects = WatchList.objects.get(id=int(data.get("watch_list_id"))).delete()
return deleted_objects[0] > 0


@token_required
def get_user_watchlists(_, info, data):
if discord_id := data.get("discord_id"):
return User.objects.get(discord_id=discord_id).watchlist_set.all()
else:
raise KeyError("discord_id missing or invalid.")


@token_required
def create_user_watchlist(_, info, data):
watchlist_name = data.get("watchlist_name")
owner_discord_id = data.get("owner_discord_id")

owner = User.objects.get(discord_id=owner_discord_id)
new_watchlist = WatchList.objects.create(name=watchlist_name)
new_watchlist.save()
new_watchlist.owners.add(owner)

return new_watchlist


@token_required
def modify_user_watchlist(_, info, data):
watchlist_id = data.get("watch_list_id")
this_watch_list = WatchList.objects.get(id=watchlist_id)
if change_name := data.get("change_name"):
this_watch_list.name = change_name
if remove_user := data.get("remove_user_discord_id"):
this_watch_list.owners.remove(User.objects.get(discord_id=remove_user))
this_watch_list.save()

return this_watch_list


@token_required
def get_user(_, info, data):
if data["discord_id"]:
Expand Down
4 changes: 3 additions & 1 deletion friendo_site/friendo_site/users/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, Note
from .models import Note, User, WatchList, WatchListTitle


class CustomUserAdmin(UserAdmin):
Expand All @@ -23,3 +23,5 @@ class CustomUserAdmin(UserAdmin):

admin.site.register(User, CustomUserAdmin)
admin.site.register(Note)
admin.site.register(WatchList)
admin.site.register(WatchListTitle)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 3.2.5 on 2021-07-13 01:52

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


class Migration(migrations.Migration):

dependencies = [
("users", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="WatchList",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50)),
("owners", models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
],
),
migrations.AlterField(
model_name="currency",
name="amount",
field=models.IntegerField(default=1),
),
migrations.CreateModel(
name="WatchListTitle",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=40)),
(
"watch_list",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="users.watchlist",
),
),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2022-03-09 15:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0002_auto_20210713_0152'),
]

operations = [
migrations.AlterField(
model_name='watchlist',
name='name',
field=models.CharField(max_length=50, unique=True),
),
]
22 changes: 21 additions & 1 deletion friendo_site/friendo_site/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ def generate_token(self):
return AuthToken(user=self)


class WatchList(models.Model):
name = models.CharField(max_length=50, unique=True)
owners = models.ManyToManyField(User)

def __str__(self):
return self.name[:15]


class WatchListTitle(models.Model):
name = models.CharField(max_length=40)
watch_list = models.ForeignKey(WatchList, on_delete=models.CASCADE)

def __str__(self):
return f"{str(self.watch_list)} || {self.name[:15]}"

def save(self, *args, **kwargs):
self.name = self.name[:40]
super().save(*args, **kwargs)


class Note(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.CharField(max_length=50)
Expand All @@ -51,7 +71,7 @@ def __str__(self):

class Currency(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
amount = models.IntegerField(default=97)
amount = models.IntegerField(default=1)

def __str__(self):
return "Chad Bucks"
Expand Down
8 changes: 4 additions & 4 deletions friendo_site/friendo_site/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def discord_login_redirect(request: HttpRequest):
user_data = exchange_code(code)

try:
user = request.user
user = request.user_type
user.discord_id = int(user_data.get("id"))
user.discord_username = user_data.get("username")
user.discord_discriminator = user_data.get("discriminator")
Expand Down Expand Up @@ -88,7 +88,7 @@ def exchange_code(code: str) -> dict:


def register(request):
if not request.user.is_authenticated:
if not request.user_type.is_authenticated:
if request.method == "POST":
form = UserRegisterForm(request.POST)

Expand All @@ -105,7 +105,7 @@ def register(request):


def login_view(request):
if not request.user.is_authenticated:
if not request.user_type.is_authenticated:
username = request.POST.get("username", None)
password = request.POST.get("password", None)
user = authenticate(request, username=username, password=password)
Expand All @@ -123,7 +123,7 @@ def profile(request):


def logout_view(request):
if request.user.is_authenticated:
if request.user_type.is_authenticated:
logout(request)
messages.success(request, "You have been logged out.")
return redirect("index")
5 changes: 3 additions & 2 deletions friendo_site/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'friendo_site.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "friendo_site.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -17,5 +17,6 @@ def main():
) from exc
execute_from_command_line(sys.argv)

if __name__ == '__main__':

if __name__ == "__main__":
main()

0 comments on commit 27b17ab

Please sign in to comment.