'this function generates the csv file'

def generate_registration_codes_csv(self, orderitems, site_name):

    course_names = []
    csv_file = StringIO.StringIO()
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['Course    Name', 'Registration    Code', 'URL'])
    for item in orderitems:
        course_id = item.course_id
        course = get_course_by_id(item.course_id, depth=0)
        registration_codes = CourseRegistrationCode.objects.filter(course_id=course_id, order=self)
        course_names.append(course.display_name)
        for registration_code in registration_codes:
            redemption_url = reverse('register_code_redemption', args=[registration_code.code])
            url = '{base_url}{redemption_url}'.format(base_url=site_name, redemption_url=redemption_url)
            csv_writer.writerow([unicode(course.display_name).encode('utf-8'), registration_code.code, url])
    return (csv_file, course_names)
