From 1d85ba41774610e9c125439900dca148ecd8d96e Mon Sep 17 00:00:00 2001 From: Ishaan Mittal Date: Sat, 17 Aug 2024 17:50:36 +0530 Subject: [PATCH] fix flake --- .flake8 | 7 ++- home/admin.py | 78 ++++++++++++------------------- home/apps.py | 3 -- home/forms.py | 1 - home/models/Semesters/autumn22.py | 1 - home/models/Semesters/autumn23.py | 1 - home/models/Semesters/spring23.py | 1 - home/models/__init__.py | 4 +- home/models/students.py | 2 - home/resources.py | 12 +++-- home/signals.py | 16 ++++--- home/tests.py | 2 - home/urls.py | 4 +- home/utils/django_email_server.py | 2 +- home/utils/get_rebate_bills.py | 3 -- home/utils/rebate_bills_saver.py | 10 +--- home/views.py | 33 +++++-------- pyproject.toml | 1 + 18 files changed, 71 insertions(+), 110 deletions(-) diff --git a/.flake8 b/.flake8 index dec2abd..4e40037 100644 --- a/.flake8 +++ b/.flake8 @@ -1,3 +1,8 @@ [flake8] max-line-length = 88 -ignore = E,F,W +# Remove F811 from admin.py +ignore = E501, E722, W503, F811 +per-file-ignores = + home/models/students.py: E402 + home/models/admin.py: F811 + home/models/__init__.py: F401 diff --git a/home/admin.py b/home/admin.py index 90340f1..0205e17 100644 --- a/home/admin.py +++ b/home/admin.py @@ -766,55 +766,37 @@ def clean(self, request, queryset): """ Clean testing period data """ + attributes_to_check = [ + ("period1_short", 0), + ("period1_long", 0), + ("period2_short", 0), + ("period2_long", 0), + ("period3_short", 0), + ("period3_long", 0), + ("period4_short", 0), + ("period4_long", 0), + ("period5_short", 0), + ("period5_long", 0), + ("period6_short", 0), + ("period6_long", 0), + ("period1_high_tea", True), + ("period2_high_tea", True), + ("period3_high_tea", True), + ("period4_high_tea", True), + ("period5_high_tea", True), + ("period6_high_tea", True), + ("period1_bill", 0), + ("period2_bill", 0), + ("period3_bill", 0), + ("period4_bill", 0), + ("period5_bill", 0), + ("period6_bill", 0), + ] + for obj in queryset: - if obj.period1_short == None: - obj.period1_short = 0 - if obj.period1_long == None: - obj.period1_long = 0 - if obj.period2_short == None: - obj.period2_short = 0 - if obj.period2_long == None: - obj.period2_long = 0 - if obj.period3_short == None: - obj.period3_short = 0 - if obj.period3_long == None: - obj.period3_long = 0 - if obj.period4_short == None: - obj.period4_short = 0 - if obj.period4_long == None: - obj.period4_long = 0 - if obj.period5_short == None: - obj.period5_short = 0 - if obj.period5_long == None: - obj.period5_long = 0 - if obj.period6_short == None: - obj.period6_short = 0 - if obj.period6_long == None: - obj.period6_long = 0 - if obj.period1_high_tea == None: - obj.period1_high_tea = True - if obj.period2_high_tea == None: - obj.period2_high_tea = True - if obj.period3_high_tea == None: - obj.period3_high_tea = True - if obj.period4_high_tea == None: - obj.period4_high_tea = True - if obj.period5_high_tea == None: - obj.period5_high_tea = True - if obj.period6_high_tea == None: - obj.period6_high_tea = True - if obj.period1_bill == None: - obj.period1_bill = 0 - if obj.period2_bill == None: - obj.period2_bill = 0 - if obj.period3_bill == None: - obj.period3_bill = 0 - if obj.period4_bill == None: - obj.period4_bill = 0 - if obj.period5_bill == None: - obj.period5_bill = 0 - if obj.period6_bill == None: - obj.period6_bill = 0 + for attr, default_value in attributes_to_check: + if getattr(obj, attr) is None: + setattr(obj, attr, default_value) obj.save() def export_as_csv(self, request, queryset): diff --git a/home/apps.py b/home/apps.py index bd81de7..2fb3345 100644 --- a/home/apps.py +++ b/home/apps.py @@ -13,9 +13,6 @@ class HomeConfig(AppConfig): def ready(self): import socket - import sys - - import home.signals # bind to port 47200, then check for it for every worker try: diff --git a/home/forms.py b/home/forms.py index 7e5e6e2..979ae24 100644 --- a/home/forms.py +++ b/home/forms.py @@ -1,5 +1,4 @@ from allauth.account.forms import SignupForm -from allauth.socialaccount.forms import SignupForm as SocialSignupForm from django import forms from django.utils.translation import gettext_lazy as _ diff --git a/home/models/Semesters/autumn22.py b/home/models/Semesters/autumn22.py index f9e94e1..1ac6fad 100644 --- a/home/models/Semesters/autumn22.py +++ b/home/models/Semesters/autumn22.py @@ -1,5 +1,4 @@ from django.db import models -from django.utils.timezone import now from django.utils.translation import gettext as _ from ..caterer import Caterer diff --git a/home/models/Semesters/autumn23.py b/home/models/Semesters/autumn23.py index 93b4d5c..15a7cf8 100644 --- a/home/models/Semesters/autumn23.py +++ b/home/models/Semesters/autumn23.py @@ -1,5 +1,4 @@ from django.db import models -from django.utils.timezone import now from django.utils.translation import gettext as _ from ..caterer import Caterer diff --git a/home/models/Semesters/spring23.py b/home/models/Semesters/spring23.py index f754c40..51c0006 100644 --- a/home/models/Semesters/spring23.py +++ b/home/models/Semesters/spring23.py @@ -1,5 +1,4 @@ from django.db import models -from django.utils.timezone import now from django.utils.translation import gettext as _ from ..caterer import Caterer diff --git a/home/models/__init__.py b/home/models/__init__.py index 9cb17ef..76942a0 100644 --- a/home/models/__init__.py +++ b/home/models/__init__.py @@ -2,8 +2,6 @@ The `__init__.py` file is used to mark a directory as a Python package, making it possible to import modules and sub-packages from that directory """ -from .allocation import Allocation, Period, Semester -from .bills import CatererBills, StudentBills from .cafeteria import Cafeteria from .caterer import Caterer from .contacts import Contact @@ -40,3 +38,5 @@ TodayRebate, UnregisteredStudent, ) +from .allocation import Allocation, Period, Semester +from .bills import CatererBills, StudentBills diff --git a/home/models/students.py b/home/models/students.py index ba78ab6..079308e 100644 --- a/home/models/students.py +++ b/home/models/students.py @@ -1,5 +1,3 @@ -import datetime - from django.db import models from django.utils.timezone import now from django.utils.translation import gettext as _ diff --git a/home/resources.py b/home/resources.py index a758426..47a41ac 100644 --- a/home/resources.py +++ b/home/resources.py @@ -1,13 +1,11 @@ +import logging + from import_export import fields, resources -from import_export.widgets import ForeignKeyWidget from .models import ( Allocation, - AllocationAutumn22, AllocationSpring23, CatererBills, - CatererBillsAutumn22, - CatererBillsSpring23, Fee, LongRebate, Period, @@ -19,7 +17,6 @@ StudentBills, UnregisteredStudent, ) -from .utils.rebate_checker import count """ File-name: resources.py @@ -32,6 +29,9 @@ handle importing and exporting data. """ +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + class StudentResource(resources.ModelResource): def skip_row(self, instance, original, row, import_validation_errors): @@ -505,6 +505,7 @@ def dehydrate_allocation1(self, obj): high_tea = "No High Tea" return str(allocation.caterer_name) + " " + high_tea except Exception as e: + logger.error(e) return "Not yet allocated" def dehydrate_allocation2(self, obj): @@ -772,6 +773,7 @@ def dehydrate_allocation1(self, obj): high_tea = "No High Tea" return str(allocation.caterer_name) + " " + high_tea except Exception as e: + logger.error(e) return "Not yet allocated" def dehydrate_allocation2(self, obj): diff --git a/home/signals.py b/home/signals.py index 362f686..829dea2 100644 --- a/home/signals.py +++ b/home/signals.py @@ -1,5 +1,3 @@ -from datetime import timedelta - from django.db.models.signals import post_save, pre_save from django.dispatch import receiver @@ -74,7 +72,7 @@ def update_short_bill(sender, instance, **kwargs): end_date = instance.end_date days = count(start_date, end_date) print(old_instance.approved, instance.approved) - if instance.approved == True and days > 0: + if instance.approved is True and days > 0: save_short_bill( email, allocation.period, @@ -128,13 +126,17 @@ def update_long_bill(sender, instance, **kwargs): email, instance.start_date, instance.end_date ) left_start_date = [ - period for period, days in days_per_period if type(period) == type(days) + period + for period, days in days_per_period + if isinstance(period, type(days)) ] left_end_date = [ - days for period, days in days_per_period if type(period) == type(days) + days + for period, days in days_per_period + if isinstance(period, type(days)) ] - if instance.approved == True: + if instance.approved is True: save_long_bill(email, days_per_period, 1) long_rebate_mail( instance.start_date, @@ -145,7 +147,7 @@ def update_long_bill(sender, instance, **kwargs): left_end_date, instance.reason, ) - elif instance.approved == False: + elif instance.approved is False: save_long_bill(email, days_per_period, -1) long_rebate_mail( instance.start_date, diff --git a/home/tests.py b/home/tests.py index 7ce503c..a39b155 100644 --- a/home/tests.py +++ b/home/tests.py @@ -1,3 +1 @@ -from django.test import TestCase - # Create your tests here. diff --git a/home/urls.py b/home/urls.py index 4628729..96fc251 100644 --- a/home/urls.py +++ b/home/urls.py @@ -17,11 +17,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from django.contrib import admin -from django.urls import include, path, re_path as url +from django.urls import path from . import views -from .views import * urlpatterns = [ path("", views.home, name="home"), diff --git a/home/utils/django_email_server.py b/home/utils/django_email_server.py index 646f386..ffccbf7 100644 --- a/home/utils/django_email_server.py +++ b/home/utils/django_email_server.py @@ -95,7 +95,7 @@ def long_rebate_mail( added="removed from", reason="", ) - if left_start_date != None: + if left_start_date is not None: message += left_message.format( left_start_date=left_start_date, left_end_date=left_end_date ) diff --git a/home/utils/get_rebate_bills.py b/home/utils/get_rebate_bills.py index 2ee4b02..4406eee 100644 --- a/home/utils/get_rebate_bills.py +++ b/home/utils/get_rebate_bills.py @@ -1,6 +1,3 @@ -from ..models import RebateSpring23 - - def get_rebate_bills(rebate, period): period = int(period) if period == 1: diff --git a/home/utils/rebate_bills_saver.py b/home/utils/rebate_bills_saver.py index c4489ae..83d8039 100644 --- a/home/utils/rebate_bills_saver.py +++ b/home/utils/rebate_bills_saver.py @@ -1,13 +1,8 @@ -from django.db.models.query import QuerySet - from ..models import ( Allocation, - Caterer, CatererBills, LeftLongRebate, - LongRebate, Period, - Semester, Student, StudentBills, ) @@ -138,11 +133,10 @@ def save_long_bill(email, days_per_period, j): break -def update_bills(student, allocation): +def update_bills(email, allocation): try: - email = student.email period = allocation.period - rebate_bill = StudentBills.objects.get(email=student, semester=period.semester) + rebate_bill = StudentBills.objects.get(email, semester=period.semester) sno = period.Sno days = (period.end_date - period.start_date).days + 1 high_tea = allocation.high_tea diff --git a/home/views.py b/home/views.py index ff5b375..d700c3b 100644 --- a/home/views.py +++ b/home/views.py @@ -1,13 +1,10 @@ -import io +import logging from datetime import date, timedelta from allauth.socialaccount.models import SocialAccount from django.contrib.admin.views.decorators import staff_member_required from django.contrib.auth.decorators import login_required -from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist -from django.core.files import File -from django.core.files.storage import default_storage -from django.http import HttpResponseRedirect, JsonResponse +from django.http import JsonResponse from django.shortcuts import redirect, render from django.utils.dateparse import parse_date from django.utils.timezone import now @@ -19,7 +16,6 @@ Cafeteria, Carousel, Caterer, - CatererBills, Contact, Form, LeftShortRebate, @@ -42,9 +38,11 @@ from .utils.get_rebate_bills import get_rebate_bills from .utils.rebate_checker import is_not_duplicate, max_days_rebate -# Create your views here. +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) +# Create your views here. def home(request): """ Display the Home Page :model:`home.models.home`. @@ -173,7 +171,7 @@ def rebate(request): continue key = str(allocation_id.student_id) except Exception as e: - print(e) + logger.error(e) key = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer" except Student.DoesNotExist: key = "Signed in account does not does not have any allocation ID" @@ -342,7 +340,8 @@ def addLongRebateBill(request): except Exception as e: text = "An error occurred while processing your form submission. If you're submitting an application, try compressing it before resubmitting. If the issue persists, please report it to the admin." print(e) - except: + except Exception as e: + logger.error(e) text = "Email ID does not exist in the database. Please login using the correct email ID" request.session["text"] = text return redirect(request.path) @@ -368,7 +367,6 @@ def allocationForm(request): alloc_form = AllocationForm.objects.filter(active=True).last() try: student = Student.objects.filter(email__iexact=str(request.user.email)).last() - key = student.email text = "" message = "" if (alloc_form.start_time and alloc_form.start_time > now()) or ( @@ -491,19 +489,12 @@ def profile(request): # else: # allocation_info = "Not allocated for this period" try: - if len(socialaccount_obj): + if socialaccount_obj: picture = socialaccount_obj[0].extra_data["picture"] - except: + else: + picture = "not available" + except (IndexError, KeyError): picture = "not available" - # if request.method == "POST" and request.user.is_authenticated: - # try: - # student = Student.objects.get(email=str(request.user.email)) - # student.name = request.POST["name"] - # student.room_no = request.POST["room_no"] - # student.save() - # text = "Profile Updated Successfully" - # except: - # text = "Email ID does not exist in the database. Please eneter the correct email ID" context = { "text": text, "student": student, diff --git a/pyproject.toml b/pyproject.toml index 8749965..c853ccf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,3 +9,4 @@ include_trailing_comma = true line_length = 88 multi_line_output = 3 known_first_party = ["config"] +skip = ["__init__.py"]