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

Add daily check-ins and Slack integration #2832

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions website/templates/connect_to_slack.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connect to Slack</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<div class="container">
<h1>Connect to Slack</h1>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="slack_token">Slack Token:</label>
<input type="text" id="slack_token" name="slack_token" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% if messages %}
<div class="messages">
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">{{ message }}</div>
{% endfor %}
</div>
{% endif %}
</div>
</body>
</html>
34 changes: 34 additions & 0 deletions website/templates/daily_checkins.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Daily Check-ins</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<div class="container">
<h1>Daily Check-ins</h1>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Check-in Time</th>
<th>Check-out Time</th>
<th>Total Hours</th>
</tr>
</thead>
<tbody>
{% for checkin in checkins %}
<tr>
<td>{{ checkin.date }}</td>
<td>{{ checkin.checkin_time }}</td>
<td>{{ checkin.checkout_time }}</td>
<td>{{ checkin.total_hours }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
7 changes: 7 additions & 0 deletions website/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from . import views

urlpatterns = [
path('daily_checkins/', views.daily_checkins, name='daily_checkins'),
path('connect_to_slack/', views.connect_to_slack, name='connect_to_slack'),
]
46 changes: 46 additions & 0 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from sendgrid import SendGridAPIClient
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

from blt import settings
from comments.models import Comment
Expand Down Expand Up @@ -2528,3 +2530,47 @@ def TimeLogListView(request):
"sizzle/time_logs.html",
{"time_logs": time_logs, "active_time_log": active_time_log, "token": token.key},
)


@login_required
def daily_checkins(request):
user = request.user
if not user.is_authenticated:
return redirect("login")

checkins = TimeLog.objects.filter(user=user).order_by("-start_time")
return render(request, "daily_checkins.html", {"checkins": checkins})


@login_required
def connect_to_slack(request):
user = request.user
if not user.is_authenticated:
return redirect("login")

if request.method == "POST":
slack_token = request.POST.get("slack_token")
if slack_token:
user_profile = UserProfile.objects.get(user=user)
user_profile.slack_token = slack_token
user_profile.save()
messages.success(request, "Slack token saved successfully.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can redirect here to avoid resubmission when the user refreshes the page.

else:
messages.error(request, "Please provide a valid Slack token.")

return render(request, "connect_to_slack.html")


def send_slack_message(user, message):
user_profile = UserProfile.objects.get(user=user)
slack_token = user_profile.slack_token
if not slack_token:
return False

client = WebClient(token=slack_token)
try:
response = client.chat_postMessage(channel="#general", text=message)
return response["ok"]
except SlackApiError as e:
print(f"Error sending message to Slack: {e.response['error']}")
return False
Loading