Notes
![]() ![]() Notes - notes.io |
from django.http import HttpResponse
from django.urls import path
from django.shortcuts import redirect
class CustomUserAdmin(admin.ModelAdmin):
actions = ['import_users_from_excel', 'export_users_to_csv']
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('export_users/', self.admin_site.admin_view(self.export_users_view), name='export_users'),
path('import_users/', self.admin_site.admin_view(self.upload_file_view), name='import_users')
]
return custom_urls + urls
# Action for redirecting to the custom export URL
def export_users_to_csv(self, request, queryset):
return redirect('admin:export_users')
# Custom view for exporting users to CSV
def export_users_view(self, request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="users.csv"'
writer = csv.writer(response)
# Write the header row
writer.writerow(['First Name', 'Last Name', 'Email', 'Phone', 'Address'])
# Retrieve all users or a filtered queryset if necessary
users = User.objects.all() # You can modify this as per your requirements
# Write the user data
for user in users:
writer.writerow([user.first_name, user.last_name, user.email, user.phone, user.address])
self.message_user(request, "Users exported successfully.", messages.SUCCESS)
return response
def upload_file_view(self, request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['file']
try:
df = pd.read_excel(file)
for index, row in df.iterrows():
email = row.get('email', '')
first_name = row.get('first_name', '')
last_name = row.get('last_name', '')
phone = row.get('phone', '')
address = row.get('address', '')
if email:
user, created = User.objects.update_or_create(
email=email,
defaults={
'first_name': first_name,
'last_name': last_name,
'phone': phone,
'address': address,
}
)
if created:
user.set_password(User.objects.make_random_password())
user.save()
self.message_user(request, "User Imported Successfully", messages.SUCCESS)
return redirect("..")
except pd.errors.EmptyDataError:
self.message_user(request, 'The uploaded file is empty', messages.ERROR)
except Exception as e:
self.message_user(request, f"An error occurred: {e}", messages.ERROR)
else:
self.message_user(request, "Form data is not valid")
else:
form = UploadFileForm()
return render(request, 'admin/import_users.html', {'form': form})
import_users_from_excel.short_description = "Import users from Excel"
export_users_to_csv.short_description = "Export users to CSV"
list_display = ('first_name', 'email', 'phone', 'address')
search_fields = ('first_name', 'email')
list_filter = ('address',)
list_editable = ('phone', 'address')
fieldsets = (
(None, {
"fields": ('email', 'password')
}),
('Personal info', {
"fields": ('first_name', 'last_name', 'phone', 'address')
}),
('Permissions', {
"fields": ('is_active', 'is_staff', 'is_superuser', 'user_permissions', 'groups')
}),
('Important dates', {
"fields": ('last_login', 'date_joined')
})
)
admin.site.register(User, CustomUserAdmin)
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team