Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def save(self, newsitem=True, newsitem_author=None, *args, **kwargs):
for f in self.config:
# if 'weekly' in False is invalid, so we have to check if self.config[f] is iterable
# before we check for 'weekly' or 'total'
if hasattr(self.config[f], '__iter__'):
if 'weekly' in self.config[f]:
self.config[f]['weekly'] = _round_hours(self.config[f]['weekly'])
if 'total' in self.config[f]:
self.config[f]['total'] = _round_hours(self.config[f]['total'])
super(TUG, self).save(*args, **kwargs)
if newsitem:
n = NewsItem(user=self.member.person, author=newsitem_author, course=self.member.offering,
source_app='ta', title='%s Time Use Guideline Changed' % (self.member.offering.name()),
content='Your Time Use Guideline for %s has been changed. If you have not already, please review it with the instructor.' % (self.member.offering.name()),
url=self.get_absolute_url())
n.save()
def __construct_subforms(self, data, initial, instance):
# this function is a simplification/clarification of this one liner:
# return OrderedDict((field, klass(prefix=field, data=data,
# initial=(instance.config[field] if instance and field in instance.config
# else initial[field] if initial and field in initial else None),
# label=TUG.config_meta[field]['label'] if field in TUG.config_meta else ''))
# for field, klass in itertools.chain(((f, TUGDutyForm) for f in TUG.regular_fields),
# ((f, TUGDutyOtherForm) for f in TUG.other_fields)))
field_names_and_formclasses = itertools.chain(
((f, TUGDutyForm) for f in TUG.regular_fields),
((f, TUGDutyOtherForm) for f in TUG.other_fields))
get_label = lambda field: TUG.config_meta[field]['label'] if field in TUG.config_meta else ''
get_initial = lambda field: None
if instance:
if initial:
get_initial = lambda field:(instance.config[field]
if field in instance.config else
initial.get(field, None))
else:
get_initial = lambda field:instance.config.get(field, None)
elif initial:
get_initial = lambda field:initial.get(field, None)
return OrderedDict(
(field,
hours_per_bu = contract_info.hours_per_bu
holiday_hours = contract_info.holiday_hours
prep_bu = contract_info.prep_bu
else:
has_lab_or_tut = course.labtas()
prep_min = LAB_PREP_HOURS if has_lab_or_tut else 0
bu = member.bu()
if not bu and tug:
bu = tug.base_units
lab_bonus = LAB_BONUS_DECIMAL
hours_per_bu = HOURS_PER_BU
holiday_hours = bu * HOLIDAY_HOURS_PER_BU
prep_bu = 0
if not tug:
tug = TUG(member=member)
if request.method == "POST":
form = TUGForm(instance=tug, data=request.POST, offering=course, userid=userid, enforced_prep_min=prep_min)
if form.is_valid():
tug = form.save(False)
tug.save(newsitem_author=Person.objects.get(userid=request.user.username))
return HttpResponseRedirect(reverse('offering:view_tug', args=(course.slug, userid)))
else:
form = TUGForm(instance=tug, offering=course, userid=userid, enforced_prep_min=prep_min, initial={
'holiday':{'total': holiday_hours},
'prep':{'total': prep_min or ''},
'base_units': bu})
if prep_bu:
form.fields['base_units'].help_text = \
('+ %s base units not assignable because of labs/tutorials' %
(prep_bu,))
offering_ids = set(m.offering_id for m in instr_members)
instr_tas = Member.objects.filter(offering_id__in=offering_ids, role='TA').select_related('offering__semester')
instr_tas = set(instr_tas)
all_tas = admin_tas | instr_tas
# build list of all instructors here, to save two queries per course later
offering_ids = set(m.offering_id for m in all_tas)
all_instr = Member.objects.filter(role='INST', offering_id__in=offering_ids).select_related('person', 'offering')
for inst in all_instr:
for ta in (m for m in all_tas if m.offering_id == inst.offering_id):
if not hasattr(ta, 'instructors'):
ta.instructors = []
ta.instructors.append(inst)
all_tugs = TUG.objects.filter(member__in=all_tas).select_related('member__person')
tug_dict = dict((tug.member_id, tug) for tug in all_tugs)
tas_with_tugs = [
{
'ta': ta,
'tug': tug_dict.get(ta.id, None),
'is_instr': ta in instr_tas
}
for ta in all_tas]
context = {
'admin': admin,
'semester': semester,
'tas_with_tugs': tas_with_tugs,
}
get_label = lambda field: TUG.config_meta[field]['label'] if field in TUG.config_meta else ''
def clean(self):
data = super(TUGForm, self).clean()
get_data = lambda subform: subform.cleaned_data if subform.cleaned_data else subform.initial
try: data['config'] = OrderedDict((field, get_data(self.subforms[field]))
for field in TUG.all_fields)
except AttributeError:
raise forms.ValidationError([])
# this can't possibly be the business logic, right?
#prep_hours = data['config']['prep']['total']
#if not prep_hours or prep_hours < self.enforced_prep_min:
# raise forms.ValidationError('Because this TA has labs or tutorials, you must assign at least %s base units to "Preparation".' % (self.enforced_prep_min))
return data
def view_tug(request, course_slug, userid):
course = get_object_or_404(CourseOffering, slug=course_slug)
member = get_object_or_404(Member, offering=course, person__userid=userid, role="TA")
try:
curr_user_role = Member.objects.exclude(role='DROP').get(person__userid=request.user.username, offering=course).role
except Member.DoesNotExist:
# we'll just assume this since it's the only other possibility
# since we're checking authorization in the decorator
curr_user_role = "ADMN"
#If the currently logged in user is a TA for the course and is viewing a TUG for another TA, show forbidden message
if curr_user_role=="TA" and not userid==request.user.username:
return ForbiddenResponse(request)
else:
tug = get_object_or_404(TUG, member=member)
iterable_fields = [(_, params) for _, params in tug.config.items() if hasattr(params, '__iter__') ]
total_hours = sum(decimal.Decimal(params.get('total',0)) for _, params in iterable_fields if params.get('total',0) is not None)
total_hours = round(total_hours, 2)
contract_info = __get_contract_info(member)
if contract_info:
bu = contract_info.bu
has_lab_or_tut = contract_info.has_labtut()
lab_bonus_decimal = contract_info.prep_bu
holiday_hours_per_bu = contract_info.holiday_hours_per_bu
hours_per_bu = contract_info.hours
total_bu = contract_info.total_bu
max_hours = contract_info.hours
else:
bu = tug.base_units
has_lab_or_tut = course.labtas()