diff --git a/website/templates/connect_to_slack.html b/website/templates/connect_to_slack.html new file mode 100644 index 000000000..f87ead435 --- /dev/null +++ b/website/templates/connect_to_slack.html @@ -0,0 +1,29 @@ + + + + + + Connect to Slack + + + +
+

Connect to Slack

+
+ {% csrf_token %} +
+ + +
+ +
+ {% if messages %} +
+ {% for message in messages %} +
{{ message }}
+ {% endfor %} +
+ {% endif %} +
+ + diff --git a/website/templates/daily_checkins.html b/website/templates/daily_checkins.html new file mode 100644 index 000000000..a934e7e43 --- /dev/null +++ b/website/templates/daily_checkins.html @@ -0,0 +1,34 @@ + + + + + + Daily Check-ins + + + +
+

Daily Check-ins

+ + + + + + + + + + + {% for checkin in checkins %} + + + + + + + {% endfor %} + +
DateCheck-in TimeCheck-out TimeTotal Hours
{{ checkin.date }}{{ checkin.checkin_time }}{{ checkin.checkout_time }}{{ checkin.total_hours }}
+
+ + diff --git a/website/urls.py b/website/urls.py new file mode 100644 index 000000000..18bb253a5 --- /dev/null +++ b/website/urls.py @@ -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'), +] diff --git a/website/views.py b/website/views.py index 210007647..31eba49fc 100644 --- a/website/views.py +++ b/website/views.py @@ -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 @@ -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.") + 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