def main(): # Copy campaigns with open('campaigns_for_management.csv', newline='', encoding="utf8") as f: reader = csv.reader(f) campaigns = list(reader) for item in campaigns: if item[1] != "url": campaign = Campaign() campaign.url = item[1] campaign.campaign_id = item[2] campaign.auto_fb_post_mode = item[3] campaign.collected_date = item[4] campaign.category_id = item[5] campaign.category = item[6] campaign.currencycode = item[7] campaign.current_amount = item[8] campaign.goal = item[9] campaign.donators = item[10] campaign.days_active = item[11] campaign.days_created = item[12] campaign.title = item[13] campaign.description = item[14] campaign.default_url = item[15] campaign.has_beneficiary = item[16] campaign.media_type = item[17] campaign.project_type = item[18] campaign.turn_off_donations = item[19] campaign.user_id = item[20] campaign.user_first_name = item[21] campaign.user_last_name = item[22] campaign.user_facebook_id = item[23] campaign.user_profile_url = item[24] campaign.visible_in_search = item[25] campaign.status = item[26] campaign.deactivated = item[27] campaign.state = item[28] campaign.is_launched = item[29] campaign.campaign_image_url = item[30] campaign.created_at = item[31] campaign.launch_date = item[32] campaign.campaign_hearts = item[33] campaign.social_share_total = item[34] campaign.social_share_last_udpate = item[35] campaign.location_city = item[36] campaign.location_country = item[37] campaign.location_zip = item[38] campaign.is_charity = item[39] campaign.charity_valid = item[40] campaign.charity_npo_id = item[41] campaign.charity_name = item[42] campaign.velocity = item[43] campaign.overall_popularity = item[44] campaign.percent_goal_complete = item[45] campaign.avg_donation_per_donor = item[46] print(campaign) campaign.save()
def main(): with open("campaigns.csv", mode="r", encoding="utf-8-sig") as read_file: campaignData = csv.DictReader(read_file) print('CAMPAIGNS') for camp in campaignData: dbcamp = Campaign() dbcamp.url = str(camp['url']) dbcamp.campaign_id = str(camp['campaign_id']) dbcamp.auto_fb_post_mode = str(camp['auto_fb_post_mode']) dbcamp.collected_date = str(camp['collected_date']) dbcamp.category_id = str(camp['category_id']) dbcamp.category = str(camp['category']) dbcamp.currencycode = str(camp['currencycode']) dbcamp.current_amount = str(camp['current_amount']) dbcamp.goal = str(camp['goal']) dbcamp.donators = str(camp['donators']) dbcamp.days_active = str(camp['days_active']) dbcamp.days_created = str(camp['days_created']) dbcamp.title = str(camp['title']) dbcamp.description = str(camp['description']) dbcamp.default_url = str(camp['default_url']) dbcamp.has_beneficiary = str(camp['has_beneficiary']) dbcamp.media_type = str(camp['media_type']) dbcamp.project_type = str(camp['project_type']) dbcamp.turn_off_donations = str(camp['turn_off_donations']) dbcamp.user_id = str(camp['user_id']) dbcamp.user_first_name = str(camp['user_first_name']) dbcamp.user_last_name = str(camp['user_last_name']) dbcamp.user_facebook_id = str(camp['user_facebook_id']) dbcamp.user_profile_url = str(camp['user_profile_url']) dbcamp.visible_in_search = str(camp['visible_in_search']) dbcamp.status = str(camp['status']) dbcamp.deactivated = str(camp['deactivated']) dbcamp.state = str(camp['state']) dbcamp.is_launched = str(camp['is_launched']) dbcamp.campaign_image_url = str(camp['campaign_image_url']) dbcamp.created_at = str(camp['created_at']) dbcamp.launch_date = str(camp['launch_date']) dbcamp.campaign_hearts = str(camp['campaign_hearts']) dbcamp.social_share_total = str(camp['social_share_total']) dbcamp.social_share_last_update = str( camp['social_share_last_update']) dbcamp.location_city = str(camp['location_city']) dbcamp.location_country = str(camp['location_country']) dbcamp.location_zip = str(camp['location_zip']) dbcamp.is_charity = str(camp['is_charity']) dbcamp.charity_valid = str(camp['charity_valid']) dbcamp.charity_npo_id = str(camp['charity_npo_id']) dbcamp.charity_name = str(camp['charity_name']) dbcamp.velocity = str(camp['velocity']) dbcamp.save() print(str(dbcamp.url))
def main(): #categories will be a dictionary #key: string name of category #value: the actual category? Weekdays = {} with open('products.json') as json_file: data = json.load(json_file) campaigns = data['campaigns'] #fill in everything from products for prod in campaigns: dbprod = Campaign() #dbprod.weekday = prod['weekday'] try: dbprod.weekday = Weekday.objects.get(day=prod['weekday']) except Weekday.DoesNotExist: newCategory = Weekday() newCategory.day = prod['weekday'] Weekdays[prod['weekday']] = newCategory newCategory.save() dbprod.weekday = Weekday.objects.get(day=prod['weekday']) #print(prod['category']) #print(categories[prod['category']]) #dbprod.Category = Category.objects.get(title=prod['category']) dbprod.url = prod['url'] dbprod.campaign_id = prod['campaign_id'] dbprod.auto_fb_post_mode = prod['auto_fb_post_mode'] dbprod.currencyCode = prod['currencycode'] dbprod.current_amount = prod['current_amount'] dbprod.goal = prod['goal'] dbprod.donators = prod['donators'] dbprod.days_active = prod['days_active'] dbprod.title = prod['title'] dbprod.description = prod['description'] dbprod.DescriptionLength = len(prod["description"]) dbprod.has_beneficiary = prod['has_beneficiary'] dbprod.user_first_name = prod['user_first_name'] dbprod.user_last_name = prod['user_last_name'] dbprod.visible_in_search = prod['visible_in_search'] dbprod.campaign_hearts = prod['campaign_hearts'] dbprod.social_share_total = prod['social_share_total'] dbprod.is_charity = prod['is_charity'] dbprod.time_of_day = prod['time_of_day'] dbprod.save()
def main(): print('start script') # read the json data with open('data.json', encoding="utf8") as json_data: data = json.load(json_data) print("opens") cats = {} concur = {} y = 1 i = 1 # find the categories, and replace them with the # new category id (created below) for prod in data: if prod['category'] not in cats: cats[prod['category']] = i i += 1 if prod['currencycode'] not in concur: concur[prod['currencycode']] = y y += 1 prod['category'] = cats[prod['category']] prod['currencycode'] = concur[prod['currencycode']] # create the categories for cat_name, cat_id in cats.items(): new_cat = Category() new_cat.id = cat_id new_cat.title = cat_name new_cat.save() for concur_code, curr_id in concur.items(): new_code = CurrencyCode() new_code.id = curr_id new_code.code = concur_code new_code.save() # create the products for prod in data: p = Campaign() p.url = prod['url'] p.campaign_id = prod['campaign_id'] if prod['auto_fb_post_mode'] == 1: p.auto_fb_post_mode = True else: p.auto_fb_post_mode = False p.current_amount = prod['current_amount'] p.category = Category.objects.get(id=prod['category']) p.currencycode = CurrencyCode.objects.get(id=prod['currencycode']) p.donators = prod['donators'] if isinstance(prod['days_active'], int): p.days_active = prod['days_active'] else: p.days_active = 0 p.title = prod['title'] p.description = prod['description'] p.default_url = prod['default_url'] if prod['has_beneficiary'] == 1: p.has_beneficiary = True else: p.has_beneficiary = False if prod['turn_off_donations'] == 1: p.turn_off_donations = True else: p.turn_off_donations = False if prod['visible_in_search'] == 1: p.visible_in_search = True else: p.visible_in_search = False if prod['status'] == 1: p.status = True else: p.status = False if prod['deactivated'] == 1: p.deactivated = True else: p.deactivated = False if prod['state'] == 1: p.state = True else: p.state = False p.campaign_image_url = prod['campaign_image_url'] if p.launch_date == '': prod['launch_date'] = '1970-01-01' p.launch_date = prod['launch_date'] p.campaign_hearts = prod['campaign_hearts'] p.social_share_total = prod['social_share_total'] if p.social_share_last_update == '': prod['social_share_last_update'] = '1970-01-01' p.social_share_last_update = prod['social_share_last_update'] p.location_city = prod['location_city'] p.location_country = prod['location_country'] if prod['location_zip'] == '': prod['location_zip'] = 0 p.location_zip = prod['location_zip'] if prod['is_charity'] == 1: p.is_charity = True else: p.is_charity = False p.charity_name = prod['charity_name'] p.riskScore = prod['riskScore'] p.riskScoreQuartile = prod['riskScoreQuartile'] p.save() print('saved')
def post(self, request, format=None): prod = request.data p = Campaign() p.url = prod['url'] p.campaign_id = prod['campaign_id'] if prod['auto_fb_post_mode'] == 1: p.auto_fb_post_mode = True else: p.auto_fb_post_mode = False p.current_amount = prod['current_amount'] p.category = Category.objects.get(title=prod['category']) p.currencycode = CurrencyCode.objects.get(code=prod['currencycode']) p.donators = prod['donators'] if isinstance(prod['days_active'], int): p.days_active = prod['days_active'] else: p.days_active = 0 p.title = prod['title'] p.description = prod['description'] p.default_url = prod['default_url'] if prod['has_beneficiary'] == 1: p.has_beneficiary = True else: p.has_beneficiary = False if prod['turn_off_donations'] == 1: p.turn_off_donations = True else: p.turn_off_donations = False if prod['visible_in_search'] == 1: p.visible_in_search = True else: p.visible_in_search = False if prod['status'] == 1: p.status = True else: p.status = False if prod['deactivated'] == 1: p.deactivated = True else: p.deactivated = False if prod['state'] == 1: p.state = True else: p.state = False p.campaign_image_url = prod['campaign_image_url'] if p.launch_date == '': prod['launch_date'] = '1970-01-01' p.launch_date = prod['launch_date'] p.campaign_hearts = prod['campaign_hearts'] p.social_share_total = prod['social_share_total'] if p.social_share_last_update == '': prod['social_share_last_update'] = '1970-01-01' p.social_share_last_update = prod['social_share_last_update'] p.location_city = prod['location_city'] p.location_country = prod['location_country'] if prod['location_zip'] == '': prod['location_zip'] = 0 p.location_zip = prod['location_zip'] if prod['is_charity'] == 1: p.is_charity = True else: p.is_charity = False p.charity_name = prod['charity_name'] #p.save() return Response({"Created"}, status=status.HTTP_201_CREATED)
def main(): import csv import json if len(Campaign.objects.all()) == 0: with open('campaigns.json') as json_file: data = json.load(json_file) campaigns = data id = 0 for p in campaigns: theCamp = Campaign() theCamp.currencycode = campaigns[p]['currencycode'] theCamp.current_amount = campaigns[p]['current_amount'] theCamp.goal = campaigns[p]['goal'] theCamp.auto_fb_post_mode = campaigns[p]['auto_fb_post_mode'] theCamp.donators = campaigns[p]['donators'] theCamp.days_active = campaigns[p]['days_active'] theCamp.title = campaigns[p]['title'] theCamp.description = campaigns[p]['description'] theCamp.has_beneficiary = campaigns[p]['has_beneficiary'] theCamp.status = campaigns[p]['status'] theCamp.deactivated = campaigns[p]['deactivated'] theCamp.campaign_hearts = campaigns[p]['campaign_hearts'] theCamp.social_share_total = campaigns[p]['social_share_total'] theCamp.location_country = campaigns[p]['location_country'] theCamp.is_charity = campaigns[p]['is_charity'] theCamp.charity_valid = campaigns[p]['charity_valid'] if campaigns[p]['donators'] != '0': avg_donation = round((int(campaigns[p]['current_amount']) / int(campaigns[p]['donators'])), 2) theCamp.avg_donation = avg_donation else: avg_donation = 0 theCamp.avg_donation = avg_donation theScore = 0 if int(campaigns[p]['donators']) < 2: theScore += 0 elif int(campaigns[p]['donators']) < 10: theScore += 0.5 elif int(campaigns[p]['donators']) < 25: theScore += 1 elif int(campaigns[p]['donators']) < 50: theScore += 1.5 elif int(campaigns[p]['donators']) < 100: theScore += 2 elif int(campaigns[p]['donators']) < 200: theScore += 2.5 elif int(campaigns[p]['donators']) < 500: theScore += 3 elif int(campaigns[p]['donators']) < 1000: theScore += 3.5 elif int(campaigns[p]['donators']) < 2000: theScore += 4 elif int(campaigns[p]['donators']) < 10000: theScore += 4.5 elif int(campaigns[p]['donators']) > 10000: theScore += 5 if int(avg_donation) < 5: theScore += 0 elif int(avg_donation) < 10: theScore += 0.5 elif int(avg_donation) < 15: theScore += 1 elif int(avg_donation) < 20: theScore += 1.5 elif int(avg_donation) < 30: theScore += 2 elif int(avg_donation) < 50: theScore += 2.5 elif int(avg_donation) < 75: theScore += 3 elif int(avg_donation) < 125: theScore += 3.5 elif int(avg_donation) < 200: theScore += 4 elif int(avg_donation) < 500: theScore += 4.5 elif int(avg_donation) > 500: theScore += 5 theCamp.c_rating = theScore theCamp.save() print(p, theScore) else: print('Already Loaded')