def import_ballot(item, last_ballot, cache, fields=None): if fields is None: fields = ballot_fields entry = item.as_dict(fields) #if entry['number'] is None: # return None try: number = entry['number'].as_int() except (ValueError, TypeError): return None if number == 0: number = cache.get_next_lb_number() cache.set_next_lb_number(number + 1) b = None try: b = Ballot.objects.get(number=number) except Ballot.DoesNotExist: b = Ballot(number=number) for field in b._meta.fields: if not field.primary_key: try: value = to_python(field, entry[field.attname].value) if entry[ field.attname] is not None else None setattr(b, field.attname, value) except KeyError: pass try: b.project = cache.get_project( task_group=entry['project.task_group'].value) except KeyError: b.project = Project.objects.get( task_group=entry['project.task_group'].value) cache.put_project(b.project) if b.opened is None: set_date(b, 'closed', entry['date'].value, format='%Y-%m-%d') b.opened = b.closed + datetime.timedelta(days=-15) if b.pool is None: try: b.pool = int(b.vote_for) + int(b.vote_against) + int( b.vote_abstain) except TypeError: pass #b.save() return b
def import_html_letter_ballot(item, last_ballot, cache): entry = item.as_dict(html_ballot_fields) if entry['number'] and entry['task_group']: lbnum = as_int(entry['number']) try: b = Ballot.objects.get(number=lbnum) except Ballot.DoesNotExist: b = Ballot(number=lbnum) if entry['task_group'] == 'TGm': # TODO: Find a better solution entry['task_group'] = 'TGma' try: wg = cache.get_project(task_group=entry['task_group']) except KeyError: try: wg = Project.objects.get(task_group=entry['task_group']) except Project.DoesNotExist: entry['task_group'] = entry['task_group'][:-1] + entry[ 'task_group'][-1].lower() try: wg = Project.objects.get(task_group=entry['task_group']) except Project.DoesNotExist: return None b.project = wg try: index = entry['comments'].index('Draft ') b.draft = entry['comments'][index + 6:] except ValueError: try: index = entry['documents'].index('_D') index2 = entry['documents'].index('.pdf') b.draft = entry['documents'][index + 2:index2] except ValueError: b.draft = 0 try: b.instructions_url = entry['instructions'].anchor except AttributeError: b.instructions_url = entry['instructions'] try: b.draft_url = entry['documents'].anchor except AttributeError: b.draft_url = make_url( entry['documents'], '/11/private/Draft_Standards/11%s/' % (wg.task_group[2:])) b.pool = as_int_or_none(entry['result']) set_date(b, 'opened', entry['opened']) set_date(b, 'closed', entry['closed']) if b.opened is None or b.closed is None: raise Exception("Failed to find date in " + str(entry)) comments = entry['comments'].lower() if comments.find('recirculation') >= 0: b.ballot_type = Ballot.WGRecirc.code elif comments.find('procedural') >= 0: b.ballot_type = Ballot.Procedural.code else: b.ballot_type = Ballot.WGInitial.code return b if last_ballot is None: return None if entry['draft']: draft = entry['draft'].lower() if 'redline' in draft: try: last_ballot.redline_url = entry['documents'].anchor except AttributeError: last_ballot.redline_url = None #make_url(entry['documents'],'/11/private/Draft_Standards/11%s/'%(last_ballot.project.task_group[2:])) elif 'resolution' in draft: try: last_ballot.resolution_url = entry['documents'].anchor except AttributeError: last_ballot.resolution_url = make_url( entry['documents'], '/11/private/Draft_Standards/11%s/' % (last_ballot.project.task_group[2:])) elif 'comment' in draft: try: last_ballot.template_url = entry['documents'].anchor except AttributeError: last_ballot.template_url = '/11/LetterBallots/LB%ds/LB%d_comment_form.xls' % ( last_ballot.number, last_ballot.number) elif 'pool' in draft: try: last_ballot.pool_url = entry['documents'].anchor except AttributeError: last_ballot.pool_url = '/11/LetterBallots/LB%ds/LB%d_voters_list.xls' % ( last_ballot.number, last_ballot.number) result_type = entry['result_type'].lower( ) if entry['result_type'] is not None else None #if result_type =='approve%': # last_ballot.result = as_int_or_none(item[-1]) if result_type == 'approve': last_ballot.vote_for = as_int_or_none(entry['result']) elif result_type == 'disapprove': last_ballot.vote_against = as_int_or_none(entry['result']) elif result_type == 'abstain': last_ballot.vote_abstain = as_int_or_none(entry['result']) return None