Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/to watch list #59

Merged
merged 18 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions friendo_site/friendo_site/static/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ body{
.messages ul{
list-style: none;
}


68 changes: 34 additions & 34 deletions friendo_site/friendo_site/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
<!DOCTYPE html>
{% load static %}
<html lang="en" style="width: 100%; height: 100%">
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" charset="UTF-8">
<title>Friendo Bot</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<div style="width: 100%; padding: 5px; text-align: right;">
{% if not user.is_authenticated %}
<div style="width: 100%; padding: 5px; text-align: right;">
{% if not user.is_authenticated %}

{% if request.path == "/users/login/" %}
<a href="{% url 'index' %}" type="button" class="btn btn-outline-light" >Home</a>
{% else %}
<a href="{% url 'login' %}" type="button" class="btn btn-outline-light" >Login</a>
{% endif %}

<a href="{% url 'register' %}" type="button" class="btn btn-outline-light">Sign Up</a>

{% if request.path == "/users/login/" %}
<a href="{% url 'index' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Home</a>
{% else %}
<a href="{% url 'login' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Login</a>
{% endif %}

<a href="{% url 'register' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Sign Up</a>
{% if request.path == "/users/profile/" %}
<a href="{% url 'index' %}" type="button" class="btn btn-outline-light" ">Home</a>
{% else %}
<a href="{% url 'profile' %}" type="button" class="btn btn-outline-light" ">Profile</a>
{% endif %}

{% else %}
<a href="{% url 'logout' %}" type="button" class="btn btn-outline-light" ">Logout</a>

{% if request.path == "/users/profile/" %}
<a href="{% url 'index' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Home</a>
{% else %}
<a href="{% url 'profile' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Profile</a>
{% endif %}
</div>

{% if messages %}
<div class="messages">
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}

<a href="{% url 'logout' %}" type="button" class="btn btn-sm btn-outline-light" style="width: 85px;">Logout</a>
{% block content %}
{% endblock content %}

{% endif %}
</div>

{% if messages %}
<div class="messages">
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}

{% block content %}
{% endblock content %}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</body>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</body>
</html>
39 changes: 20 additions & 19 deletions friendo_site/friendo_site/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{% extends "base.html" %}


{% block content %}
<div class="container">
<div class="row">
<div style="border-bottom: 10px solid white; border-radius: 10px; margin: 40px auto; padding: 20px; width: 400px; text-align: center">
<h1 style="color: white; margin: 0 auto; padding: 20px;">Friendo Bot</h1>
</div>
<div class="container-fluid">
<div class="row justify-content-center">
<div style="border-bottom: 10px solid white; border-radius: 10px; margin: 40px auto; padding: 20px; width: 400px; text-align: center">
<h1 style="color: white; margin: 0 auto; padding: 20px;">Friendo Bot</h1>
</div>
</div>

<div class="row justify-content-md-center">
<div class="column">
<a href="https://discord.com/api/oauth2/authorize?client_id=742897218939387925&permissions=2147352305&scope=bot" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 250px;">Invite To Your Server</a>
</div>
<div class="row justify-content-center">
<div class="column">
<a href="https://discord.com/api/oauth2/authorize?client_id=742897218939387925&permissions=2147352305&scope=bot" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 250px;">Invite To Your Server</a>
</div>
</div>

<div class="row justify-content-md-center">
<div class="column">
<a href="{% url 'graphql' %}" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 200px; margin-top: 50px">API</a>
</div>
<div class="row justify-content-center">
<div class="column">
<a href="{% url 'graphql' %}" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 200px; margin-top: 50px">API</a>
</div>
</div>

<div class="row justify-content-md-center">
<div class="column">
<a href="https://github.com/fisher60/Friendo_Bot" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 150px; margin-top: 50px">Source</a>
</div>
<div class="row justify-content-center">
<div class="column">
<a href="https://github.com/fisher60/Friendo_Bot" target="_blank" type="button" class="btn btn-lg btn-outline-light" style="width: 150px; margin-top: 50px">Source</a>
</div>

</div>
{% endblock content %}

</div>
{% endblock content %}
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)
Loading