def handle(self, *args, **options): """ "uid","user_id","offer_id","offer_user_id","read_date","created_date","message" """ start_date = datetime.datetime.now() # Open csv file and initialize counter and other objects ifile = open('legacy_migration/csv/cargos.csv', "rU") reader = csv.reader(ifile, quotechar='"') reader.next() # Jump header row count = 0 for row in reader: count = count + 1 # Get Trip try: trip = Trip.objects.only('uid').get(uid__iexact=get_value(row[2])) except (IndexError, ValueError, Trip.DoesNotExist): with open("legacy_migration/csv/err_cargos.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue # Get User try: comment_profile = UserProfile.objects.only('uid', 'user').get(uid__iexact=get_value(row[1])) except UserProfile.DoesNotExist: with open("legacy_migration/csv/err_cargos.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue comment_user = comment_profile.user # Get or create Cargos if get_value(row[1]) == get_value(row[3]): cargos = Cargo.objects.filter(trip__uid__iexact=get_value(row[2])) else: try: cargo = Cargo.objects.get(trip__uid__iexact=get_value(row[2]), requesting_user=comment_user) except Cargo.DoesNotExist: fields = { 'trip': trip, 'requesting_user': comment_user, 'traveller_user': trip.user, } cargo = Cargo.objects.create(**fields) cargos = Cargo.objects.filter(trip__uid__iexact=get_value(row[2]), requesting_user=comment_user) # # Get or create CargoComment base_fields = { 'uid': get_value(row[0]), 'user': comment_user, 'content': get_value(row[6]), 'unread': get_value(row[4]) and False or True, 'creation_dt': get_value(row[5]), } for cargo in cargos: fields = base_fields.copy() fields.update({'cargo': cargo,}) try: comment = CargoComment.objects.get(uid=get_value(row[0]), cargo=cargo) except CargoComment.DoesNotExist: try: pass # comment = CargoComment.objects.create(**fields) except (DatabaseError, UnicodeDecodeError, AttributeError, ValidationError): with open("legacy_migration/csv/err_trips.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) from django.db import connection connection._rollback() continue print count, row[0] # Print total elapsed seconds end_date = datetime.datetime.now() delta = end_date - start_date print print "Total: ", delta.total_seconds(), "seconds" # Close csv file ifile.close()
def handle(self, *args, **options): """ "uid","email","name","lastname","language","language2","created_date","last_login","facebook_id","facebook_link","twitter_link","country" """ start_date = datetime.datetime.now() # Make sure we do not send emails settings.SEND_EMAIL_NOTIFICATIONS = False # Open csv file and initialize counter and other objects ifile = open('legacy_migration/csv/users.csv', "rb") reader = csv.reader(ifile) reader.next() # Jump header row count = 0 default_profile_country = Country.objects.latest('id') countries_dict = dict([(obj.name.lower(), obj) for obj in Country.objects.all()]) for row in reader: count = count + 1 # Get or create User try: email = get_value(row[1])[:75] except TypeError: with open("legacy_migration/csv/err_users.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue # suffix_num = str(User.objects.count()) username = '******' % (email.split('@', 1)[0], count) username = username[:30] password = '******' fields = { 'id': count, 'username': username, 'email': email, 'password': password, 'first_name': get_value(row[2])[:30], 'last_name': get_value(row[3])[:30], 'date_joined': get_value(row[6]), 'last_login': get_value(row[7]) or datetime.datetime.now(), } User.objects.create(**fields) # Update UserProfile try: country = countries_dict[row[11].lower().strip()] except KeyError: country = default_profile_country fields = { 'id': count, 'user_id': count, 'uid': get_value(row[0]), 'country': country, 'language': get_language(row[4]), 'second_language': get_language(row[5]), 'completed': True, 'email_verified': True, } UserProfile.objects.create(**fields) # Get or create UserSocialAuth if get_value(row[8]): fields = { 'user_id': count, 'provider': 'facebook', 'uid': get_value(row[8]), 'extra_data': '{"access_token": "faketoken" ,"expires": "5183999", "id": "%s"}' % get_value(row[8]), } try: UserSocialAuth.objects.get(uid=get_value(row[8])) except UserSocialAuth.DoesNotExist: UserSocialAuth.objects.create(**fields) # # Update SocialLink related objects SocialLink.objects.create(profile_id=count, pos=1, url=get_value(row[9]) or '') SocialLink.objects.create(profile_id=count, pos=2, url=get_value(row[10]) or '') SocialLink.objects.create(profile_id=count, pos=3) SocialLink.objects.create(profile_id=count, pos=4) SocialLink.objects.create(profile_id=count, pos=5) SocialLink.objects.create(profile_id=count, pos=6) SocialLink.objects.create(profile_id=count, pos=7) SocialLink.objects.create(profile_id=count, pos=8) print count, row[1] # Print total elapsed seconds end_date = datetime.datetime.now() delta = end_date - start_date print print "Total: ", delta.total_seconds(), "seconds" # Close csv file ifile.close()
def handle(self, *args, **options): """ "uid","user_id","title","language","body","created_date","transport_type","departure_date","arrival_date","departure_city","arrival_city" """ start_date = datetime.datetime.now() # Make sure we do not send emails settings.SEND_EMAIL_NOTIFICATIONS = False # Open csv file and initialize counter and other objects ifile = open("legacy_migration/csv/trips.csv", "rb") reader = csv.reader(ifile) reader.next() # Jump header row count = 0 for row in reader: count = count + 1 if len(row) < 10: with open("legacy_migration/csv/err_trips.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue # Get User try: profile = UserProfile.objects.get(uid__iexact=get_value(row[1])) except (ValueError, UserProfile.DoesNotExist): with open("legacy_migration/csv/err_trips.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue user = profile.user # get or create Trip try: dep_string = get_value(row[9]).split(",") dep_city_name = dep_string[0].strip() dep_country_name = dep_string[-1].strip() dest_string = get_value(row[10]).split(",") dest_city_name = dest_string[0].strip() dest_country_name = dest_string[-1].strip() dep_country = Country.objects.get(name_ascii__iexact=dep_country_name) dep_city = City.objects.get(name_ascii__iexact=dep_city_name, country=dep_country) dest_country = Country.objects.get(name_ascii__iexact=dest_country_name) dest_city = City.objects.get(name_ascii__iexact=dest_city_name, country=dest_country) except: with open("legacy_migration/csv/err_trips.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) # TODO continue fields = { "uid": get_value(row[0]), "user": user, "comments": get_value(row[4]), "creation_dt": get_value(row[5]), "travelling_by": get_transportation(row[6]), # 'departure_dt': get_value(row[7]) or get_value(row[8]), "destination_dt": get_value(row[8]), "dep_country": dep_country, "dep_city": dep_city, "dest_country": dest_country, "dest_city": dest_city, } try: Trip.objects.get(uid=get_value(row[0])) except Trip.DoesNotExist: try: Trip.objects.create(**fields) except (UnicodeDecodeError, AttributeError, ValidationError): with open("legacy_migration/csv/err_trips.txt", "a") as myfile: myfile.write("%s\n" % repr(row)) continue print count, row[2] # Print total elapsed seconds end_date = datetime.datetime.now() delta = end_date - start_date print print "Total: ", delta.total_seconds(), "seconds" # Close csv file ifile.close()