From f675a43f944fecd9a5565d9c8c06d5d2859383c1 Mon Sep 17 00:00:00 2001 From: Mark Hamblin Date: Sun, 11 Jul 2021 00:39:34 +0200 Subject: [PATCH 1/2] refactor company app - remove model fields, move form, and related template changes --- .gitignore | 1 + company/forms.py | 10 +++ company/migrations/0001_initial.py | 38 +++++---- company/migrations/0002_auto_20210506_1337.py | 79 ------------------- .../0003_alter_company_phonenumber.py | 26 ------ .../0004_alter_company_phonenumber.py | 18 ----- company/migrations/0005_auto_20210518_1126.py | 36 --------- company/models.py | 77 +++++------------- .../templates/company/companyoverview.html | 14 ++-- .../company/detailViewEditUpdateProfile.html | 18 ++--- company/views.py | 13 +-- event/templates/event/index.html | 14 ++-- 12 files changed, 74 insertions(+), 270 deletions(-) create mode 100644 company/forms.py delete mode 100644 company/migrations/0002_auto_20210506_1337.py delete mode 100644 company/migrations/0003_alter_company_phonenumber.py delete mode 100644 company/migrations/0004_alter_company_phonenumber.py delete mode 100644 company/migrations/0005_auto_20210518_1126.py diff --git a/.gitignore b/.gitignore index b9efc75..8ce6d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Our stuff... djangodenmark/settings/local.py +djangodenmark/media # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/company/forms.py b/company/forms.py new file mode 100644 index 0000000..2c877f5 --- /dev/null +++ b/company/forms.py @@ -0,0 +1,10 @@ +from django.forms import ModelForm + +from .models import Company + + +class CompanyForm(ModelForm): + class Meta: + model = Company + exclude = ("user", "created_at") + fields = "__all__" diff --git a/company/migrations/0001_initial.py b/company/migrations/0001_initial.py index e7731ea..53a3aa0 100644 --- a/company/migrations/0001_initial.py +++ b/company/migrations/0001_initial.py @@ -1,12 +1,10 @@ -# Generated by Django 3.2 on 2021-05-18 11:26 +# Generated by Django 3.2.5 on 2021-07-10 21:44 import django.core.validators import django.db.models.deletion from django.conf import settings from django.db import migrations from django.db import models -import company.models - class Migration(migrations.Migration): @@ -29,14 +27,21 @@ class Migration(migrations.Migration): verbose_name="ID", ), ), - ("logoImage", models.ImageField(null=True, upload_to="images/")), - ("companyName", models.CharField(max_length=145)), - ("description", models.TextField()), ( - "websiteURL", + "logo_image", + models.ImageField( + null=True, upload_to="images/", verbose_name="Company logo" + ), + ), + ( + "company_name", + models.CharField(max_length=145, verbose_name="Company name"), + ), + ("description", models.TextField(verbose_name="Describe your company")), + ( + "website_url", models.URLField( blank=True, - max_length=100, validators=[ django.core.validators.RegexValidator( code="invalid_url", @@ -44,21 +49,20 @@ class Migration(migrations.Migration): regex="[http]", ) ], + verbose_name="Link to your website", ), ), - ("relationToDjango", models.TextField()), ( - "phoneNumber", + "relation_to_django", + models.TextField(verbose_name="Your relation to Django"), + ), + ("email", models.EmailField(max_length=100)), + ( + "main_contact", models.CharField( - max_length=15, validators=[company.models.only_int] + max_length=100, verbose_name="The main point of contact" ), ), - ("email", models.EmailField(max_length=100)), - ("mainContact", models.CharField(max_length=50)), - ("streetName", models.CharField(max_length=45)), - ("houseNumber", models.IntegerField()), - ("postalCode", models.IntegerField()), - ("region", models.CharField(max_length=45)), ("created_at", models.DateTimeField(auto_now_add=True)), ( "user", diff --git a/company/migrations/0002_auto_20210506_1337.py b/company/migrations/0002_auto_20210506_1337.py deleted file mode 100644 index 8d56b21..0000000 --- a/company/migrations/0002_auto_20210506_1337.py +++ /dev/null @@ -1,79 +0,0 @@ -# Generated by Django 3.2 on 2021-05-06 13:37 -import django.core.validators -import django.db.models.deletion -from django.conf import settings -from django.db import migrations -from django.db import models - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ("company", "0001_initial"), - ] - - operations = [ - migrations.AddField( - model_name="company", - name="logoImage", - field=models.ImageField(null=True, upload_to="images/"), - ), - migrations.AddField( - model_name="company", - name="user", - field=models.ForeignKey( - default=None, - on_delete=django.db.models.deletion.CASCADE, - to=settings.AUTH_USER_MODEL, - ), - ), - migrations.AlterField( - model_name="company", - name="email", - field=models.EmailField(max_length=100), - ), - migrations.AlterField( - model_name="company", - name="houseNumber", - field=models.IntegerField(), - ), - migrations.AlterField( - model_name="company", - name="mainContact", - field=models.CharField(max_length=50), - ), - migrations.AlterField( - model_name="company", - name="phoneNumber", - field=models.CharField( - blank=True, - max_length=17, - validators=[ - django.core.validators.RegexValidator( - message="fejl", regex="^\\+?1?\\d{9,15}$" - ) - ], - ), - ), - migrations.AlterField( - model_name="company", - name="postalCode", - field=models.IntegerField(), - ), - migrations.AlterField( - model_name="company", - name="region", - field=models.CharField(max_length=45), - ), - migrations.AlterField( - model_name="company", - name="streetName", - field=models.CharField(max_length=45), - ), - migrations.AlterField( - model_name="company", - name="websiteURL", - field=models.CharField(max_length=100), - ), - ] diff --git a/company/migrations/0003_alter_company_phonenumber.py b/company/migrations/0003_alter_company_phonenumber.py deleted file mode 100644 index 85684e6..0000000 --- a/company/migrations/0003_alter_company_phonenumber.py +++ /dev/null @@ -1,26 +0,0 @@ -# Generated by Django 3.2.2 on 2021-05-07 13:23 -import django.core.validators -from django.db import migrations -from django.db import models - - -class Migration(migrations.Migration): - - dependencies = [ - ("company", "0002_auto_20210506_1337"), - ] - - operations = [ - migrations.AlterField( - model_name="company", - name="phoneNumber", - field=models.CharField( - max_length=17, - validators=[ - django.core.validators.RegexValidator( - message="Det er forkert", regex="^\\+?1?\\d{9,15}$" - ) - ], - ), - ), - ] diff --git a/company/migrations/0004_alter_company_phonenumber.py b/company/migrations/0004_alter_company_phonenumber.py deleted file mode 100644 index f71284b..0000000 --- a/company/migrations/0004_alter_company_phonenumber.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.2.2 on 2021-05-07 13:27 -from django.db import migrations -from django.db import models - - -class Migration(migrations.Migration): - - dependencies = [ - ("company", "0003_alter_company_phonenumber"), - ] - - operations = [ - migrations.AlterField( - model_name="company", - name="phoneNumber", - field=models.CharField(max_length=15), - ), - ] diff --git a/company/migrations/0005_auto_20210518_1126.py b/company/migrations/0005_auto_20210518_1126.py deleted file mode 100644 index c5a2eaf..0000000 --- a/company/migrations/0005_auto_20210518_1126.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 3.2 on 2021-05-18 11:26 -import django.core.validators -from django.db import migrations -from django.db import models - -import company.models - - -class Migration(migrations.Migration): - - dependencies = [ - ("company", "0004_alter_company_phonenumber"), - ] - - operations = [ - migrations.AlterField( - model_name="company", - name="phoneNumber", - field=models.CharField(max_length=15, validators=[company.models.only_int]), - ), - migrations.AlterField( - model_name="company", - name="websiteURL", - field=models.URLField( - blank=True, - max_length=100, - validators=[ - django.core.validators.RegexValidator( - code="invalid_url", - message="Website URL must include https:// or http://", - regex="[http]", - ) - ], - ), - ), - ] diff --git a/company/models.py b/company/models.py index 7ad55e2..b3537f9 100644 --- a/company/models.py +++ b/company/models.py @@ -1,24 +1,20 @@ from django.contrib.auth.models import User -from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.db import models -from django.forms import ModelForm - - -def only_int(value): - """function for valid phonenumber only contains numbers""" - if not value.isdigit(): - raise ValidationError("Phone number must only contain numbers.") class Company(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, default=None) - logoImage = models.ImageField(upload_to="images/", null=True) - companyName = models.CharField(max_length=145) - description = models.TextField() - websiteURL = models.URLField( - max_length=100, + logo_image = models.ImageField( + upload_to="images/", + null=True, + verbose_name="Company logo", + ) + company_name = models.CharField(max_length=145, verbose_name="Company name") + description = models.TextField(verbose_name="Describe your company") + website_url = models.URLField( + max_length=200, blank=True, validators=[ RegexValidator( @@ -27,54 +23,17 @@ class Company(models.Model): code="invalid_url", ), ], + verbose_name="Link to your website", + ) + relation_to_django = models.TextField( + verbose_name="Your relation to Django", ) - relationToDjango = models.TextField() - phoneNumber = models.CharField(max_length=15, blank=False, validators=[only_int]) email = models.EmailField(max_length=100) - mainContact = models.CharField(max_length=50) - streetName = models.CharField(max_length=45) - houseNumber = models.IntegerField() - postalCode = models.IntegerField() - region = models.CharField(max_length=45) + main_contact = models.CharField( + max_length=100, + verbose_name="The main point of contact", + ) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): - return "{} {} {} {} {} {} {} {} {} {} {} {} {}".format( - self.user, - self.companyName, - self.description, - self.websiteURL, - self.relationToDjango, - self.phoneNumber, - self.email, - self.mainContact, - self.streetName, - self.houseNumber, - self.postalCode, - self.region, - self.created_at, - ) - - -class CompanyForm(ModelForm): - def init(self, args, **kwargs): - super(CompanyForm, self).__init__(args, **kwargs) - - class Meta: - model = Company - exclude = ("user", "created_at") - fields = "__all__" - labels = { - "email": ("Email"), - "companyName": ("Company name"), - "logoImage": ("Company logo"), - "description": ("Describe your company"), - "websiteURL": ("Link to your website"), - "relationToDjango": ("Your relation to Django"), - "phoneNumber": ("Phone number"), - "mainContact": ("The main point of contact"), - "streetName": ("Street name"), - "houseNumber": ("House number"), - "postalCode": ("Postal code"), - "region": ("Region"), - } + return self.company_name diff --git a/company/templates/company/companyoverview.html b/company/templates/company/companyoverview.html index ed0f1ff..bca85b2 100644 --- a/company/templates/company/companyoverview.html +++ b/company/templates/company/companyoverview.html @@ -7,9 +7,9 @@

Companies that use Django

{% for Company in company_list %} {% comment %} Each company is shown in the form of a bootstrap card {% endcomment %}
- +
-
{{ Company.companyName }}
+
{{ Company.company_name }}

{{ Company.description }}

@@ -17,24 +17,22 @@
{{ Company.companyName }}