From 9b9eb2f9cce490e3152e7f217b726ceb4f35d26f Mon Sep 17 00:00:00 2001 From: DonnieBLT <128622481+DonnieBLT@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:44:28 -0400 Subject: [PATCH] removing unsued code / comments --- website/views.py | 390 +++++------------------------------------------ 1 file changed, 34 insertions(+), 356 deletions(-) diff --git a/website/views.py b/website/views.py index a91cbaae9..15227d5cd 100644 --- a/website/views.py +++ b/website/views.py @@ -20,8 +20,6 @@ import six import stripe import tweepy - -# from django_cron import CronJobBase, Schedule from allauth.account.models import EmailAddress from allauth.account.signals import user_logged_in, user_signed_up from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter @@ -136,7 +134,6 @@ from .forms import UserProfileForm from .models import BaconToken, Contribution, Tag, UserProfile -# Load environment variables load_dotenv() @@ -170,7 +167,6 @@ def add_domain_to_company(request): domain.company = company domain.save() messages.success(request, "Organization added successfully") - # back to the domain detail page return redirect("domain", slug=domain.url) else: messages.error(request, "Organization not found in the domain") @@ -179,18 +175,15 @@ def add_domain_to_company(request): domain.company = company domain.save() messages.success(request, "Organization added successfully") - # back to the domain detail page return redirect("domain", slug=domain.url) else: return redirect("index") def check_status(request): - # Check if the status is already cached status = cache.get("service_status") if not status: - # Initialize the status dictionary status = { "bitcoin": False, "bitcoin_block": None, @@ -198,7 +191,6 @@ def check_status(request): "github": False, } - # Check Bitcoin Core Node Status bitcoin_rpc_user = os.getenv("BITCOIN_RPC_USER") bitcoin_rpc_password = os.getenv("BITCOIN_RPC_PASSWORD") bitcoin_rpc_host = os.getenv("BITCOIN_RPC_HOST", "127.0.0.1") @@ -230,7 +222,6 @@ def check_status(request): except Exception as e: print(f"SendGrid Error: {e}") - # Check GitHub Repo Access github_token = os.getenv("GITHUB_ACCESS_TOKEN") if not github_token: @@ -259,10 +250,8 @@ def check_status(request): status["github"] = False print(f"GitHub API Error: {e}") - # Cache the status for 1 minute (60 seconds) cache.set("service_status", status, timeout=60) - # Pass the status to the template return render(request, "status_page.html", {"status": status}) @@ -321,7 +310,7 @@ def image_validator(img): def is_valid_https_url(url): - validate = URLValidator(schemes=["https"]) # Only allow HTTPS URLs + validate = URLValidator(schemes=["https"]) try: validate(url) return True @@ -331,7 +320,6 @@ def is_valid_https_url(url): def rebuild_safe_url(url): parsed_url = urlparse(url) - # Rebuild the URL with scheme, netloc, and path only return urlunparse((parsed_url.scheme, parsed_url.netloc, parsed_url.path, "", "", "")) @@ -379,100 +367,6 @@ def post(self, request, *args, **kwargs): return self.render_to_response(context) -# # @cache_page(60 * 60 * 24) -# def index(request, template="index.html"): -# try: -# domains = random.sample(Domain.objects.all(), 3) -# except: -# domains = None -# try: -# if not EmailAddress.objects.get(email=request.user.email).verified: -# messages.error(request, "Please verify your email address") -# except: -# pass - -# latest_hunts_filter = request.GET.get("latest_hunts", None) - -# bug_count = Issue.objects.all().count() -# user_count = User.objects.all().count() -# hunt_count = Hunt.objects.all().count() -# domain_count = Domain.objects.all().count() - -# captcha_form = CaptchaForm() - -# wallet = None -# if request.user.is_authenticated: -# wallet, created = Wallet.objects.get_or_create(user=request.user) - -# activity_screenshots = {} -# for activity in Issue.objects.all(): -# activity_screenshots[activity] = IssueScreenshot.objects.filter(issue=activity).first() - -# top_companies = ( -# Issue.objects.values("domain__name") -# .annotate(count=Count("domain__name")) -# .order_by("-count")[:10] -# ) -# top_testers = ( -# Issue.objects.values("user__id", "user__username") -# .filter(user__isnull=False) -# .annotate(count=Count("user__username")) -# .order_by("-count")[:10] -# ) - -# if request.user.is_anonymous: -# activities = Issue.objects.exclude(Q(is_hidden=True))[0:10] -# else: -# activities = Issue.objects.exclude(Q(is_hidden=True) & ~Q(user_id=request.user.id))[0:10] - -# top_hunts = Hunt.objects.values( -# "id", -# "name", -# "url", -# "logo", -# "starts_on", -# "starts_on__day", -# "starts_on__month", -# "starts_on__year", -# "end_on", -# "end_on__day", -# "end_on__month", -# "end_on__year", -# ).annotate(total_prize=Sum("huntprize__value")) - -# if latest_hunts_filter is not None: -# top_hunts = top_hunts.filter(result_published=True).order_by("-created")[:3] -# else: -# top_hunts = top_hunts.filter(is_published=True, result_published=False).order_by( -# "-created" -# )[:3] - -# context = { -# "server_url": request.build_absolute_uri("/"), -# "activities": activities, -# "domains": domains, -# "hunts": Hunt.objects.exclude(txn_id__isnull=True)[:4], -# "leaderboard": User.objects.filter( -# points__created__month=datetime.now().month, -# points__created__year=datetime.now().year, -# ) -# .annotate(total_score=Sum("points__score")) -# .order_by("-total_score")[:10], -# "bug_count": bug_count, -# "user_count": user_count, -# "hunt_count": hunt_count, -# "domain_count": domain_count, -# "wallet": wallet, -# "captcha_form": captcha_form, -# "activity_screenshots": activity_screenshots, -# "top_companies": top_companies, -# "top_testers": top_testers, -# "top_hunts": top_hunts, -# "ended_hunts": False if latest_hunts_filter is None else True, -# } -# return render(request, template, context) - - def newhome(request, template="new_home.html"): if request.user.is_authenticated: email_record = EmailAddress.objects.filter(email=request.user.email).first() @@ -482,19 +376,16 @@ def newhome(request, template="new_home.html"): else: messages.error(request, "No email associated with your account. Please add an email.") - # Fetch and paginate issues issues_queryset = Issue.objects.exclude(Q(is_hidden=True) & ~Q(user_id=request.user.id)) paginator = Paginator(issues_queryset, 15) page_number = request.GET.get("page") page_obj = paginator.get_page(page_number) - # Prefetch related screenshots only for issues on the current page issues_with_screenshots = page_obj.object_list.prefetch_related( Prefetch("screenshots", queryset=IssueScreenshot.objects.all()) ) bugs_screenshots = {issue: issue.screenshots.all()[:3] for issue in issues_with_screenshots} - # Filter leaderboard for current month and year current_time = now() leaderboard = User.objects.filter( points__created__month=current_time.month, points__created__year=current_time.year @@ -535,7 +426,6 @@ def github_callback(request): ALLOWED_HOSTS = ["github.com"] params = urllib.parse.urlencode(request.GET) url = f"{settings.CALLBACK_URL_FOR_GITHUB}?{params}" - return safe_redirect(url, ALLOWED_HOSTS) @@ -543,7 +433,6 @@ def google_callback(request): ALLOWED_HOSTS = ["accounts.google.com"] params = urllib.parse.urlencode(request.GET) url = f"{settings.CALLBACK_URL_FOR_GOOGLE}?{params}" - return safe_redirect(url, ALLOWED_HOSTS) @@ -551,7 +440,6 @@ def facebook_callback(request): ALLOWED_HOSTS = ["www.facebook.com"] params = urllib.parse.urlencode(request.GET) url = f"{settings.CALLBACK_URL_FOR_FACEBOOK}?{params}" - return safe_redirect(url, ALLOWED_HOSTS) @@ -561,8 +449,6 @@ class FacebookLogin(SocialLoginView): @property def callback_url(self): - # use the same callback url as defined in your Facebook app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("facebook_callback")) @@ -572,8 +458,6 @@ class GoogleLogin(SocialLoginView): @property def callback_url(self): - # use the same callback url as defined in your Google app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("google_callback")) @@ -583,8 +467,6 @@ class GithubLogin(SocialLoginView): @property def callback_url(self): - # use the same callback url as defined in your GitHub app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("github_callback")) @@ -594,8 +476,6 @@ class FacebookConnect(SocialConnectView): @property def callback_url(self): - # use the same callback url as defined in your Facebook app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("facebook_callback")) @@ -605,8 +485,6 @@ class GithubConnect(SocialConnectView): @property def callback_url(self): - # use the same callback url as defined in your GitHub app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("github_callback")) @@ -616,8 +494,6 @@ class GoogleConnect(SocialConnectView): @property def callback_url(self): - # use the same callback url as defined in your Google app, this url - # must be absolute: return self.request.build_absolute_uri(reverse("google_callback")) @@ -774,7 +650,6 @@ def process_issue(self, user, obj, created, domain, tokenauth=False, score=3): print("processing process_issue for ip address: ", get_client_ip(self.request)) p = Points.objects.create(user=user, issue=obj, score=score) messages.success(self.request, "Bug added ! +" + str(score)) - # tweet feature try: auth = tweepy.Client( settings.BEARER_TOKEN, @@ -941,7 +816,6 @@ def get_initial(self): if isinstance(self.request.POST.get("file"), six.string_types): import imghdr - # Check if the base64 string is in the "data:" format data = ( "data:image/" + self.request.POST.get("type") @@ -951,18 +825,14 @@ def get_initial(self): data = data.replace(" ", "") data += "=" * ((4 - len(data) % 4) % 4) if "data:" in data and ";base64," in data: - # Break out the header from the base64 content header, data = data.split(";base64,") - # Try to decode the file. Return validation error if it fails. try: decoded_file = base64.b64decode(data) except TypeError: TypeError("invalid_image") - # Generate file name: - file_name = str(uuid.uuid4())[:12] # 12 characters are more than enough. - # Get the file name extension: + file_name = str(uuid.uuid4())[:12] extension = imghdr.what(file_name, decoded_file) extension = "jpg" if extension == "jpeg" else extension file_extension = extension @@ -982,32 +852,19 @@ def get_initial(self): initial["screenshot"] = "uploads\/" + self.request.POST.get("screenshot-hash") + ".png" return initial - # def get(self, request, *args, **kwargs): - # print("processing get for ip address: ", get_client_ip(request)) - - # captcha_form = CaptchaForm() - # return render( - # request, - # self.template_name, - # {"form": self.get_form(), "captcha_form": captcha_form}, - # ) - def post(self, request, *args, **kwargs): print("processing post for ip address: ", get_client_ip(request)) - # resolve domain url = request.POST.get("url").replace("www.", "").replace("https://", "") request.POST._mutable = True - request.POST.update(url=url) # only domain.com will be stored in db + request.POST.update(url=url) request.POST._mutable = False - # disable domain search on testing if not settings.IS_TEST: try: if settings.DOMAIN_NAME in url: print("Web site exists") - # skip domain validation check if bugreport server down elif request.POST["label"] == "7": pass @@ -1035,14 +892,6 @@ def post(self, request, *args, **kwargs): "report.html", {"form": self.get_form(), "captcha_form": captcha_form}, ) - # if not request.FILES.get("screenshots"): - # messages.error(request, "Screenshot is required") - # captcha_form = CaptchaForm(request.POST) - # return render( - # self.request, - # "report.html", - # {"form": self.get_form(), "captcha_form": captcha_form}, - # ) screenshot = request.FILES.get("screenshots") if not screenshot: @@ -1055,9 +904,8 @@ def post(self, request, *args, **kwargs): ) try: - # Attempt to open the image to validate if it's a correct format img = Image.open(screenshot) - img.verify() # Verify that it is, in fact, an image + img.verify() except (IOError, ValueError): messages.error(request, "Invalid image file.") captcha_form = CaptchaForm(request.POST) @@ -1077,7 +925,6 @@ def form_valid(self, form): reporter_ip = get_client_ip(self.request) form.instance.reporter_ip_address = reporter_ip - # implement rate limit limit = 50 if self.request.user.is_authenticated else 30 today = now().date() recent_issues_count = Issue.objects.filter( @@ -1871,34 +1718,33 @@ def get_cumulative_data(queryset, date_field="created"): return cumulative_data - # Prepare cumulative sparklines data context["sparklines_data"] = [ - get_cumulative_data(Issue.objects), # Uses "created" - get_cumulative_data(User.objects, date_field="date_joined"), # Uses "date_joined" - get_cumulative_data(Hunt.objects), # Uses "created" - get_cumulative_data(Domain.objects), # Uses "created" - get_cumulative_data(Subscription.objects), # Uses "created" - get_cumulative_data(Company.objects), # Uses "created" - get_cumulative_data(HuntPrize.objects), # Uses "created" - get_cumulative_data(IssueScreenshot.objects), # Uses "created" - get_cumulative_data(Winner.objects), # Uses "created" - get_cumulative_data(Points.objects), # Uses "created" - get_cumulative_data(InviteFriend.objects), # Uses "created" - get_cumulative_data(UserProfile.objects), # Uses "created" - get_cumulative_data(IP.objects), # Uses "created" - get_cumulative_data(CompanyAdmin.objects), # Uses "created" - get_cumulative_data(Transaction.objects), # Uses "created" - get_cumulative_data(Payment.objects), # Uses "created" - get_cumulative_data(ContributorStats.objects), # Uses "created" - get_cumulative_data(Monitor.objects), # Uses "created" - get_cumulative_data(Bid.objects), # Uses "created" - get_cumulative_data(ChatBotLog.objects), # Uses "created" - get_cumulative_data(Suggestion.objects), # Uses "created" - get_cumulative_data(SuggestionVotes.objects), # Uses "created" - get_cumulative_data(Contributor.objects), # Uses "created" - get_cumulative_data(Project.objects), # Uses "created" - get_cumulative_data(Contribution.objects), # Uses "created" - get_cumulative_data(BaconToken.objects), # Uses "created" + get_cumulative_data(Issue.objects), + get_cumulative_data(User.objects, date_field="date_joined"), + get_cumulative_data(Hunt.objects), + get_cumulative_data(Domain.objects), + get_cumulative_data(Subscription.objects), + get_cumulative_data(Company.objects), + get_cumulative_data(HuntPrize.objects), + get_cumulative_data(IssueScreenshot.objects), + get_cumulative_data(Winner.objects), + get_cumulative_data(Points.objects), + get_cumulative_data(InviteFriend.objects), + get_cumulative_data(UserProfile.objects), + get_cumulative_data(IP.objects), + get_cumulative_data(CompanyAdmin.objects), + get_cumulative_data(Transaction.objects), + get_cumulative_data(Payment.objects), + get_cumulative_data(ContributorStats.objects), + get_cumulative_data(Monitor.objects), + get_cumulative_data(Bid.objects), + get_cumulative_data(ChatBotLog.objects), + get_cumulative_data(Suggestion.objects), + get_cumulative_data(SuggestionVotes.objects), + get_cumulative_data(Contributor.objects), + get_cumulative_data(Project.objects), + get_cumulative_data(Contribution.objects), + get_cumulative_data(BaconToken.objects), ] return context @@ -2008,18 +1854,7 @@ def get_context_data(self, *args, **kwargs): class LeaderboardBase: - """ - get: - 1) ?monthly=true will give list of winners for current month - 2) ?year=2022 will give list of winner of every month from month 1-12 else None - - """ - def get_leaderboard(self, month=None, year=None, api=False): - """ - all user scores for specified month and year - """ - data = User.objects if year and not month: @@ -2381,8 +2216,8 @@ def get(self, request, *args, **kwargs): ) self.object.save() except Exception as e: + print(e) pass # pass this temporarly to avoid error - # print(e) # messages.error(self.request, "That issue was not found 2." + str(e)) # ipdetails.save() # self.object.views = (self.object.views or 0) + 1 @@ -3755,7 +3590,6 @@ def submit_bug(request, pk, template="hunt_submittion.html"): if isinstance(request.POST.get("file"), six.string_types): import imghdr - # Check if the base64 string is in the "data:" format data = ( "data:image/" + request.POST.get("type") @@ -3915,23 +3749,18 @@ def handler500(request, exception=None): def users_view(request, *args, **kwargs): context = {} - # Get all tags related to all user profiles context["user_related_tags"] = Tag.objects.filter(userprofile__isnull=False).distinct() - # Get all tags in the system context["tags"] = Tag.objects.all() - # Check if a specific tag is being requested tag_name = request.GET.get("tag") if tag_name: - # Check if the requested tag exists in user_related_tags if context["user_related_tags"].filter(name=tag_name).exists(): context["tag"] = tag_name context["users"] = UserProfile.objects.filter(tags__name=tag_name) else: context["users"] = UserProfile.objects.none() # No users if the tag isn't found else: - # Default filter: Show users with the tag "BLT Contributor" context["tag"] = "BLT Contributors" context["users"] = UserProfile.objects.filter(tags__name="BLT Contributors") @@ -3976,7 +3805,6 @@ def get_bch_balance(address): print(f"An error occurred: {e}") return None - # Example BCH address bch_address = "bitcoincash:qr5yccf7j4dpjekyz3vpawgaarl352n7yv5d5mtzzc" balance = get_bch_balance(bch_address) @@ -3990,15 +3818,11 @@ def safe_redirect(request: HttpRequest): http_referer = request.META.get("HTTP_REFERER") if http_referer: referer_url = urlparse(http_referer) - # Check if the referer URL's host is the same as the site's host if referer_url.netloc == request.get_host(): - # Build a 'safe' version of the referer URL (without query string or fragment) safe_url = urlunparse( (referer_url.scheme, referer_url.netloc, referer_url.path, "", "", "") ) return redirect(safe_url) - # Redirect to the fallback path if 'HTTP_REFERER' is not provided or is not safe - # Build the fallback URL using the request's scheme and host fallback_url = f"{request.scheme}://{request.get_host()}/" return redirect(fallback_url) @@ -4263,9 +4087,6 @@ def get_context_data(self, **kwargs): def create_github_issue(request, id): issue = get_object_or_404(Issue, id=id) screenshot_all = IssueScreenshot.objects.filter(issue=issue) - # referer = request.META.get("HTTP_REFERER") - # if not referer: - # return HttpResponseForbidden() if not os.environ.get("GITHUB_ACCESS_TOKEN"): return JsonResponse({"status": "Failed", "status_reason": "GitHub Access Token is missing"}) if issue.github_url: @@ -4372,15 +4193,13 @@ def handle_user_signup(request, user, **kwargs): def reward_sender_with_points(sender): - # Create or update points for the sender points, created = Points.objects.get_or_create(user=sender, defaults={"score": 0}) - points.score += 2 # Reward 2 points for each successful referral signup + points.score += 2 points.save() def referral_signup(request): referral_token = request.GET.get("ref") - # check the referral token is present on invitefriend model or not and if present then set the referral token in the session if referral_token: try: invite = InviteFriend.objects.get(referral_code=referral_token) @@ -4392,7 +4211,6 @@ def referral_signup(request): def invite_friend(request): - # check if the user is authenticated or not if not request.user.is_authenticated: return redirect("account_login") current_site = get_current_site(request) @@ -4439,120 +4257,6 @@ def trademark_detailview(request, slug): return render(request, "trademark_detailview.html", context) -# class CreateIssue(CronJobBase): -# RUN_EVERY_MINS = 1 - -# schedule = Schedule(run_every_mins=RUN_EVERY_MINS) -# code = "blt.create_issue" # a unique code - -# def do(self): -# from django.conf import settings -# import imaplib - -# mail = imaplib.IMAP4_SSL("imap.gmail.com", 993) -# error = False -# mail.login(settings.REPORT_EMAIL, settings.REPORT_EMAIL_PASSWORD) -# mail.list() -# # Out: list of "folders" aka labels in gmail. -# mail.select("inbox") # connect to inbox. -# typ, data = mail.search(None, "ALL", "UNSEEN") -# import email - - -# for num in data[0].split(): -# image = False -# screenshot_base64 = "" -# url = "" -# label = "" -# token = "None" -# typ, data = mail.fetch(num, "(RFC822)") -# raw_email = (data[0][1]).decode("utf-8") -# email_message = email.message_from_string(raw_email) -# maintype = email_message.get_content_maintype() -# error = False -# for part in email_message.walk(): -# if part.get_content_type() == "text/plain": # ignore attachments/html -# body = part.get_payload(decode=True) -# body_text = body.decode("utf-8") -# words = body_text.split() -# flag_word = False -# for word in words: -# if word.lower() == ":": -# continue -# if word.lower() == "url": -# continue -# if word.lower() == "type": -# flag_word = True -# continue -# if not flag_word: -# url = word -# continue -# if flag_word: -# label = word -# if part.get_content_maintype() == "multipart": -# continue -# if part.get("Content-Disposition") is None: -# continue -# image = True -# screenshot_base64 = part -# sender = email_message["From"].split()[-1] -# address = re.sub(r"[<>]", "", sender) -# for user in User.objects.all(): -# if user.email == address: -# token = Token.objects.get(user_id=user.id).key -# break -# if label.lower() == "general": -# label = 0 -# elif label.lower() == "number error": -# label = 1 -# elif label.lower() == "functional": -# label = 2 -# elif label.lower() == "performance": -# label = 3 -# elif label.lower() == "security": -# label = 4 -# elif label.lower() == "typo": -# label = 5 -# elif label.lower() == "design": -# label = 6 -# else: -# error = True -# if token == "None": -# error = "TokenTrue" -# if not image: -# error = True -# if error: -# send_mail( -# "Error In Your Report", -# "There was something wrong with the mail you sent regarding the issue to be created. Please check the content and try again later !", -# settings.EMAIL_TO_STRING, -# [address], -# fail_silently=False, -# ) -# elif error == "TokenTrue": -# send_mail( -# "You are not a user of " + settings.PROJECT_NAME, -# "You are not a Registered user at " + settings.PROJECT_NAME + " .Please first Signup at " + settings.PROJECT_NAME + " and Try Again Later ! .", -# settings.EMAIL_TO_STRING, -# [address], -# fail_silently=False, -# ) -# else: -# data = { -# "url": url, -# "description": email_message["Subject"], -# "file": str(screenshot_base64.get_payload(decode=False)), -# "token": token, -# "label": label, -# "type": "jpg", -# } -# headers = {"Content-Type": "application/x-www-form-urlencoded"} -# requests.post( -# "https://" + settings.FQDN + "/api/v1/createissues/", -# data=json.dumps(data), -# headers=headers, -# ) -# mail.logout() def update_bch_address(request): if request.method == "POST": selected_crypto = request.POST.get("selected_crypto") @@ -4763,17 +4467,13 @@ def submit_pr(request): return render(request, "submit_pr.html") -# Global variable to store the vector store vector_store = None - -# Define the daily request limit as a variable DAILY_REQUEST_LIMIT = 10 @api_view(["POST"]) def chatbot_conversation(request): try: - # Rate Limit Check today = datetime.now(timezone.utc).date() rate_limit_key = f"global_daily_requests_{today}" request_count = cache.get(rate_limit_key, 0) @@ -4791,7 +4491,6 @@ def chatbot_conversation(request): ChatBotLog.objects.create(question=question, answer="Error: Invalid API Key") return Response({"error": "Invalid API Key"}, status=status.HTTP_400_BAD_REQUEST) - # Apply validation for question if not question or not isinstance(question, str): ChatBotLog.objects.create(question=question, answer="Error: Invalid question") return Response({"error": "Invalid question"}, status=status.HTTP_400_BAD_REQUEST) @@ -4824,9 +4523,7 @@ def chatbot_conversation(request): status=status.HTTP_500_INTERNAL_SERVER_ERROR, ) - # Handle the "exit" command if question.lower() == "exit": - # if buffer is present in the session then delete it if "buffer" in request.session: del request.session["buffer"] return Response({"answer": "Conversation memory cleared."}, status=status.HTTP_200_OK) @@ -4841,11 +4538,9 @@ def chatbot_conversation(request): error_message = f"Error: {str(e)}" ChatBotLog.objects.create(question=question, answer=error_message) return Response({"error": error_message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - # Increment the request count cache.set(rate_limit_key, request_count + 1, timeout=86400) # Timeout set to one day request.session["buffer"] = memory.buffer - # Log the conversation ChatBotLog.objects.create(question=question, answer=response["answer"]) return Response({"answer": response["answer"]}, status=status.HTTP_200_OK) @@ -5021,44 +4716,34 @@ def view_suggestions(request): def sizzle(request): - # Check if the user is authenticated; if not, redirect to the home page with a message print(request.user) if not request.user.is_authenticated: messages.error(request, "Please login to access the Sizzle page.") return redirect("index") - # Initialize the variable to store Sizzle tracking data sizzle_data = None - # Fetch the latest TimeLog entry for the current user last_data = TimeLog.objects.filter(user=request.user).order_by("-created").first() if last_data: - # Fetch all TimeLog entries created on the same date as the last_data entry all_data = TimeLog.objects.filter( user=request.user, created__date=last_data.created.date() ).order_by("created") - # Calculate the total duration for all the entries on the same date total_duration = sum((entry.duration for entry in all_data if entry.duration), timedelta()) - # Format total duration to minutes and seconds total_duration_seconds = total_duration.total_seconds() formatted_duration = ( f"{int(total_duration_seconds // 60)} min {int(total_duration_seconds % 60)} sec" ) - # Get the first entry's GitHub issue URL and extract the repository path and issue number github_issue_url = all_data.first().github_issue_url - # Fetch the issue title from GitHub API issue_title = get_github_issue_title(github_issue_url) - # Format start time and date to human-readable format start_time = all_data.first().start_time.strftime("%I:%M %p") - date = last_data.created.strftime("%d %B %Y") # Example: '14 August 2024' + date = last_data.created.strftime("%d %B %Y") - # Prepare sizzle_data with human-readable format sizzle_data = { "issue_title": issue_title, "duration": formatted_duration, @@ -5089,37 +4774,30 @@ def TimeLogListAPIView(request): if not start_date or not end_date: return JsonResponse({"error": "Invalid date format."}, status=status.HTTP_400_BAD_REQUEST) - # Fetch the time logs within the specified date range for the authenticated user time_logs = TimeLog.objects.filter( user=request.user, created__range=[start_date, end_date] ).order_by("created") - # Group time logs by date grouped_logs = defaultdict(list) for log in time_logs: date_str = log.created.strftime("%Y-%m-%d") grouped_logs[date_str].append(log) - # Prepare the final structured response response_data = [] for date, logs in grouped_logs.items(): first_log = logs[0] total_duration = sum((log.duration for log in logs if log.duration), timedelta()) - # Format total duration to minutes and seconds total_duration_seconds = total_duration.total_seconds() formatted_duration = ( f"{int(total_duration_seconds // 60)} min {int(total_duration_seconds % 60)} sec" ) - # Fetch the issue title using GitHub API (for the first log of the day) issue_title = get_github_issue_title(first_log.github_issue_url) - # Format start time and date to human-readable format start_time = first_log.start_time.strftime("%I:%M %p") - formatted_date = first_log.created.strftime("%d %B %Y") # Example: '14 August 2024' + formatted_date = first_log.created.strftime("%d %B %Y") - # Prepare the day's data day_data = { "issue_title": issue_title, "duration": formatted_duration,