Skip to content

Commit

Permalink
:bowtie: Reformat all Python code to Black standard (closes #123) (#251)
Browse files Browse the repository at this point in the history
This commit refactors all of the remaining Python code to follow the
standards set by the Python Software Foundation's "Black" tool:

https://github.com/psf/black

This are a lot of changes, but all of the changes are supposed to be
cosmetic. These changes change the appearance of the Python code to
adhere to the PEP-8 standards (with some deviations):

https://pep8.org/

I used the `code_lint.sh` script to automatically clean up all the code.
To better automate this in the future, I make a new pull request with a
git pre-commit hook so Black is run every time before a commit is made.
This way, we don't have to worry about following the standard
consistently; it will be mandated for us.

Closes #123. Huzzah! 🎉

Signed-off-by: Justin W. Flory <[email protected]>
  • Loading branch information
jwflory authored and leong96 committed Nov 28, 2019
1 parent 988eb84 commit 62a2e87
Show file tree
Hide file tree
Showing 9 changed files with 927 additions and 696 deletions.
2 changes: 1 addition & 1 deletion grasa_event_locator/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib import admin

# Register your models here.
from .models import *
from .models import *
2 changes: 1 addition & 1 deletion grasa_event_locator/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ApplicationConfig(AppConfig):
name = 'grasa_event_locator'
name = "grasa_event_locator"
13 changes: 8 additions & 5 deletions grasa_event_locator/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
("Public Speaking", "Public Speaking"),
("Social and Emotional Learning (SEL)", "Social and Emotional Learning (SEL)"),
("Sports and Recreation", "Sports and Recreation"),
("Science, Tech, Engineering, Math (STEM)", "Science, Tech, Engineering, Math (STEM)"),
(
"Science, Tech, Engineering, Math (STEM)",
"Science, Tech, Engineering, Math (STEM)",
),
("Tutoring", "Tutoring"),
("Other", "Other"),
]
Expand Down Expand Up @@ -52,7 +55,7 @@
("Evenings", "Evenings"),
("Weekends", "Weekends"),
("Summer", "Summer"),
#Refactored to "Other Time" to avoid conflicts with activities "Other"
# Refactored to "Other Time" to avoid conflicts with activities "Other"
("Other-Time", "Other-Time"),
]

Expand Down Expand Up @@ -131,8 +134,8 @@ def search(self):
print("q = something")
sqs = super(grasaSearchForm, self).search()
print(sqs.count())
sqs = sqs.order_by('title')

sqs = sqs.order_by("title")
selectedActivities = self.cleaned_data["activities"]
for activity in activityList:
for selectedActivity in selectedActivities:
Expand All @@ -144,7 +147,7 @@ def search(self):
for transportation in transportationList:
for selectedTransportation in selectedTransportations:
if transportation[0] == selectedTransportation:
print('inside transport filter')
print("inside transport filter")
print(transportation[0])
print(selectedTransportation)
sqs = sqs.filter(content=transportation[0])
Expand Down
15 changes: 9 additions & 6 deletions grasa_event_locator/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def change_username(old_email, new_email, request):
send_email(
[old_email, new_email],
render_to_string("messaging/email_change_confirm_subject.txt"),
render_to_string("messaging/email_change_confirm_mail.txt", context={
"request": request,
"old_email": old_email,
"new_email": new_email,
})
render_to_string(
"messaging/email_change_confirm_mail.txt",
context={
"request": request,
"old_email": old_email,
"new_email": new_email,
},
),
)
return

Expand Down Expand Up @@ -70,7 +73,7 @@ def write_categories_table():
"Evenings",
"Weekends",
"Summer",
"Other-Time"
"Other-Time",
]
# Had to leave this in but there's no way for someone to inject SQL here
with connection.cursor() as cursor:
Expand Down
70 changes: 49 additions & 21 deletions grasa_event_locator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,46 @@ class userInfo(models.Model):
isAdmin = models.BooleanField(default=False)
isPending = models.BooleanField(default=True)
contact_name = models.CharField(max_length=255, default="Name not Specified")
contact_phone = models.CharField(max_length=255, default="Phone Number not Specified")
contact_email = models.CharField(max_length=255, default="Email Address not Specified")
contact_phone = models.CharField(
max_length=255, default="Phone Number not Specified"
)
contact_email = models.CharField(
max_length=255, default="Email Address not Specified"
)
image_reference = models.CharField(max_length=40, default="NOT SPECIFIED")
last_login = models.CharField(max_length=100, default="Has Not Logged In")

def __str__(self):
return self.org_name

#Get a specific users info by their ID
# Get a specific users info by their ID
def get_by_id(self, id):
query = userInfo.objects.filter(id=id)
if query.count() == 1:
return query[0]
return None

#Since we are not deleting users and have pending users we only want to gab ones that are Active
# Since we are not deleting users and have pending users we only want to gab ones that are Active
def get_all_users(self):
query = userInfo.objects.filter(isActive=True)
if query.count() > 0:
return query
return None

#Return all pending users, will be useful for showing on admin page all accounts awaiting approval
# Return all pending users, will be useful for showing on admin page all accounts awaiting approval
def get_all_pending(self):
query = userInfo.objects.filter(isPending=True)
if query.count() > 0:
return query
return None

#Edit a user's email - likely to change with auth
# Edit a user's email - likely to change with auth
def editEmail(self, id, tmpEmail):
u = self.get_by_id(id)
u.email = tmpEmail
u.save()

#Edit a user's password - likely to change with auth
# Edit a user's password - likely to change with auth
def editPassword(self, id, tmpPassword):
u = self.get_by_id(id)
u.password = tmpPassword
Expand All @@ -67,7 +71,7 @@ class Program(models.Model):
lat = models.CharField(max_length=255, default="43.154535")
lng = models.CharField(max_length=255, default="-77.590575")
website = models.CharField(max_length=255, default="NOT SPECIFIED")
#Refactored fees to a float so they could be filtered by a range
# Refactored fees to a float so they could be filtered by a range
fees = models.FloatField(default=0)
isPending = models.BooleanField(default=True)
contact_name = models.CharField(max_length=255, default="NOT SPECIFIED")
Expand All @@ -76,49 +80,72 @@ class Program(models.Model):
editOf = models.IntegerField(default=0)
categories = models.ManyToManyField(Category)


def __str__(self):
return self.title

#Get a specific programs info by it's ID
# Get a specific programs info by it's ID
def get_by_id(self, id):
query = Program.objects.filter(id=id)
if query.count() == 1:
return query[0]
return None

#Since we have pending programs we only want to gab ones that are not pending
# Since we have pending programs we only want to gab ones that are not pending
def get_all_programs(self):
query = Program.objects.filter(isPending=False)
if query.count() > 0:
return query
return None

#Since we have programs that belong to a user, we may want to get all that user's programs
# Since we have programs that belong to a user, we may want to get all that user's programs
def get_my_programs(self, id):
query = Program.objects.filter(user_id=id)
if query.count() > 0:
return query
return None

#Return all pending programs, will be useful for showing on admin page all programs awaiting approval
# Return all pending programs, will be useful for showing on admin page all programs awaiting approval
def get_all_pending(self):
query = Program.objects.filter(isPending=True)
if query.count() > 0:
return query
return None

#New Program, defaults to pending
def createNewProgram(self, tmpUser_id, tmpTitle, tmpContent, tmpAddress, tmpWebsite, tmpCategories):
p = Program(user_id=tmpUser_id, title=tmpTitle, content=tmpContent, address=tmpAddress, website=tmpWebsite)
# New Program, defaults to pending
def createNewProgram(
self, tmpUser_id, tmpTitle, tmpContent, tmpAddress, tmpWebsite, tmpCategories
):
p = Program(
user_id=tmpUser_id,
title=tmpTitle,
content=tmpContent,
address=tmpAddress,
website=tmpWebsite,
)
for descr in tmpCategories:
c = Category.objects.get(description=descr)
p.categories.add(c)
p.save()

#Edit a program, creates a new program with an editOf value that corresponds to the original program
def editProgram(self, currentId, tmpUser_id, tmpTitle, tmpContent, tmpAddress, tmpWebsite, tmpCategories):
p = Program(user_id=tmpUser_id, title=tmpTitle, content=tmpContent, address=tmpAddress, website=tmpWebsite, editOf=currentId)
# Edit a program, creates a new program with an editOf value that corresponds to the original program
def editProgram(
self,
currentId,
tmpUser_id,
tmpTitle,
tmpContent,
tmpAddress,
tmpWebsite,
tmpCategories,
):
p = Program(
user_id=tmpUser_id,
title=tmpTitle,
content=tmpContent,
address=tmpAddress,
website=tmpWebsite,
editOf=currentId,
)
for descr in tmpCategories:
c = Category.objects.get(description=descr)
p.categories.add(c)
Expand All @@ -134,12 +161,13 @@ def approveProgram(self, id):
origProg.delete()
p.save()

#Deny a currently pending program by ID, will only be inactive
# Deny a currently pending program by ID, will only be inactive
def denyProgram(self, id):
p = self.get_by_id(id)
p.delete()


class resetPWURLs(models.Model):
user_ID = models.TextField(default="DEFAULT PROVIDER")
reset_string = models.CharField(max_length=15, default="A DEFAULTSTRING")
expiry_time = models.CharField(max_length=26, default="NO TIMESTAMP")
expiry_time = models.CharField(max_length=26, default="NO TIMESTAMP")
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

def rebuildWhooshIndex():
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
command = 'cd {}; yes | python3 manage.py rebuild_index'.format(PROJECT_PATH)
command = "cd {}; yes | python3 manage.py rebuild_index".format(PROJECT_PATH)
command = shlex.quote(command)
command = shlex.split(command)
process = subprocess.Popen(command, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
stdout, stderr = process.communicate()
stdout = stdout.decode().strip()
stderr = stderr.decode().strip()
status = process.returncode



if status == 0:
print(stdout)
print('\nindex rebuilt')
print(stdout)
print("\nindex rebuilt")
else:
print(stderr)
print('\nCould not rebuild index')
print(stderr)
print("\nCould not rebuild index")
10 changes: 5 additions & 5 deletions grasa_event_locator/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

class ProgramIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
#Added this line so that filtering by fees didn't always return empty set
fees = indexes.FloatField(model_attr='fees')
title = indexes.CharField(model_attr='title')
# Added this line so that filtering by fees didn't always return empty set
fees = indexes.FloatField(model_attr="fees")
title = indexes.CharField(model_attr="title")

def get_model(self):
return Program

def index_queryset(self, using=None):
#"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(isPending=False)
# """Used when the entire index for model is updated."""
return self.get_model().objects.filter(isPending=False)
Loading

0 comments on commit 62a2e87

Please sign in to comment.