diff --git a/crispy_ai/settings.py b/crispy_ai/settings.py index ad92576..d7c3fb1 100644 --- a/crispy_ai/settings.py +++ b/crispy_ai/settings.py @@ -121,6 +121,10 @@ # Variable to set default login/logout url +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + +MEDIA_URL = '/media/' + LOGIN_REDIRECT_URL = '/users/' # LOGOUT_REDIRECT_URL = '/' diff --git a/requirements.txt b/requirements.txt index ed4b331..89759e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ pytz==2019.1 setuptools==41.0.1 sqlparse==0.3.0 py3dns==3.2.0 -validate-email==1.3 \ No newline at end of file +validate-email==1.3 +Pillow==5.4.1 diff --git a/users/apps.py b/users/apps.py index 4ce1fab..b96159a 100644 --- a/users/apps.py +++ b/users/apps.py @@ -3,3 +3,7 @@ class UsersConfig(AppConfig): name = 'users' + + def ready(self): + + import users.signals diff --git a/users/forms.py b/users/forms.py index a9c7443..c741bc2 100644 --- a/users/forms.py +++ b/users/forms.py @@ -11,8 +11,14 @@ class Meta: model = User fields = ['username', 'email', 'password1', 'password2'] - def save(self, commit=True): - user = super(UserRegisterForm, self).save(commit=False) - if commit: - user.save() - return user + +class UserEditForm(forms.ModelForm): + + class Meta: + model = ProfileModel + exclude = ['user'] + # def save(self, commit=True): + # user = super(UserRegisterForm, self).save(commit=False) + # if commit: + # user.save() + # return user diff --git a/users/migrations/0002_auto_20190519_0948.py b/users/migrations/0002_auto_20190519_0948.py new file mode 100644 index 0000000..eecc499 --- /dev/null +++ b/users/migrations/0002_auto_20190519_0948.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.1 on 2019-05-19 09:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='profilemodel', + name='profile_image', + field=models.ImageField(upload_to='profile pics'), + ), + ] diff --git a/users/migrations/0003_auto_20190519_1038.py b/users/migrations/0003_auto_20190519_1038.py new file mode 100644 index 0000000..0dfa959 --- /dev/null +++ b/users/migrations/0003_auto_20190519_1038.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.1 on 2019-05-19 10:38 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0002_auto_20190519_0948'), + ] + + operations = [ + migrations.AlterField( + model_name='profilemodel', + name='dob', + field=models.DateField(default=datetime.date.today), + ), + ] diff --git a/users/migrations/0004_auto_20190519_1042.py b/users/migrations/0004_auto_20190519_1042.py new file mode 100644 index 0000000..3fd8886 --- /dev/null +++ b/users/migrations/0004_auto_20190519_1042.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.1 on 2019-05-19 10:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0003_auto_20190519_1038'), + ] + + operations = [ + migrations.AlterField( + model_name='profilemodel', + name='profile_image', + field=models.ImageField(default='media/default.png', upload_to='profile pics'), + ), + ] diff --git a/users/models.py b/users/models.py index 9b628c4..e602cd5 100644 --- a/users/models.py +++ b/users/models.py @@ -2,6 +2,7 @@ from django.db import models from django.contrib.auth.models import User from django.urls import reverse +import datetime # Create your models here. @@ -9,11 +10,11 @@ class ProfileModel(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) university = models.CharField(max_length=30) discription = models.CharField(max_length=500) - dob = models.DateField() - profile_image = models.ImageField() + dob = models.DateField(default=datetime.date.today) + profile_image = models.ImageField(upload_to='profile pics', default='media/default.png') def get_absolute_url(self): return reverse('login') def __str__(self): - return self.university + return self.user.username diff --git a/users/signals.py b/users/signals.py new file mode 100644 index 0000000..bc552a7 --- /dev/null +++ b/users/signals.py @@ -0,0 +1,15 @@ +from django.db.models.signals import post_save +from django.contrib.auth.models import User +from django.dispatch import receiver +from .models import ProfileModel + + +@receiver(post_save, sender=User) +def create_profile(sender, instance, created, **kwargs): + if created: + ProfileModel.objects.create(user=instance) + + +@receiver(post_save, sender=User) +def save_profile(sender, instance, created, **kwargs): + instance.profilemodel.save() diff --git a/users/templates/base.html b/users/templates/base.html index 0f76c77..d0b3ed4 100644 --- a/users/templates/base.html +++ b/users/templates/base.html @@ -9,6 +9,8 @@ + + diff --git a/users/templates/registration/login.html b/users/templates/registration/login.html index 085ebab..f8dd8bd 100644 --- a/users/templates/registration/login.html +++ b/users/templates/registration/login.html @@ -5,6 +5,7 @@ {% block content %}
+
Login
Here you have to Login into your account
diff --git a/users/templates/users/edit.html b/users/templates/users/edit.html new file mode 100644 index 0000000..eae4def --- /dev/null +++ b/users/templates/users/edit.html @@ -0,0 +1,26 @@ +{% extends 'base.html' %} +{% block title %} Edit your info{% endblock %} +{% block content %} +
+
+
+
edit these things
+
Here you have to edit your account {{ user.username }}
+
+ {% csrf_token %} +{# {% for field in form %}#} +{#

#} +{# {{ field.label_tag }}
#} +{# {{ field }}#} +{# {% endfor %}#} + {{ form.as_p }} +

+ +
+ +
+
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/users/templates/users/home.html b/users/templates/users/home.html index c457d0b..670945d 100644 --- a/users/templates/users/home.html +++ b/users/templates/users/home.html @@ -14,11 +14,26 @@ {% block title %}Home{% endblock %} {% block content %} -{% if user.is_authenticated %} - Hello {{ user.username }}! -

Logout

-{% else %} -

You are not logged in

- Login -{% endif %} +
+ {% if user.is_authenticated %} +
+
+
Hello {{ user.username }}!
+
Welcome
+
+ +
+ +
+ +
+ {% else %} + Another link +
+
+ {% endif %} + + + +
{% endblock %} diff --git a/users/templates/users/login_please.html b/users/templates/users/login_please.html new file mode 100644 index 0000000..21a31ff --- /dev/null +++ b/users/templates/users/login_please.html @@ -0,0 +1,16 @@ +{% extends 'base.html' %} + +{% block title %}Please Login{% endblock %} + +{% block content %} + +
+ +
+ {% csrf_token %} + +
+
+{% endblock %} diff --git a/users/templates/users/profile.html b/users/templates/users/profile.html new file mode 100644 index 0000000..c078102 --- /dev/null +++ b/users/templates/users/profile.html @@ -0,0 +1,29 @@ +{% extends 'base.html' %} + +{% block title %}User Profile{% endblock %} + +{% block content %} +
+
+ Card image cap +
+
{{ user.username }}
+
+ +
+
+
+
+
+
+
    +
  • {{ user.profilemodel.university }}
  • +
  • {{ user.profilemodel.discription }}
  • +
  • {{ user.profilemodel.dob }}
  • +
+ +
+
If these fields are empty edit your profile
+
+ +{% endblock %} diff --git a/users/templates/users/register.html b/users/templates/users/register.html index 7fe2329..ff0ffaf 100644 --- a/users/templates/users/register.html +++ b/users/templates/users/register.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% block title %}Registration Form{% endblock %} {% block content %}
diff --git a/users/templates/users/test.html b/users/templates/users/test.html index d67d320..886c60b 100644 --- a/users/templates/users/test.html +++ b/users/templates/users/test.html @@ -1,12 +1,10 @@ {% extends 'base.html' %} +{% block title %}Error Page{% endblock %} {% block content %}
{% for field in form %}

{{ field.label_tag }}
- {% if field.help_text %} - {{ field.help_text }} - {% endif %} {% for error in field.errors %}

{{ error }}

{% endfor %} diff --git a/users/urls.py b/users/urls.py index e7dfe86..a2ff41b 100644 --- a/users/urls.py +++ b/users/urls.py @@ -1,6 +1,8 @@ from django.contrib import admin from django.urls import path, include from . import views +from django.conf import settings +from django.conf.urls.static import static urlpatterns = [ path('', include('django.contrib.auth.urls')), @@ -10,4 +12,10 @@ # path('login',views.login,name='login'), # Register Page path('register', views.register_user, name='register'), + path('profile', views.user_profile, name='profile'), + path('edit', views.profile_update, name='edit'), + ] +if settings.DEBUG: + urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/users/views.py b/users/views.py index c42b68f..984becc 100644 --- a/users/views.py +++ b/users/views.py @@ -1,7 +1,9 @@ from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required -from .forms import UserRegisterForm +from .forms import UserRegisterForm, UserEditForm from django.http import Http404 +from django.contrib.auth.models import User +from django.views.generic.edit import UpdateView from validate_email import validate_email from django.contrib import messages @@ -22,21 +24,42 @@ def register_user(request): form = UserRegisterForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') - is_valid = validate_email(email, check_mx=True) - if is_valid: - is_valid = validate_email(email, verify=True) - if is_valid: - username = form.cleaned_data.get('username') - messages.success(request, 'Account Created for' + username) - form.save() - return redirect('Home') - else: - return render(request, './users/test.html', {'form': form}) - else: - return render(request, './users/test.html', {'form': form}) - + username = form.cleaned_data.get('username') + messages.success(request, f'Account Created for {username}!') + form.save() + return redirect('Home') else: return render(request, './users/test.html', {'form': form}) + else: form = UserRegisterForm() return render(request, './users/register.html', {'form': form}) + + +def user_profile(request): + + if request.user.is_authenticated: + return render(request, './users/profile.html', {'user': request.user}) + else: + return render(request, './users/login_please.html') + + +def profile_update(request): + + if request.method == 'POST': + user = User.objects.get(username=request.user.username) + user_id = user.profilemodel.id + form = UserEditForm(request.POST) + if form.is_valid(): + profile = form.save(commit=False) + profile.user = user + profile.user.profilemodel.id = user_id + profile.save() + return redirect('profile') + else: + form = UserEditForm() + return render(request, './users/edit.html', {'form': form, 'user': request.user}) + + else: + form = UserEditForm() + return render(request, './users/edit.html', {'form': form, 'user': request.user})