Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import csv done in action format #189

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codershq/challenge/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin

from django.http import HttpResponse
from codershq.challenge.models import Challenge

admin.site.register(Challenge)
1 change: 0 additions & 1 deletion codershq/challenge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from codershq.users.models import User


class Challenge(models.Model):
"""Main challenge model"""

Expand Down
Empty file added codershq/dashboard/models.py
Empty file.
1 change: 0 additions & 1 deletion codershq/portfolio/admin.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# Register your models here.
1 change: 0 additions & 1 deletion codershq/portfolio/models.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# Create your models here.
2 changes: 1 addition & 1 deletion codershq/static/images/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions codershq/users/admin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from os import path
from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _

from django.http import HttpResponse
from codershq.users.forms import UserChangeForm, UserCreationForm
import csv

User = get_user_model()


@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):

actions = ['export', ]
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (
Expand All @@ -33,3 +35,30 @@ class UserAdmin(auth_admin.UserAdmin):
)
list_display = ["username", "name", "is_superuser"]
search_fields = ["name"]


def export(self,request,queryset):
meta = self.model._meta

# field names to be exported
# use [field.name for field in meta.fields] to import all the data of specific user
# here it only uploads the name and email
fieldnames = ["username","email"]

response = HttpResponse(content_type='text/csv')

# name of the file
response['Content-Disposition'] = 'attachment; filename="UsersData.csv"'

# initialize the writer to write the responses in csv
writer = csv.writer(response)

writer.writerow(fieldnames) #heading in csv files

# write each data in the csv file
for obj in queryset:
row = writer.writerow([getattr(obj, field) for field in fieldnames])

return response
# short description of the of action name
export.short_description ="Export to csv"
1 change: 1 addition & 0 deletions codershq/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ class UserTrophyType(models.Model):
class UserTrophyRecord(models.Model):
trophy_type = models.ForeignKey(UserTrophyType, on_delete=models.PROTECT)
user = models.ForeignKey(User, on_delete=models.CASCADE)

1 change: 1 addition & 0 deletions compose/local/django/tempCodeRunnerFile.shellscript
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python manage.py runserver_plus 0.0.0.0:8000
4 changes: 3 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pathlib import Path

import environ
import os


ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
# codershq/
Expand Down Expand Up @@ -178,7 +180,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
"BACKEND": "django.template.backends.django.DjangoTemplates",
# https://docs.djangoproject.com/en/dev/ref/settings/#dirs
"DIRS": [str(APPS_DIR / "templates")],
"DIRS": [str(APPS_DIR / "templates"),], #os.path.join(APPS_DIR, "users/templates"),],
# https://docs.djangoproject.com/en/dev/ref/settings/#app-dirs
"APP_DIRS": False,
"OPTIONS": {
Expand Down
2 changes: 1 addition & 1 deletion config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
default="LOXGprvKQt8D1LFcAHxTpeUNpvFQwXcEfbBpAaxqYXXaWbRsCY98415cwiQOJOAm",
)
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
GITHUB_TOKEN = env.str("GITHUB_TOKEN", "")

# CACHES
Expand Down