Flake8 Fixes

Mostly whitespace fixes
Some unicode fixes
Fixed up CSV writer.  str is not bytes and all.
This commit is contained in:
Sachi King 2017-04-17 22:51:48 +10:00
parent ecf14b514d
commit 298b162be6
18 changed files with 97 additions and 94 deletions

View file

@ -25,13 +25,13 @@ def export_as_csv_action(description=None, fields=None, exclude=None,
excludeset = set(exclude)
field_names = field_names - excludeset
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = "attachment; filename=%s.csv" % unicode(opts).replace(".", "_")
response["Content-Disposition"] = "attachment; filename=%s.csv" % str(opts).replace(".", "_")
writer = csv.writer(response)
if header:
writer.writerow(list(field_names))
for obj in queryset:
writer.writerow(
[unicode(getattr(obj, field)).encode("utf-8", "replace") for field in field_names])
[str(getattr(obj, field)).encode("utf-8", "replace") for field in field_names])
return response
if description is None:
description = _("Export selected objects as CSV file")

View file

@ -205,6 +205,7 @@ class ProposalBase(models.Model):
def __str__(self):
return self.title
reversion.register(ProposalBase)

View file

@ -17,4 +17,4 @@ admin.site.register(
list_display=['proposal', 'status', 'score', 'vote_count', 'accepted']
)
admin.site.register(ResultNotification)
admin.site.register(ResultNotification)

View file

@ -8,5 +8,5 @@ class Command(BaseCommand):
def handle(self, *args, **options):
for proposal in ProposalBase.objects.filter(cancelled=0):
print "Creating assignments for %s" % (proposal.title,)
print("Creating assignments for %s" % proposal.title)
ReviewAssignment.create_assignments(proposal)

View file

@ -22,4 +22,4 @@ class Command(BaseCommand):
content_type__pk=ct.id,
defaults={"name": "Can %s %s" % (action, ps), "content_type": ct}
)
print perm
print(perm)

View file

@ -47,6 +47,8 @@ class Votes(object):
(MINUS_TWO, _("2 — Serious issues and I will argue to reject this proposal.")),
(ABSTAIN, _("Abstain - I do not want to review this proposal and I do not want to see it again.")),
]
VOTES = Votes()
@ -395,4 +397,6 @@ def accepted_proposal(sender, instance=None, **kwargs):
promote_proposal(instance.proposal)
else:
unpromote_proposal(instance.proposal)
post_save.connect(accepted_proposal, sender=ProposalResult)

View file

@ -78,23 +78,23 @@ REVIEW_STATUS_FILTERS = {
# proposals with at least VOTE_THRESHOLD reviews and at least one +2 and no -2s, sorted by
# the 'score'
POSITIVE: lambda qs: qs.filter(result__vote_count__gte=VOTE_THRESHOLD, result__plus_two__gt=0,
result__minus_two=0).order_by("-result__score"),
result__minus_two=0).order_by("-result__score"),
# proposals with at least VOTE_THRESHOLD reviews and at least one -2 and no +2s, reverse
# sorted by the 'score'
NEGATIVE: lambda qs: qs.filter(result__vote_count__gte=VOTE_THRESHOLD, result__minus_two__gt=0,
result__plus_two=0).order_by("result__score"),
result__plus_two=0).order_by("result__score"),
# proposals with at least VOTE_THRESHOLD reviews and neither a +2 or a -2, sorted by total
# votes (lowest first)
INDIFFERENT: lambda qs: qs.filter(result__vote_count__gte=VOTE_THRESHOLD, result__minus_two=0,
result__plus_two=0).order_by("result__vote_count"),
result__plus_two=0).order_by("result__vote_count"),
# proposals with at least VOTE_THRESHOLD reviews and both a +2 and -2, sorted by total
# votes (highest first)
CONTROVERSIAL: lambda qs: qs.filter(result__vote_count__gte=VOTE_THRESHOLD,
result__plus_two__gt=0, result__minus_two__gt=0)
.order_by("-result__vote_count"),
CONTROVERSIAL: lambda qs: qs.filter(
result__vote_count__gte=VOTE_THRESHOLD, result__plus_two__gt=0,
result__minus_two__gt=0).order_by("-result__vote_count"),
# proposals with fewer than VOTE_THRESHOLD reviews
TOO_FEW: lambda qs: qs.filter(result__vote_count__lt=VOTE_THRESHOLD)
.order_by("result__vote_count"),
TOO_FEW: lambda qs: qs.filter(
result__vote_count__lt=VOTE_THRESHOLD).order_by("result__vote_count"),
}
@ -153,7 +153,7 @@ def review_all_proposals_csv(request):
# The fields from each proposal object to report in the csv
fields = [
"id", "proposal_type", "speaker_name","speaker_email", "title",
"id", "proposal_type", "speaker_name", "speaker_email", "title",
"submitted", "other_speakers", "speaker_travel",
"speaker_accommodation", "cancelled", "status", "score", "total_votes",
"minus_two", "minus_one", "plus_one", "plus_two",
@ -189,11 +189,6 @@ def review_all_proposals_csv(request):
csv_line = [getattr(proposal, field) for field in fields]
# Enusre that unicode items are handled properly.
for i, item in enumerate(csv_line):
if isinstance(item, unicode):
csv_line[i] = item.encode("utf8")
writer.writerow(csv_line)
return response
@ -229,7 +224,7 @@ def review_random_proposal(request, section_slug):
# Select a proposal with less than the median number of total votes
proposals = proposals_generator(request, queryset, check_speaker=False)
proposals = list(proposals)
proposals.sort(key = lambda proposal: proposal.total_votes)
proposals.sort(key=lambda proposal: proposal.total_votes)
# The first half is the median or less.
# The +1 means we round _up_.
proposals = proposals[:(len(proposals) + 1) / 2]
@ -319,7 +314,7 @@ def review_admin(request, section_slug):
yield user
reviewers_sorted = list(reviewers())
reviewers_sorted.sort(key= lambda reviewer: 0 - reviewer.total_votes)
reviewers_sorted.sort(key=lambda reviewer: 0 - reviewer.total_votes)
ctx = {
"section_slug": section_slug,

View file

@ -28,4 +28,4 @@ def create_slot(section_slug, date, kind, start, end, rooms):
slot_room.slot = slot
slot_room.room = room
slot_room.save(force_insert=True)
print "created {} [start={}; end={}]".format(slot.kind.label, slot.start, slot.end)
print("created {} [start={}; end={}]".format(slot.kind.label, slot.start, slot.end))

View file

@ -21,6 +21,7 @@ from symposion.schedule.models import Schedule, Day, Slot, Presentation, Session
from symposion.schedule.timetable import TimeTable
from symposion.conference.models import Conference
def fetch_schedule(slug):
qs = Schedule.objects.all()
@ -265,11 +266,13 @@ def schedule_json(request):
content_type="application/json"
)
class EventFeed(ICalFeed):
product_id = '-//linux.conf.au/schedule//EN'
timezone = settings.TIME_ZONE
filename = 'conference.ics'
def description(self):
return Conference.objects.all().first().title
@ -283,7 +286,7 @@ class EventFeed(ICalFeed):
def item_title(self, item):
if hasattr(item.content, 'proposal'):
title = item.content.title
title = item.content.title
else:
title = item.kind if item.kind else "Slot"
return title
@ -305,18 +308,18 @@ class EventFeed(ICalFeed):
def item_location(self, item):
return ", ".join(room["name"] for room in item.rooms.values())
def item_link(self, item):
def item_link(self, item) -> str:
if hasattr(item.content, 'proposal'):
return 'http://%s%s' % (
Site.objects.get_current().domain,
reverse('schedule_presentation_detail', args=[item.content.pk])
)
return (
'http://%s%s' % (Site.objects.get_current().domain,
reverse('schedule_presentation_detail', args=[item.content.pk])))
else:
return 'http://%s' % Site.objects.get_current().domain
def item_guid(self, item):
return '%d@%s' % (item.pk, Site.objects.get_current().domain)
def session_list(request):
sessions = Session.objects.all().order_by('pk')

View file

@ -9,11 +9,12 @@ from symposion.speakers.models import Speaker
class Command(BaseCommand):
def handle(self, *args, **options):
csv_file = csv.writer(open(os.path.join(os.getcwd(), "speakers.csv"), "wb"))
csv_file.writerow(["Name", "Bio"])
with open(os.path.join(os.getcwd(), "speakers.csv"), "w") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Name", "Bio"])
for speaker in Speaker.objects.all():
csv_file.writerow([
speaker.name.encode("utf-8"),
speaker.biography.encode("utf-8"),
])
for speaker in Speaker.objects.all():
csv_writer.writerow([
speaker.name,
speaker.biography,
])

View file

@ -66,9 +66,9 @@ class Speaker(models.Model):
accessibility = models.TextField(
blank=True,
help_text=_("Please describe any special accessibility requirements "
"that you may have. Edit using "
"<a href='http://warpedvisions.org/projects/"
"markdown-cheat-sheet/' target='_blank'>Markdown</a>."),
"that you may have. Edit using "
"<a href='http://warpedvisions.org/projects/"
"markdown-cheat-sheet/' target='_blank'>Markdown</a>."),
verbose_name=_("Accessibility requirements"))
accessibility_html = models.TextField(blank=True)
travel_assistance = models.BooleanField(

View file

@ -30,48 +30,48 @@ class Command(BaseCommand):
except:
pass
csv_file = csv.writer(
open(os.path.join(os.getcwd(), "build", "sponsors.csv"), "wb")
)
csv_file.writerow(["Name", "URL", "Level", "Description"])
with open(os.path.join(os.getcwd(), "build", "sponsors.csv"), "w") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Name", "URL", "Level", "Description"])
for sponsor in Sponsor.objects.all():
path = os.path.join(os.getcwd(), "build", slugify(sponsor.name))
try:
os.makedirs(path)
except:
pass
for sponsor in Sponsor.objects.all():
path = os.path.join(os.getcwd(), "build", slugify(sponsor.name))
try:
os.makedirs(path)
except:
pass
data = {
"name": sponsor.name,
"url": sponsor.external_url,
"level": sponsor.level.name,
"description": "",
}
for sponsor_benefit in sponsor.sponsor_benefits.all():
if sponsor_benefit.benefit_id == 2:
data["description"] = sponsor_benefit.text
if sponsor_benefit.benefit_id == 1:
if sponsor_benefit.upload:
data["ad"] = sponsor_benefit.upload.path
if sponsor_benefit.benefit_id == 7:
if sponsor_benefit.upload:
data["logo"] = sponsor_benefit.upload.path
data = {
"name": sponsor.name,
"url": sponsor.external_url,
"level": sponsor.level.name,
"description": "",
}
if "ad" in data:
ad_path = data.pop("ad")
shutil.copy(ad_path, path)
if "logo" in data:
logo_path = data.pop("logo")
shutil.copy(logo_path, path)
for sponsor_benefit in sponsor.sponsor_benefits.all():
if sponsor_benefit.benefit_id == 2:
data["description"] = sponsor_benefit.text
if sponsor_benefit.benefit_id == 1:
if sponsor_benefit.upload:
data["ad"] = sponsor_benefit.upload.path
if sponsor_benefit.benefit_id == 7:
if sponsor_benefit.upload:
data["logo"] = sponsor_benefit.upload.path
csv_file.writerow([
data["name"].encode("utf-8"),
data["url"].encode("utf-8"),
data["level"].encode("utf-8"),
data["description"].encode("utf-8")
])
if "ad" in data:
ad_path = data.pop("ad")
shutil.copy(ad_path, path)
if "logo" in data:
logo_path = data.pop("logo")
shutil.copy(logo_path, path)
zipdir(
os.path.join(os.getcwd(), "build"),
os.path.join(os.getcwd(), "sponsors.zip"))
csv_writer.writerow([
data["name"],
data["url"],
data["level"],
data["description"]
])
zipdir(
os.path.join(os.getcwd(), "build"),
os.path.join(os.getcwd(), "sponsors.zip"))

View file

@ -19,7 +19,7 @@ class Command(BaseCommand):
sponsor=sponsor, benefit=benefit_level.benefit)
if created:
print "created", sponsor_benefit, "for", sponsor
print("created %s for %s" % (sponsor_benefit, sponsor))
# and set to default limits for this level.
sponsor_benefit.max_words = benefit_level.max_words

View file

@ -203,12 +203,16 @@ class Sponsor(models.Model):
def _store_initial_level(sender, instance, **kwargs):
if instance:
instance._initial_level_id = instance.level_id
post_init.connect(_store_initial_level, sender=Sponsor)
def _check_level_change(sender, instance, created, **kwargs):
if instance and (created or instance.level_id != instance._initial_level_id):
instance.reset_benefits()
post_save.connect(_check_level_change, sender=Sponsor)
@ -330,4 +334,6 @@ def _denorm_weblogo(sender, instance, created, **kwargs):
sponsor = instance.sponsor
sponsor.sponsor_logo = instance
sponsor.save()
post_save.connect(_denorm_weblogo, sender=SponsorBenefit)

View file

@ -13,4 +13,5 @@ class MembershipAdmin(VersionAdmin):
list_filter = ["team"]
search_fields = ["user__username"]
admin.site.register(Membership, MembershipAdmin)

View file

@ -16,16 +16,16 @@ class TeamPermissionsBackend(object):
if user_obj.is_anonymous() or obj is not None:
return set()
if not hasattr(user_obj, "_team_perm_cache"):
# Member permissions
# Member permissions
memberships = Team.objects.filter(
Q(memberships__user=user_obj),
Q(memberships__user=user_obj),
Q(memberships__state="member"),
)
perms = memberships.values_list(
"permissions__content_type__app_label",
"permissions__codename"
).order_by()
permissions = ["%s.%s" % (ct, name) for ct, name in perms]
permissions = ["%s.%s" % (ct, name) for ct, name in perms]
# Manager permissions
memberships = Team.objects.filter(
Q(memberships__user=user_obj),
@ -35,7 +35,7 @@ class TeamPermissionsBackend(object):
"manager_permissions__content_type__app_label",
"manager_permissions__codename"
).order_by()
permissions += ["%s.%s" % (ct, name) for ct, name in perms]
permissions += ["%s.%s" % (ct, name) for ct, name in perms]
user_obj._team_perm_cache = set(permissions)
return user_obj._team_perm_cache

View file

@ -69,6 +69,7 @@ class Team(models.Model):
verbose_name = _('Team')
verbose_name_plural = _('Teams')
MEMBERSHIP_STATE_CHOICES = [
("applied", _("applied")),
("invited", _("invited")),
@ -93,4 +94,5 @@ class Membership(models.Model):
verbose_name = _("Membership")
verbose_name_plural = _("Memberships")
reversion.register(Membership)

View file

@ -53,16 +53,6 @@ def __send_email__(template_prefix, to, kind, **kwargs):
from_email = settings.DEFAULT_FROM_EMAIL
try:
bcc_email = settings.ENVELOPE_BCC_LIST
except AttributeError:
bcc_email = None
try:
bcc_email = settings.ENVELOPE_BCC_LIST
except AttributeError:
bcc_email = None
email = EmailMultiAlternatives(subject, message_plaintext, from_email, to)
email.attach_alternative(message_html, "text/html")
email.send()