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

chore: fix lint #521

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
exclude = .git,__pycache__,backend/settings*,*/migrations/*,venv,.venv
exclude = .git,__pycache__,backend/settings*,*/migrations/*,venv,.venv,adj_list.py
ignore = E302,E731,W503
max-complexity = 15
max-line-length = 120
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MASTER]
load-plugins=pylint_django
ignore=.git,__pycache__,backend,migrations,venv,.venv
ignore=.git,__pycache__,backend,migrations,venv,.venv,adj_list.py

[MESSAGES CONTROL]
max-line-length=120
Expand Down
5 changes: 1 addition & 4 deletions bans/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from django.db import models
from uuid import uuid4
from django.db import models
from users.models import UserProfile

# Create your models here.


BAN_REASON_CHOICHES = [
("IDF", "Unappropriate Comment"),
("Buy&Sell", "Unappropriate Activity in Buy and Sell"),
Expand Down
28 changes: 12 additions & 16 deletions bans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
forbidden_no_privileges,
)


from users.models import UserProfile
from .models import SSOBan
from .serializers import SSOBansSerializer


# Create your views here.


class SSOBanViewSet(viewsets.ModelViewSet):
queryset = SSOBan.objects.all()
serializer_class = SSOBansSerializer
Expand All @@ -29,17 +25,17 @@ def list(self, request):
queryset = self.get_queryset()
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
else:
return forbidden_no_privileges()

return forbidden_no_privileges()

@login_required_ajax
def retrieve(self, request, pk):
if user_has_insti_privilege(request.user.profile, "RoleB"):
instance = get_object_or_404(self.queryset, pk=pk)
serializer = self.get_serializer(instance)
return Response(serializer.data)
else:
return forbidden_no_privileges()

return forbidden_no_privileges()

@login_required_ajax
def create(self, request):
Expand Down Expand Up @@ -69,11 +65,11 @@ def create(self, request):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return forbidden_no_privileges()

return forbidden_no_privileges()

@login_required_ajax
def update(self, request, pk=None, *args, **kwargs):
def update(self, request, *args, pk=None, **kwargs):
if user_has_insti_privilege(request.user.profile, "RoleB"):
instance = get_object_or_404(self.queryset, pk=pk)
serializer = self.get_serializer(instance, data=request.data, partial=True)
Expand All @@ -84,14 +80,14 @@ def update(self, request, pk=None, *args, **kwargs):
return Response(serializer.data)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return forbidden_no_privileges()

return forbidden_no_privileges()

@login_required_ajax
def destroy(self, request, pk=None, *args, **kwargs):
def destroy(self, request, *args, pk=None, **kwargs):
if user_has_insti_privilege(request.user.profile, "RoleB"):
instance = get_object_or_404(self.queryset, pk=pk)
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return forbidden_no_privileges()

return forbidden_no_privileges()
6 changes: 4 additions & 2 deletions buyandsell/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from uuid import uuid4

from django.db.models.deletion import SET
from django.db.models.fields.related import ForeignKey
from django.db import models
from uuid import uuid4

from helpers.misc import get_url_friendly

PDT_NAME_MAX_LENGTH = 60
Expand Down Expand Up @@ -49,7 +51,7 @@ class Product(models.Model):
name = models.CharField(max_length=PDT_NAME_MAX_LENGTH, blank=False, null=False)
description = models.TextField(blank=True, default="", null=False)
product_image = models.TextField(blank=True, null=True)
# TODO: Change the on_delete function to .
# _TODO: Change the on_delete function to .
category = models.ForeignKey(
Category, on_delete=models.SET_NULL, null=True, blank=True
)
Expand Down
45 changes: 25 additions & 20 deletions buyandsell/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""Views for BuyAndSell."""
import json

from django.conf import settings
from django.core.mail import send_mail
from django.shortcuts import get_object_or_404
from django.db.models import Q
from django.utils import timezone

from rest_framework.response import Response
from rest_framework import viewsets
from django.shortcuts import get_object_or_404

from roles.helpers import login_required_ajax
from buyandsell.models import Ban, Category, ImageURL, Limit, Product, Report
from buyandsell.serializers import ProductSerializer
from helpers.misc import query_search
from users.models import UserProfile
from django.db.models import Q
import json
from django.utils import timezone

REPORTS_THRES = 3

Expand All @@ -37,26 +40,26 @@ def update_limits(self):

def update_bans(self, product: Product = None):
if product is not None:
"""Get the existing reports on this product that have been accepted by the moderator
but have not been addressed (by initiating a ban)."""
# Get the existing reports on this product that have been accepted by the moderator
# but have not been addressed (by initiating a ban).
reports = Report.objects.filter(
product=product, addressed=False, accepted=True
)
if len(reports) >= REPORTS_THRES:
"""Create a ban for the user lasting three days."""
# Create a ban for the user lasting three days.
endtime = timezone.localtime() + timezone.timedelta(days=3)
Ban.objects.create(user=product.user, endtime=endtime)
product.deleted = True
reports.update(addressed=True)
else:
"""Calls the above if-block on products that have accepted but unaddressed reports."""
# Calls the above if-block on products that have accepted but unaddressed reports.
reports = Report.objects.filter(accepted=True, addressed=False)
products = []
for report in reports:
products.append(report.product)
for prod in set(products):
if products.count(prod) >= REPORTS_THRES:
"""Products from a banned user cannot be reported. (acc. to report function.)"""
# Products from a banned user cannot be reported. (acc. to report function.)
endtime = timezone.localtime() + timezone.timedelta(days=3)
Ban.objects.create(user=product.user, endtime=endtime)
reports.update(addressed=True)
Expand All @@ -77,13 +80,13 @@ def list(self, request):
# introduce tags?
self.update_bans()
queryset = self.queryset.filter(status=True)
"""remove products from banned users"""
# remove products from banned users
bans = Ban.objects.all()
for ban in bans:
if ban.endtime > timezone.localtime():
"""Remove products from users whose bans are still running."""
# Remove products from users whose bans are still running.
queryset = queryset.filter(~Q(user=ban.user))
# TODO: allow category to be passed here too.
# _TODO: allow category to be passed here too.
queryset = query_search(
request, 3, queryset, ["name", "description"], "buyandsell"
)
Expand All @@ -93,12 +96,15 @@ def list(self, request):
data = ProductSerializer(queryset, many=True).data
return Response(data)

@staticmethod
def get_contact_details(userpro: UserProfile):
return f"""
Phone: {userpro.contact_no}
Email: {userpro.email}"""

def update_image_urls(self, request, instance, image_urls=[]):
def update_image_urls(self, request, instance, image_urls=None):
if image_urls is None:
image_urls = []
if len(image_urls) == 0:
image_urls = json.loads(request.data["image_urls"])
ImageURL.objects.filter(product=instance).delete()
Expand All @@ -119,19 +125,18 @@ def create(self, request):
"""

self.update_limits()
from users.models import UserProfile

userpro = UserProfile.objects.get(user=request.user)

"""Limit checking:"""
limit, created = Limit.objects.get_or_create(user=userpro)
# Limit checking
limit, _ = Limit.objects.get_or_create(user=userpro)
if limit.strikes >= 1000:
return Response("Limit of Three Products per Day Reached.", status=403)
limit.strikes += 1
if limit.strikes == 3:
limit.endtime = timezone.localtime() + timezone.timedelta(days=1)
limit.save()
"""Create the product, modifying some fields."""
# Create the product, modifying some fields
# request.data._mutable = True
# request.data['status'] = True
# image_urls = json.loads(request.data['image_urls'])
Expand Down Expand Up @@ -177,12 +182,12 @@ def report(self, request, pk):
product = self.get_product(pk)
self.update_bans(product)
if len(Ban.objects.filter(user=product.user)) > 0:
"""If user is banned, their products don't show up in the list.
This if-block is for calls made to the api manually."""
# If user is banned, their products don't show up in the list.
# This if-block is for calls made to the api manually
return Response("User is Banned atm.")
reporter = UserProfile.objects.get(user=request.user)
# reporter = product.user
report_by_user, created = Report.objects.get_or_create(
report_by_user, _ = Report.objects.get_or_create(
product=product, reporter=reporter
)
report_by_user: Report
Expand Down
2 changes: 1 addition & 1 deletion community/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from community.models import Community, CommunityPost
from community.serializer_min import CommunityPostSerializerMin
from roles.serializers import RoleSerializerMin
from users.models import UserProfile
from bodies.models import Body
from users.models import UserProfile
from users.serializers import UserProfileSerializer


Expand Down
4 changes: 2 additions & 2 deletions community/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def create(self, request):
if "community" not in request.data or not request.data["community"]:
return forbidden_no_privileges()

user, created = UserProfile.objects.get_or_create(user=request.user)
UserProfile.objects.get_or_create(user=request.user)
return super().create(request)

@login_required_ajax
Expand Down Expand Up @@ -253,7 +253,7 @@ def get_community(self, pk):
@login_required_ajax
def create(self, request):
name = request.data["name"]
user, created = UserProfile.objects.get_or_create(user=request.user)
UserProfile.objects.get_or_create(user=request.user)
if not Community.objects.all().filter(name=name).exists():
super().create(request)
return Response({"message": "Community created"})
Expand Down
12 changes: 8 additions & 4 deletions locations/management/commands/adj_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def load_adj_list(self):
adj_list = {}

with open(adj_list_path, "r") as f:
# pylint: disable=eval-used
adj_list = dict(eval(f.read()))

return adj_list
Expand All @@ -33,11 +34,14 @@ def calculate_distance(loc1, loc2):

return m.sqrt(0.001 * ((x_loc1 - x_loc2) ** 2 + (y_loc1 - y_loc2) ** 2))

"""
This function updates the adj_list with the new connections and distances betweem them.
"""
def add_conns(self, loc1, connections=None):
"""
This function updates the adj_list with the new connections and distances betweem them.
"""

if not connections:
connections = []

def add_conns(self, loc1, connections=[]):
new_data = self.adj_list.copy()
for loc2 in connections:
if loc2:
Expand Down
Loading
Loading