From 4089a78d1cd7cbe960616860385a6a933eb60ae2 Mon Sep 17 00:00:00 2001 From: Ayaan Ahmad <76241848+ayaan278@users.noreply.github.com> Date: Tue, 5 Apr 2022 20:55:21 +0400 Subject: [PATCH 1/4] First import successful --- CsvImport/__init__.py | 0 CsvImport/admin.py | 33 +++++++++++++++++++ CsvImport/apps.py | 6 ++++ CsvImport/migrations/0001_initial.py | 22 +++++++++++++ CsvImport/migrations/__init__.py | 0 CsvImport/models.py | 10 ++++++ CsvImport/tests.py | 3 ++ CsvImport/views.py | 3 ++ codershq/challenge/admin.py | 1 - codershq/challenge/models.py | 1 - codershq/dashboard/models.py | 0 codershq/portfolio/admin.py | 1 - codershq/portfolio/models.py | 1 - codershq/static/images/favicon.svg | 2 +- codershq/users/admin.py | 4 +-- .../django/tempCodeRunnerFile.shellscript | 1 + config/settings/base.py | 1 + config/settings/local.py | 2 +- 18 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 CsvImport/__init__.py create mode 100644 CsvImport/admin.py create mode 100644 CsvImport/apps.py create mode 100644 CsvImport/migrations/0001_initial.py create mode 100644 CsvImport/migrations/__init__.py create mode 100644 CsvImport/models.py create mode 100644 CsvImport/tests.py create mode 100644 CsvImport/views.py create mode 100644 codershq/dashboard/models.py create mode 100644 compose/local/django/tempCodeRunnerFile.shellscript diff --git a/CsvImport/__init__.py b/CsvImport/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/CsvImport/admin.py b/CsvImport/admin.py new file mode 100644 index 00000000..2314f72c --- /dev/null +++ b/CsvImport/admin.py @@ -0,0 +1,33 @@ +from django.contrib import admin +from django.http import HttpResponse +from .models import getInfo +# from django.contrib import messages +import csv + +# Register your models here. + +class SaveInfo(admin.ModelAdmin): + + actions = ['export', ] + + def export(self,request,queryset): + meta = self.model._meta + + fieldnames = [field.name for field in meta.fields] + + response = HttpResponse(content_type='text/csv') + + response['Content-Disposition'] = 'attachment; filename="Responses.csv"' + + writer = csv.writer(response) + + writer.writerow(fieldnames) #heading in csv files + + for obj in queryset: + row = writer.writerow([getattr(obj, field) for field in fieldnames]) + + return response + + export.short_description ="Export to csv" + +admin.site.register(getInfo,SaveInfo) diff --git a/CsvImport/apps.py b/CsvImport/apps.py new file mode 100644 index 00000000..e8c8e78e --- /dev/null +++ b/CsvImport/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CsvimportConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'CsvImport' diff --git a/CsvImport/migrations/0001_initial.py b/CsvImport/migrations/0001_initial.py new file mode 100644 index 00000000..624425d8 --- /dev/null +++ b/CsvImport/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.11 on 2022-04-05 16:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='getInfo', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('email', models.EmailField(max_length=100)), + ], + ), + ] diff --git a/CsvImport/migrations/__init__.py b/CsvImport/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/CsvImport/models.py b/CsvImport/models.py new file mode 100644 index 00000000..dc81eccd --- /dev/null +++ b/CsvImport/models.py @@ -0,0 +1,10 @@ +from django.db import models +# Create your models here. +class getInfo(models.Model): + + object = None + name = models.CharField(max_length=100) + email = models.EmailField(max_length=100) + + def __str__(self): + return self.name diff --git a/CsvImport/tests.py b/CsvImport/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/CsvImport/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/CsvImport/views.py b/CsvImport/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/CsvImport/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/codershq/challenge/admin.py b/codershq/challenge/admin.py index 5ac8d0be..5d5c7762 100644 --- a/codershq/challenge/admin.py +++ b/codershq/challenge/admin.py @@ -1,5 +1,4 @@ from django.contrib import admin - from codershq.challenge.models import Challenge admin.site.register(Challenge) diff --git a/codershq/challenge/models.py b/codershq/challenge/models.py index 93682d92..06a9db59 100644 --- a/codershq/challenge/models.py +++ b/codershq/challenge/models.py @@ -7,7 +7,6 @@ from codershq.users.models import User - class Challenge(models.Model): """Main challenge model""" diff --git a/codershq/dashboard/models.py b/codershq/dashboard/models.py new file mode 100644 index 00000000..e69de29b diff --git a/codershq/portfolio/admin.py b/codershq/portfolio/admin.py index 846f6b40..e69de29b 100644 --- a/codershq/portfolio/admin.py +++ b/codershq/portfolio/admin.py @@ -1 +0,0 @@ -# Register your models here. diff --git a/codershq/portfolio/models.py b/codershq/portfolio/models.py index 6b202199..e69de29b 100644 --- a/codershq/portfolio/models.py +++ b/codershq/portfolio/models.py @@ -1 +0,0 @@ -# Create your models here. diff --git a/codershq/static/images/favicon.svg b/codershq/static/images/favicon.svg index 25180256..b022ddb9 100644 --- a/codershq/static/images/favicon.svg +++ b/codershq/static/images/favicon.svg @@ -1 +1 @@ - + \ No newline at end of file diff --git a/codershq/users/admin.py b/codershq/users/admin.py index be60fdcf..b69bbe77 100644 --- a/codershq/users/admin.py +++ b/codershq/users/admin.py @@ -2,15 +2,15 @@ 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 - User = get_user_model() @admin.register(User) class UserAdmin(auth_admin.UserAdmin): + form = UserChangeForm add_form = UserCreationForm fieldsets = ( diff --git a/compose/local/django/tempCodeRunnerFile.shellscript b/compose/local/django/tempCodeRunnerFile.shellscript new file mode 100644 index 00000000..0a6d64fd --- /dev/null +++ b/compose/local/django/tempCodeRunnerFile.shellscript @@ -0,0 +1 @@ +python manage.py runserver_plus 0.0.0.0:8000 \ No newline at end of file diff --git a/config/settings/base.py b/config/settings/base.py index 486cc4b8..9871dad8 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -55,6 +55,7 @@ # APPS # ------------------------------------------------------------------------------ DJANGO_APPS = [ + "CsvImport", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", diff --git a/config/settings/local.py b/config/settings/local.py index 8484f911..5ffef4a5 100644 --- a/config/settings/local.py +++ b/config/settings/local.py @@ -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 From d618bf49df6b9ed1f3fbbe0d7965fb8463cc37c9 Mon Sep 17 00:00:00 2001 From: Ayaan Ahmad <76241848+ayaan278@users.noreply.github.com> Date: Tue, 5 Apr 2022 21:38:30 +0400 Subject: [PATCH 2/4] Implemented in users --- CsvImport/__init__.py | 0 CsvImport/admin.py | 33 ------------------- CsvImport/apps.py | 6 ---- CsvImport/migrations/0001_initial.py | 22 ------------- CsvImport/migrations/__init__.py | 0 CsvImport/models.py | 10 ------ CsvImport/tests.py | 3 -- CsvImport/views.py | 3 -- codershq/challenge/admin.py | 1 + .../templates/users/admin/change_list.html | 7 ++++ codershq/users/admin.py | 28 ++++++++++++++++ codershq/users/models.py | 1 + config/settings/base.py | 1 - 13 files changed, 37 insertions(+), 78 deletions(-) delete mode 100644 CsvImport/__init__.py delete mode 100644 CsvImport/admin.py delete mode 100644 CsvImport/apps.py delete mode 100644 CsvImport/migrations/0001_initial.py delete mode 100644 CsvImport/migrations/__init__.py delete mode 100644 CsvImport/models.py delete mode 100644 CsvImport/tests.py delete mode 100644 CsvImport/views.py create mode 100644 codershq/templates/users/admin/change_list.html diff --git a/CsvImport/__init__.py b/CsvImport/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/CsvImport/admin.py b/CsvImport/admin.py deleted file mode 100644 index 2314f72c..00000000 --- a/CsvImport/admin.py +++ /dev/null @@ -1,33 +0,0 @@ -from django.contrib import admin -from django.http import HttpResponse -from .models import getInfo -# from django.contrib import messages -import csv - -# Register your models here. - -class SaveInfo(admin.ModelAdmin): - - actions = ['export', ] - - def export(self,request,queryset): - meta = self.model._meta - - fieldnames = [field.name for field in meta.fields] - - response = HttpResponse(content_type='text/csv') - - response['Content-Disposition'] = 'attachment; filename="Responses.csv"' - - writer = csv.writer(response) - - writer.writerow(fieldnames) #heading in csv files - - for obj in queryset: - row = writer.writerow([getattr(obj, field) for field in fieldnames]) - - return response - - export.short_description ="Export to csv" - -admin.site.register(getInfo,SaveInfo) diff --git a/CsvImport/apps.py b/CsvImport/apps.py deleted file mode 100644 index e8c8e78e..00000000 --- a/CsvImport/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class CsvimportConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'CsvImport' diff --git a/CsvImport/migrations/0001_initial.py b/CsvImport/migrations/0001_initial.py deleted file mode 100644 index 624425d8..00000000 --- a/CsvImport/migrations/0001_initial.py +++ /dev/null @@ -1,22 +0,0 @@ -# Generated by Django 3.2.11 on 2022-04-05 16:44 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ] - - operations = [ - migrations.CreateModel( - name='getInfo', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=100)), - ('email', models.EmailField(max_length=100)), - ], - ), - ] diff --git a/CsvImport/migrations/__init__.py b/CsvImport/migrations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/CsvImport/models.py b/CsvImport/models.py deleted file mode 100644 index dc81eccd..00000000 --- a/CsvImport/models.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.db import models -# Create your models here. -class getInfo(models.Model): - - object = None - name = models.CharField(max_length=100) - email = models.EmailField(max_length=100) - - def __str__(self): - return self.name diff --git a/CsvImport/tests.py b/CsvImport/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/CsvImport/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/CsvImport/views.py b/CsvImport/views.py deleted file mode 100644 index 91ea44a2..00000000 --- a/CsvImport/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here. diff --git a/codershq/challenge/admin.py b/codershq/challenge/admin.py index 5d5c7762..bcee2f1c 100644 --- a/codershq/challenge/admin.py +++ b/codershq/challenge/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin +from django.http import HttpResponse from codershq.challenge.models import Challenge admin.site.register(Challenge) diff --git a/codershq/templates/users/admin/change_list.html b/codershq/templates/users/admin/change_list.html new file mode 100644 index 00000000..2433f577 --- /dev/null +++ b/codershq/templates/users/admin/change_list.html @@ -0,0 +1,7 @@ +{% extends "admin/change_list.html" %} + + {{ block.super }} + +{% block object-tools %} + +{% endblock %} \ No newline at end of file diff --git a/codershq/users/admin.py b/codershq/users/admin.py index b69bbe77..57f85e46 100644 --- a/codershq/users/admin.py +++ b/codershq/users/admin.py @@ -4,6 +4,8 @@ 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() @@ -33,3 +35,29 @@ class UserAdmin(auth_admin.UserAdmin): ) list_display = ["username", "name", "is_superuser"] search_fields = ["name"] + + # change_list_template = '/templates/users/admin/change_list.html' + actions = ['export', ] + + 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') + + response['Content-Disposition'] = 'attachment; filename="UsersData.csv"' + + writer = csv.writer(response) + + writer.writerow(fieldnames) #heading in csv files + + for obj in queryset: + row = writer.writerow([getattr(obj, field) for field in fieldnames]) + + return response + + export.short_description ="Export to csv" diff --git a/codershq/users/models.py b/codershq/users/models.py index 6775d155..e8112e1a 100644 --- a/codershq/users/models.py +++ b/codershq/users/models.py @@ -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) + diff --git a/config/settings/base.py b/config/settings/base.py index 9871dad8..486cc4b8 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -55,7 +55,6 @@ # APPS # ------------------------------------------------------------------------------ DJANGO_APPS = [ - "CsvImport", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", From 57fc06923c269d286135c13d08f1376fcc46f893 Mon Sep 17 00:00:00 2001 From: Ayaan Ahmad <76241848+ayaan278@users.noreply.github.com> Date: Sat, 16 Apr 2022 20:38:17 +0400 Subject: [PATCH 3/4] some fixes --- codershq/templates/users/admin/change_list.html | 7 ------- codershq/users/admin.py | 11 +++++++---- codershq/users/templates/admin/change_list.html | 14 ++++++++++++++ config/settings/base.py | 4 +++- 4 files changed, 24 insertions(+), 12 deletions(-) delete mode 100644 codershq/templates/users/admin/change_list.html create mode 100644 codershq/users/templates/admin/change_list.html diff --git a/codershq/templates/users/admin/change_list.html b/codershq/templates/users/admin/change_list.html deleted file mode 100644 index 2433f577..00000000 --- a/codershq/templates/users/admin/change_list.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "admin/change_list.html" %} - - {{ block.super }} - -{% block object-tools %} - -{% endblock %} \ No newline at end of file diff --git a/codershq/users/admin.py b/codershq/users/admin.py index 57f85e46..1d9156ce 100644 --- a/codershq/users/admin.py +++ b/codershq/users/admin.py @@ -1,3 +1,4 @@ +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 @@ -12,7 +13,7 @@ @admin.register(User) class UserAdmin(auth_admin.UserAdmin): - + actions = ['export', ] form = UserChangeForm add_form = UserCreationForm fieldsets = ( @@ -36,8 +37,11 @@ class UserAdmin(auth_admin.UserAdmin): list_display = ["username", "name", "is_superuser"] search_fields = ["name"] - # change_list_template = '/templates/users/admin/change_list.html' - actions = ['export', ] + # def get_urls(self): + # urls = super(UserAdmin, self).get_urls() + # # new_urls =[path('exportcsv/',self.export),] + # # return new_urls + urls + def export(self,request,queryset): meta = self.model._meta @@ -59,5 +63,4 @@ def export(self,request,queryset): row = writer.writerow([getattr(obj, field) for field in fieldnames]) return response - export.short_description ="Export to csv" diff --git a/codershq/users/templates/admin/change_list.html b/codershq/users/templates/admin/change_list.html new file mode 100644 index 00000000..2f5ec263 --- /dev/null +++ b/codershq/users/templates/admin/change_list.html @@ -0,0 +1,14 @@ +{% extends "admin/change_list.html" %} + + {{ block.super }} +{% block extrastyle %} + +{% endblock %} +{% block object-tools %} +
+{% endblock %} \ No newline at end of file diff --git a/config/settings/base.py b/config/settings/base.py index 486cc4b8..82f6e48e 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -4,6 +4,8 @@ from pathlib import Path import environ +import os + ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent # codershq/ @@ -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": { From 8ec916837c565f94b7dd21ab5e418d982a5a3e28 Mon Sep 17 00:00:00 2001 From: Ayaan Ahmad <76241848+ayaan278@users.noreply.github.com> Date: Mon, 25 Apr 2022 05:44:53 +0400 Subject: [PATCH 4/4] finalized action --- codershq/users/admin.py | 14 ++++++-------- codershq/users/templates/admin/change_list.html | 14 -------------- config/settings/base.py | 2 +- 3 files changed, 7 insertions(+), 23 deletions(-) delete mode 100644 codershq/users/templates/admin/change_list.html diff --git a/codershq/users/admin.py b/codershq/users/admin.py index 1d9156ce..5e38bd4d 100644 --- a/codershq/users/admin.py +++ b/codershq/users/admin.py @@ -9,7 +9,6 @@ User = get_user_model() - @admin.register(User) class UserAdmin(auth_admin.UserAdmin): @@ -37,12 +36,7 @@ class UserAdmin(auth_admin.UserAdmin): list_display = ["username", "name", "is_superuser"] search_fields = ["name"] - # def get_urls(self): - # urls = super(UserAdmin, self).get_urls() - # # new_urls =[path('exportcsv/',self.export),] - # # return new_urls + urls - - + def export(self,request,queryset): meta = self.model._meta @@ -53,14 +47,18 @@ def export(self,request,queryset): 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" diff --git a/codershq/users/templates/admin/change_list.html b/codershq/users/templates/admin/change_list.html deleted file mode 100644 index 2f5ec263..00000000 --- a/codershq/users/templates/admin/change_list.html +++ /dev/null @@ -1,14 +0,0 @@ -{% extends "admin/change_list.html" %} - - {{ block.super }} -{% block extrastyle %} - -{% endblock %} -{% block object-tools %} - -{% endblock %} \ No newline at end of file diff --git a/config/settings/base.py b/config/settings/base.py index 82f6e48e..249e56cd 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -180,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"), os.path.join(APPS_DIR, "users/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": {