예제 #1
0
    def download_csv(self, request, queryset):
        pseudo_buffer = Echo()
        writer = csv.writer(pseudo_buffer)

        query = dict()

        filter_type = request.GET.get('type__exact', None)
        b = request.POST
        for key in self.list_filter:
            field_filter = '{}__exact'.format(key)
            if field_filter in request.GET.keys():
                query[field_filter] = request.GET.get(field_filter)
        print(request.GET)

        report_header = [
            "First Name", "Last Name", "E-mail", "Phone Number", "User Type",
            "Company", "Country", "City", "Street", "Plot Number", "ZIP Code",
            "Postal Address", "VAT Number"
        ]

        if filter_type != str(USER_TYPE_DEVELOPER):
            report_header.append("Company Reg. Number")

        report_rows = [report_header]
        for user in self.model.objects.filter(**query):
            phone_number = user.profile and user.profile.phone_number or ""
            user_info = [
                user.first_name and user.first_name.encode('utf-8') or '',
                user.last_name and user.last_name.encode('utf-8') or '',
                user.email,
                "=\"%s\"" % phone_number,
                user.display_type,
                user.profile and user.profile.company or "",
                user.profile and user.profile.country_name or "",
                user.profile and user.profile.city_name or "",
                user.profile and user.profile.street or "",
                user.profile and user.profile.plot_number or "",
                user.profile and user.profile.postal_code or "",
                user.profile and user.profile.postal_address or "",
                user.profile and user.profile.vat_number or "",
            ]
            if filter_type != str(USER_TYPE_DEVELOPER):
                user_info.append(user.profile and user.profile.company_reg_no
                                 or "")
            report_rows.append(user_info)

        file_suffix = "users"
        if filter_type == str(USER_TYPE_DEVELOPER):
            file_suffix = "developers"
        elif filter_type == str(USER_TYPE_PROJECT_OWNER):
            file_suffix = "clients"

        response = StreamingHttpResponse(
            (writer.writerow(row) for row in report_rows),
            content_type="text/csv")
        response[
            'Content-Disposition'] = 'attachment; filename=tunga_%s.csv' % file_suffix
        return response
예제 #2
0
    def download_csv(self, request, queryset):
        pseudo_buffer = Echo()
        writer = csv.writer(pseudo_buffer)

        report_header = ["E-mail"]

        report_rows = [report_header]
        for user in queryset:
            if TungaUser.objects.filter(email=user.email).count() == 0:
                user_info = [
                    user.email
                ]
                report_rows.append(user_info)

        response = StreamingHttpResponse((writer.writerow(row) for row in report_rows), content_type="text/csv")
        response['Content-Disposition'] = 'attachment; filename=tunga_email_visitors.csv'
        return response