def obj_create(self, bundle, **kwargs): try: attachments = [] data = bundle.data contacts = associate_contacts(bundle, data) if 'attachments' in bundle.data: for atch in data['attachments']: attachment = Attachment.objects.get(id=atch['id']) attachments.append(attachment) del data['attachments'] fields_to_use = { 'author': bundle.request.user } for field in ['title', 'free_edit_body', 'private', 'text']: if field in data: try: #setattr(bundle.obj, field, data[field]) fields_to_use[field] = data[field] except Exception as e: logger.info('error setting field %s e=%s' % (field, e)) else: logger.info('field %s not allowed' % field) therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.attachments = attachments therequest.save() bundle.obj = therequest logger.info("request %s created" % therequest.id) except Exception as e: logger.exception(e) return bundle
def obj_create(self, bundle, **kwargs): try: attachments = [] data = bundle.data contacts = associate_contacts(bundle, data) if 'attachments' in bundle.data: for atch in data['attachments']: attachment = Attachment.objects.get(id=atch['id']) attachments.append(attachment) del data['attachments'] fields_to_use = {'author': bundle.request.user} for field in ['title', 'free_edit_body', 'private', 'text']: if field in data: try: #setattr(bundle.obj, field, data[field]) fields_to_use[field] = data[field] except Exception as e: logger.info('error setting field %s e=%s' % (field, e)) else: logger.info('field %s not allowed' % field) therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.attachments = attachments therequest.save() bundle.obj = therequest logger.info("request %s created" % therequest.id) except Exception as e: logger.exception(e) return bundle
def handle(self, *args, **options): letter_responses = {} if len(args) < 1: print "Please provide ID of Google Spreadsheet" return -1 idd = args[0] resp = requests.get("https://docs.google.com/spreadsheets/d/%s/pub?output=csv" % idd) reader = list(csv.reader(resp.content.split('\n'), delimiter=',')) header = reader[0] for row in reader[1:-1]: #get user, contact and agency user = User.objects.get(username=row[header.index('username')]) user_profile = UserProfile.objects.get(user=user) govt = get_or_create_us_govt(row[header.index("state")], 'state') agency, acreated = Agency.objects.get_or_create(name=row[header.index("agency")], government=govt) contact, ccreated = agency.contacts.get_or_create( first_name=row[header.index("contact.first.name")], middle_name=row[header.index("contact.middle.name")], last_name=row[header.index("contact.last.name")]) if row[header.index("contact.email")] != "": contact.add_email(row[header.index("contact.email")]) if row[header.index("contact.phone")] != "": contact.add_phone(row[header.index("contact.phone")]) #set up group and tags group, created = Group.objects.get_or_create(name=row[header.index("group")]) assign_perm(UserProfile.get_permission_name('edit'), user, group) assign_perm(UserProfile.get_permission_name('view'), user, group) user.groups.add(group) user_profile.tags.add(row[header.index("tag")]) #assemble law text law_texts = [] for l in govt.statutes.all(): law_texts.append('%s' % (l.short_title,)) law_text = ' and '.join(law_texts) #get the letter template letter_url = row[header.index("letter.url")] letter_template = '' if letter_url in letter_responses.keys(): letter_template = letter_responses[letter_url] else: letter_resp = requests.get(letter_url) letter_template = letter_resp.content letter_responses[letter_url] = letter_template #render the template context = Context({ 'contact': contact, 'user_profile': user_profile, 'user': user, 'law_text': law_text }) template = Template(letter_template) letter = template.render(context) #create the request fields_to_use = { 'author': user, 'title': row[header.index("request.title")], 'free_edit_body': letter, 'private': True if row[header.index("request.private")] == "TRUE" else False, 'text': letter#silly distinction leftover from old days but fill it in } #delete all requests that look like the one i'm about to make so we don't have duplicates floating around Request.objects.filter(author=user, title=row[header.index("request.title")]).delete() #create the request therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = [contact] therequest.government = govt therequest.agency = agency therequest.tags.add(row[header.index("tag")]) therequest.save() #assing permissions to the request assign_perm(Request.get_permission_name('view'), group, therequest) assign_perm(Request.get_permission_name('edit'), group, therequest) if row[header.index("request.send")] == "TRUE": therequest.send() print "SENT request %s" % row[header.index("request.title")] else: print "STAGED request %s" % row[header.index("request.title")]
def handle(self, *args, **options): users = [ User.objects.get(username='******'), #User.objects.get(username='******'), #User.objects.get(username='******'), #User.objects.get(username='******') ] up = UserProfile.objects.get(user=users[0]) up.tags.add(ncaa_tag_name) up.tags.add(coach_tag_name) for user in users: assign_perm(UserProfile.get_permission_name('edit'), user, ncaa_group) assign_perm(UserProfile.get_permission_name('view'), user, ncaa_group) assign_perm(UserProfile.get_permission_name('edit'), user, coach_group) assign_perm(UserProfile.get_permission_name('view'), user, coach_group) #Request.objects.all().delete() ncaa_text_to_use = """ Pursuant to the %s, I am requesting the following documents:<br/><br/>\ The equity/revenue-and-expenses report completed by the athletic department for the \ National Collegiate Athletic Association for the 2014 fiscal year. This report is a \ multi-page document that had to be submitted to the NCAA by Jan. 15, 2015. \ It contains 38 revenue and expense categories, followed by specific breakdowns of \ each of those categories, by sport and gender. I am requesting the full report, \ including the detail tables and the Statement of Revenues and Expenses that appear at the end of the report. <br/><br/>\ PLEASE NOTE: The NCAA report is different than the equity report that is sent to the\ U.S. Department of Education for Title IX compliance. <br/><br/>\ %s """ coach_text_to_use = """ Pursuant to %s, I am requesting the following documents:<br/><br/>\ The current contracts for %s. If a contract is under negotiation, \ please forward the current contract but let me know that a new contract may be forthcoming. \ If there is no contact for one or both, please forward the letter(s) of intent or other \ document(s) outlining each employee's conditions of employment \ -- including bonus structure -- and/or a current statement of salary. <br/><br/>\ %s """ fname = settings.SITE_ROOT + "/apps/requests/data/NCAA-pio.csv" #with codecs.open(fname, 'w', encoding="utf-8") as f: # resp = requests.get("https://docs.google.com/spreadsheets/d/1kccaiCCYIHOTEvpUWQiKs51v6K2TNRX7-NN6l1WtzyM/pub?output=csv") # f.write(resp.text) reader = list(UnicodeReader(open(fname, 'rb'))) #create contacts header = reader[0] for idx, row in enumerate(reader[1:]): user = users[0] up = UserProfile.objects.get(user=user) state = row[header.index('STATE')] agency_name = row[header.index("UNIVERSITY")] pio = row[header.index("PIO OFFICER")] email = row[header.index("PIO Email")] phone = row[header.index("PIO Phone")] sid_pio = row[header.index("SID ")] sid_email = row[header.index("SID Email")] sid_phone = row[header.index("SID Phone")] is_power = (row[header.index("Power Conference")] == 'TRUE') is_private = (row[header.index("Is Private")] == 'TRUE') if not is_private and state != '' and email != 'N/A' and pio != 'N/A' and agency_name != '': govt = get_or_create_us_govt(state, 'state') fname = pio.split(" ")[0] lname = pio.split(" ")[-1] middle = '' #alter table `contacts_contact` convert to character set utf8 collate utf8_general_ci; #alter table `agency_agency` convert to character set utf8 collate utf8_general_ci; #alter table `requests_request` convert to character set utf8 collate utf8_general_ci; try: agency, acreated = Agency.objects.get_or_create(name=agency_name, government=govt) except Exception as e: print e print "If more than one agency was returned, pick one!" import pdb;pdb.set_trace() try: contact, ccreated = agency.contacts.get_or_create(first_name=fname, middle_name=middle, last_name=lname) except Exception as e: print e print "If more than one contact was returned, pick one!" import pdb;pdb.set_trace() sid_contact = None if phone != 'N/A': contact.add_phone(phone) contact.add_email(email) #agency.contacts.add(contact) if sid_pio != 'N/A' and sid_email != 'N/A': fname = sid_pio.split(" ")[0] lname = sid_pio.split(" ")[-1] sid_contact, ccreated = Contact.objects.get_or_create(first_name=fname, middle_name='', last_name=lname) sid_contact.add_title("SID") sid_contact.add_email(sid_email) if sid_phone != 'N/A': sid_contact.add_phone(sid_phone) agency.contacts.add(sid_contact) contacts = [contact] if sid_contact is not None: contacts = [contact, sid_contact] agency.save() #logger.info('agency %s %s contact %s %s %s %s' % (agency_name, acreated, fname, middle, lname, ccreated)) law_texts = [] for l in govt.statutes.all(): law_texts.append('%s' % (l.short_title,)) misc_graf = """ Please advise me in advance of the estimated charges associated with fulfilling \ this request.</br></br>In the interest of expediency, and to minimize the research\ and/or duplication burden on your staff, please send records electronically if possible.\ If this is not possible, please notify me by phone at %s before sending to the address listed below. """ % (up.phone) misc_graf += '<br/></br>Sincerly,<br/><br/>%s<br/>%s<br/>%s<br/>%s' % (user.first_name + ' ' + user.last_name, up.mailing_address, up.mailing_city + ', ' + up.mailing_state + ' ' + up.mailing_zip, up.phone) if not is_power: fields_to_use = { 'author': user, 'title': 'NCAA Report - %s' % agency_name, 'free_edit_body': ncaa_text_to_use % (' and '.join(law_texts), misc_graf), 'private': True, 'text': ncaa_text_to_use } therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.government = govt therequest.agency = agency therequest.tags.add(ncaa_tag_name) therequest.save() assign_perm(Request.get_permission_name('view'), ncaa_group, therequest) #assign_perm(Request.get_permission_name('edit'), thegroup, therequest) coaches = [ 'Football Coach', 'Offensive Coord.', 'Defensive Coord.', "Men's BB Coach", "Women's BB Coach" ] coaches_str = [] for coach in coaches: val = row[header.index(coach)].strip() if val != 'N/A' and val != '': coaches_str.append("%s (%s)" % (val, coach)) print val fields_to_use = { 'author': user, 'title': 'Coach Contracts - %s' % agency_name, 'free_edit_body': coach_text_to_use % (' and '.join(law_texts), ', '.join(coaches_str), misc_graf), 'private': True, 'text': coach_text_to_use } therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.government = govt therequest.agency = agency therequest.tags.add(coach_tag_name) therequest.save() assign_perm(Request.get_permission_name('view'), coach_group, therequest)
def handle(self, *args, **options): users = [ User.objects.get(username='******'), #User.objects.get(username='******'), #User.objects.get(username='******'), #User.objects.get(username='******') ] up = UserProfile.objects.get(user=users[0]) up.tags.add(ncaa_tag_name) up.tags.add(coach_tag_name) for user in users: assign_perm(UserProfile.get_permission_name('edit'), user, ncaa_group) assign_perm(UserProfile.get_permission_name('view'), user, ncaa_group) assign_perm(UserProfile.get_permission_name('edit'), user, coach_group) assign_perm(UserProfile.get_permission_name('view'), user, coach_group) #Request.objects.all().delete() ncaa_text_to_use = """ Pursuant to the %s, I am requesting the following documents:<br/><br/>\ The equity/revenue-and-expenses report completed by the athletic department for the \ National Collegiate Athletic Association for the 2014 fiscal year. This report is a \ multi-page document that had to be submitted to the NCAA by Jan. 15, 2015. \ It contains 38 revenue and expense categories, followed by specific breakdowns of \ each of those categories, by sport and gender. I am requesting the full report, \ including the detail tables and the Statement of Revenues and Expenses that appear at the end of the report. <br/><br/>\ PLEASE NOTE: The NCAA report is different than the equity report that is sent to the\ U.S. Department of Education for Title IX compliance. <br/><br/>\ %s """ coach_text_to_use = """ Pursuant to %s, I am requesting the following documents:<br/><br/>\ The current contracts for %s. If a contract is under negotiation, \ please forward the current contract but let me know that a new contract may be forthcoming. \ If there is no contact for one or both, please forward the letter(s) of intent or other \ document(s) outlining each employee's conditions of employment \ -- including bonus structure -- and/or a current statement of salary. <br/><br/>\ %s """ fname = settings.SITE_ROOT + "/apps/requests/data/NCAA-pio.csv" #with codecs.open(fname, 'w', encoding="utf-8") as f: # resp = requests.get("https://docs.google.com/spreadsheets/d/1kccaiCCYIHOTEvpUWQiKs51v6K2TNRX7-NN6l1WtzyM/pub?output=csv") # f.write(resp.text) reader = list(UnicodeReader(open(fname, 'rb'))) #create contacts header = reader[0] for idx, row in enumerate(reader[1:]): user = users[0] up = UserProfile.objects.get(user=user) state = row[header.index('STATE')] agency_name = row[header.index("UNIVERSITY")] pio = row[header.index("PIO OFFICER")] email = row[header.index("PIO Email")] phone = row[header.index("PIO Phone")] sid_pio = row[header.index("SID ")] sid_email = row[header.index("SID Email")] sid_phone = row[header.index("SID Phone")] is_power = (row[header.index("Power Conference")] == 'TRUE') is_private = (row[header.index("Is Private")] == 'TRUE') if not is_private and state != '' and email != 'N/A' and pio != 'N/A' and agency_name != '': govt = get_or_create_us_govt(state, 'state') fname = pio.split(" ")[0] lname = pio.split(" ")[-1] middle = '' #alter table `contacts_contact` convert to character set utf8 collate utf8_general_ci; #alter table `agency_agency` convert to character set utf8 collate utf8_general_ci; #alter table `requests_request` convert to character set utf8 collate utf8_general_ci; try: agency, acreated = Agency.objects.get_or_create( name=agency_name, government=govt) except Exception as e: print e print "If more than one agency was returned, pick one!" import pdb pdb.set_trace() try: contact, ccreated = agency.contacts.get_or_create( first_name=fname, middle_name=middle, last_name=lname) except Exception as e: print e print "If more than one contact was returned, pick one!" import pdb pdb.set_trace() sid_contact = None if phone != 'N/A': contact.add_phone(phone) contact.add_email(email) #agency.contacts.add(contact) if sid_pio != 'N/A' and sid_email != 'N/A': fname = sid_pio.split(" ")[0] lname = sid_pio.split(" ")[-1] sid_contact, ccreated = Contact.objects.get_or_create( first_name=fname, middle_name='', last_name=lname) sid_contact.add_title("SID") sid_contact.add_email(sid_email) if sid_phone != 'N/A': sid_contact.add_phone(sid_phone) agency.contacts.add(sid_contact) contacts = [contact] if sid_contact is not None: contacts = [contact, sid_contact] agency.save() #logger.info('agency %s %s contact %s %s %s %s' % (agency_name, acreated, fname, middle, lname, ccreated)) law_texts = [] for l in govt.statutes.all(): law_texts.append('%s' % (l.short_title, )) misc_graf = """ Please advise me in advance of the estimated charges associated with fulfilling \ this request.</br></br>In the interest of expediency, and to minimize the research\ and/or duplication burden on your staff, please send records electronically if possible.\ If this is not possible, please notify me by phone at %s before sending to the address listed below. """ % (up.phone) misc_graf += '<br/></br>Sincerly,<br/><br/>%s<br/>%s<br/>%s<br/>%s' % ( user.first_name + ' ' + user.last_name, up.mailing_address, up.mailing_city + ', ' + up.mailing_state + ' ' + up.mailing_zip, up.phone) if not is_power: fields_to_use = { 'author': user, 'title': 'NCAA Report - %s' % agency_name, 'free_edit_body': ncaa_text_to_use % (' and '.join(law_texts), misc_graf), 'private': True, 'text': ncaa_text_to_use } therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.government = govt therequest.agency = agency therequest.tags.add(ncaa_tag_name) therequest.save() assign_perm(Request.get_permission_name('view'), ncaa_group, therequest) #assign_perm(Request.get_permission_name('edit'), thegroup, therequest) coaches = [ 'Football Coach', 'Offensive Coord.', 'Defensive Coord.', "Men's BB Coach", "Women's BB Coach" ] coaches_str = [] for coach in coaches: val = row[header.index(coach)].strip() if val != 'N/A' and val != '': coaches_str.append("%s (%s)" % (val, coach)) print val fields_to_use = { 'author': user, 'title': 'Coach Contracts - %s' % agency_name, 'free_edit_body': coach_text_to_use % (' and '.join(law_texts), ', '.join(coaches_str), misc_graf), 'private': True, 'text': coach_text_to_use } therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = contacts therequest.government = govt therequest.agency = agency therequest.tags.add(coach_tag_name) therequest.save() assign_perm(Request.get_permission_name('view'), coach_group, therequest)
def handle(self, *args, **options): letter_responses = {} if len(args) < 1: print "Please provide ID of Google Spreadsheet" return -1 idd = args[0] resp = requests.get( "https://docs.google.com/spreadsheets/d/%s/pub?output=csv" % idd) reader = list(csv.reader(resp.content.split('\n'), delimiter=',')) header = reader[0] for row in reader[1:-1]: #get user, contact and agency user = User.objects.get(username=row[header.index('username')]) user_profile = UserProfile.objects.get(user=user) govt = get_or_create_us_govt(row[header.index("state")], 'state') agency, acreated = Agency.objects.get_or_create( name=row[header.index("agency")], government=govt) contact, ccreated = agency.contacts.get_or_create( first_name=row[header.index("contact.first.name")], middle_name=row[header.index("contact.middle.name")], last_name=row[header.index("contact.last.name")]) if row[header.index("contact.email")] != "": contact.add_email(row[header.index("contact.email")]) if row[header.index("contact.phone")] != "": contact.add_phone(row[header.index("contact.phone")]) #set up group and tags group, created = Group.objects.get_or_create( name=row[header.index("group")]) assign_perm(UserProfile.get_permission_name('edit'), user, group) assign_perm(UserProfile.get_permission_name('view'), user, group) user.groups.add(group) user_profile.tags.add(row[header.index("tag")]) #assemble law text law_texts = [] for l in govt.statutes.all(): law_texts.append('%s' % (l.short_title, )) law_text = ' and '.join(law_texts) #get the letter template letter_url = row[header.index("letter.url")] letter_template = '' if letter_url in letter_responses.keys(): letter_template = letter_responses[letter_url] else: letter_resp = requests.get(letter_url) letter_template = letter_resp.content letter_responses[letter_url] = letter_template #render the template context = Context({ 'contact': contact, 'user_profile': user_profile, 'user': user, 'law_text': law_text }) template = Template(letter_template) letter = template.render(context) #create the request fields_to_use = { 'author': user, 'title': row[header.index("request.title")], 'free_edit_body': letter, 'private': True if row[header.index("request.private")] == "TRUE" else False, 'text': letter #silly distinction leftover from old days but fill it in } #delete all requests that look like the one i'm about to make so we don't have duplicates floating around Request.objects.filter( author=user, title=row[header.index("request.title")]).delete() #create the request therequest = Request(**fields_to_use) therequest.date_added = datetime.now() therequest.save() therequest.contacts = [contact] therequest.government = govt therequest.agency = agency therequest.tags.add(row[header.index("tag")]) therequest.save() #assing permissions to the request assign_perm(Request.get_permission_name('view'), group, therequest) assign_perm(Request.get_permission_name('edit'), group, therequest) if row[header.index("request.send")] == "TRUE": therequest.send() print "SENT request %s" % row[header.index("request.title")] else: print "STAGED request %s" % row[header.index("request.title")]