diff --git a/lostplaces/lostplaces/settings.py b/lostplaces/lostplaces/settings.py
index f69bacb..23940fb 100644
--- a/lostplaces/lostplaces/settings.py
+++ b/lostplaces/lostplaces/settings.py
@@ -131,9 +131,6 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
-# Use custom user model
-AUTH_USER_MODEL = 'lostplaces_app.Explorer'
-
# Templates to use for authentication
LOGIN_URL = reverse_lazy('login')
LOGIN_REDIRECT_URL = reverse_lazy('lostplaces_home')
diff --git a/lostplaces/lostplaces_app/admin.py b/lostplaces/lostplaces_app/admin.py
index 11e082a..5d53ab7 100644
--- a/lostplaces/lostplaces_app/admin.py
+++ b/lostplaces/lostplaces_app/admin.py
@@ -12,17 +12,11 @@ from .forms import ExplorerCreationForm, ExplorerChangeForm
# Register your models here.
-class ExplorerAdmin(UserAdmin):
- add_form = ExplorerCreationForm
- form = ExplorerChangeForm
- model = Explorer
- list_display = ['email', 'username',]
-
class VoucherAdmin(admin.ModelAdmin):
fields = ['code', 'expires', 'created']
readonly_fields = ['created']
-admin.site.register(Explorer, ExplorerAdmin)
+admin.site.register(Explorer)
admin.site.register(Voucher, VoucherAdmin)
admin.site.register(Place)
admin.site.register(PlaceImage)
diff --git a/lostplaces/lostplaces_app/forms.py b/lostplaces/lostplaces_app/forms.py
index 8d78269..2b3f178 100644
--- a/lostplaces/lostplaces_app/forms.py
+++ b/lostplaces/lostplaces_app/forms.py
@@ -5,11 +5,12 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
-from .models import Explorer, Place, PlaceImage, Voucher
+from django.contrib.auth.models import User
+from lostplaces_app.models import Place, PlaceImage, Voucher
class ExplorerCreationForm(UserCreationForm):
class Meta:
- model = Explorer
+ model = User
fields = ('username', 'email')
voucher = forms.CharField(
max_length=30,
@@ -30,7 +31,7 @@ class ExplorerCreationForm(UserCreationForm):
class ExplorerChangeForm(UserChangeForm):
class Meta:
- model = Explorer
+ model = User
fields = ('username', 'email')
class PlaceForm(forms.ModelForm):
diff --git a/lostplaces/lostplaces_app/models.py b/lostplaces/lostplaces_app/models.py
index 47133e0..8e1885a 100644
--- a/lostplaces/lostplaces_app/models.py
+++ b/lostplaces/lostplaces_app/models.py
@@ -10,22 +10,40 @@ import os
import uuid
from django.db import models
+from django.contrib.auth.models import User
+from django.db.models.signals import post_save
from django.dispatch import receiver
-from django.contrib.auth.models import AbstractUser
from django.core.validators import MaxValueValidator, MinValueValidator
from easy_thumbnails.fields import ThumbnailerImageField
from taggit.managers import TaggableManager
# Create your models here.
-class Explorer(AbstractUser):
+
+class Explorer(models.Model):
"""
- Custom user model
- Addtional fields wbd
+ Profile that is linked to the a User.
+ Every user has a profile.
"""
+
+ user = models.OneToOneField(
+ User,
+ on_delete=models.CASCADE,
+ related_name='explorer'
+ )
def __str__(self):
- return self.username
+ return self.user.name
+
+@receiver(post_save, sender=User)
+def create_user_profile(sender, instance, created, **kwargs):
+ if created:
+ Explorer.objects.create(user=instance)
+
+@receiver(post_save, sender=User)
+def save_user_profile(sender, instance, **kwargs):
+ instance.explorer.save()
+
class Voucher(models.Model):
"""
@@ -42,6 +60,7 @@ class Voucher(models.Model):
def __str__(self):
return "Voucher " + str(self.pk)
+
class Place (models.Model):
"""
Place defines a lost place (location, name, description etc.).
@@ -53,7 +72,7 @@ class Place (models.Model):
Explorer,
on_delete=models.SET_NULL,
null=True,
- blank=True,
+ blank=True,
related_name='places'
)
location = models.CharField(max_length=50)
@@ -79,7 +98,7 @@ class Place (models.Model):
# Init fill values to prevent None
longitude = 0
latitude = 0
-
+
if amount > 0:
for place in place_list:
longitude += place.longitude
@@ -91,6 +110,7 @@ class Place (models.Model):
def __str__(self):
return self.name
+
def generate_image_upload_path(instance, filename):
"""
Callback for generating path for uploaded images.
@@ -98,13 +118,14 @@ def generate_image_upload_path(instance, filename):
return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1]
+
class PlaceImage (models.Model):
"""
PlaceImage defines an image file object that points to a file in uploads/.
Intermediate image sizes are generated as defined in SIZES.
PlaceImage references a Place to which it belongs.
"""
-
+
description = models.TextField(blank=True)
filename = ThumbnailerImageField(upload_to=generate_image_upload_path)
place = models.ForeignKey(
@@ -117,7 +138,7 @@ class PlaceImage (models.Model):
Explorer,
on_delete=models.SET_NULL,
null=True,
- blank=True,
+ blank=True,
related_name='images'
)
@@ -130,6 +151,8 @@ class PlaceImage (models.Model):
return ' '.join([self.place.name, str(self.pk)])
# These two auto-delete files from filesystem when they are unneeded:
+
+
@receiver(models.signals.post_delete, sender=PlaceImage)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""
@@ -140,6 +163,7 @@ def auto_delete_file_on_delete(sender, instance, **kwargs):
if os.path.isfile(instance.filename.path):
os.remove(instance.filename.path)
+
@receiver(models.signals.pre_save, sender=PlaceImage)
def auto_delete_file_on_change(sender, instance, **kwargs):
"""
@@ -160,6 +184,7 @@ def auto_delete_file_on_change(sender, instance, **kwargs):
if os.path.isfile(old_file.path):
os.remove(old_file.path)
+
class ExternalLink(models.Model):
url = models.URLField(max_length=200)
label = models.CharField(max_length=100)
@@ -167,11 +192,12 @@ class ExternalLink(models.Model):
Explorer,
on_delete=models.SET_NULL,
null=True,
- blank=True,
+ blank=True,
related_name='external_links'
)
submitted_when = models.DateTimeField(auto_now_add=True, null=True)
+
class PhotoAlbum(ExternalLink):
place = models.ForeignKey(
Place,
diff --git a/lostplaces/lostplaces_app/templates/partials/tagging.html b/lostplaces/lostplaces_app/templates/partials/tagging.html
index 581055b..471f880 100644
--- a/lostplaces/lostplaces_app/templates/partials/tagging.html
+++ b/lostplaces/lostplaces_app/templates/partials/tagging.html
@@ -6,7 +6,7 @@
{{tag}}
- {% if request.user and request.user == config.tagged_item.submitted_by %}
+ {% if request.user and request.user.explorer == config.tagged_item.submitted_by %}