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

There is a bug still #42

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions crispy_ai/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '/'

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytz==2019.1
setuptools==41.0.1
sqlparse==0.3.0
py3dns==3.2.0
validate-email==1.3
validate-email==1.3
Pillow==5.4.1
4 changes: 4 additions & 0 deletions users/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

class UsersConfig(AppConfig):
name = 'users'

def ready(self):

import users.signals
16 changes: 11 additions & 5 deletions users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions users/migrations/0002_auto_20190519_0948.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
19 changes: 19 additions & 0 deletions users/migrations/0003_auto_20190519_1038.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
18 changes: 18 additions & 0 deletions users/migrations/0004_auto_20190519_1042.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
7 changes: 4 additions & 3 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
import datetime


# Create your models here.
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
15 changes: 15 additions & 0 deletions users/signals.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions users/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="{% static 'css/header.css' %}" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">


</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions users/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% block content %}
<div class="container">
<div class="card mx-auto " style="width: 30rem;margin-top: 5%">

<div class="card-body">
<h5 class="card-title">Login</h5>
<h6 class="card-subtitle mb-2 text-muted">Here you have to Login into your account</h6>
Expand Down
26 changes: 26 additions & 0 deletions users/templates/users/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends 'base.html' %}
{% block title %} Edit your info{% endblock %}
{% block content %}
<div class="container">
<div class="card mx-auto " style="width: 30rem;margin-top: 5%">
<div class="card-body">
<h5 class="card-title">edit these things</h5>
<h6 class="card-subtitle mb-2 text-muted">Here you have to edit your account {{ user.username }}</h6>
<form method="post" action="">
{% csrf_token %}
{# {% for field in form %}#}
{# <p>#}
{# {{ field.label_tag }}<br>#}
{# {{ field }}#}
{# {% endfor %}#}
{{ form.as_p }}
</p>
<button class="btn btn-success" type="submit">Edit</button>
</form>

</div>
</div>

</div>

{% endblock %}
29 changes: 22 additions & 7 deletions users/templates/users/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@
{% block title %}Home{% endblock %}

{% block content %}
{% if user.is_authenticated %}
Hello {{ user.username }}!
<p><a href="{% url 'logout' %}"> Logout</a></p>
{% else %}
<p>You are not logged in</p>
<a href="{% url 'login' %}">Login</a>
{% endif %}
<div class="container" style="margin-top: 5%;">
{% if user.is_authenticated %}
<div class="card mx-auto" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"> Hello {{ user.username }}!</h5>
<h6 class="card-subtitle mb-2 text-muted">Welcome</h6>
<form action="{% url 'logout' %}" method="get" style="display:inline;">
<button class="btn btn-success" >Logout</button>
</form>

<form action="{% url 'profile' %}" method="get" style="display:inline;">
<button class="btn btn-success" >Profile</button>
</form>
{% else %}
<a href="#" class="card-link">Another link</a>
</div>
</div>
{% endif %}



</div>
{% endblock %}
16 changes: 16 additions & 0 deletions users/templates/users/login_please.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base.html' %}

{% block title %}Please Login{% endblock %}

{% block content %}

<div class="container" style="margin-top: 5%">
<div class="alert alert-danger" role="alert">
You need To Login Please
</div>
<form action="{% url 'Home' %}" method="get">
{% csrf_token %}
<button type="submit" class="btn btn-success">Login</button>
</form>
</div>
{% endblock %}
29 changes: 29 additions & 0 deletions users/templates/users/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends 'base.html' %}

{% block title %}User Profile{% endblock %}

{% block content %}
<div class="container">
<div class="card mx-auto" style="width: 18rem; margin-top: 5%">
<img class="card-img-top" src="{{ MEDIA_ROOT }}{{ user.profilemodel.profile_image.url }}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{ user.username }}</h5>
<form action="{% url 'edit' %}" method="get">
<button class="btn btn-outline-primary"><i class="far fa-edit">Edit</i></button>
</form>
</div>
</div>
</div>
<div class="container">
<div class="card mx-auto" style="width: 40rem;margin-top: 10%">
<ul class="list-group list-group-flush">
<li class="list-group-item">{{ user.profilemodel.university }}</li>
<li class="list-group-item">{{ user.profilemodel.discription }}</li>
<li class="list-group-item">{{ user.profilemodel.dob }}</li>
</ul>

</div>
<h6 style="margin-left:25% ">If these fields are empty edit your profile</h6>
</div>

{% endblock %}
1 change: 1 addition & 0 deletions users/templates/users/register.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'base.html' %}
{% block title %}Registration Form{% endblock %}
{% block content %}
<div class="container">
<div class="card mx-auto " style="width: 30rem;margin-top: 5%">
Expand Down
4 changes: 1 addition & 3 deletions users/templates/users/test.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{% extends 'base.html' %}
{% block title %}Error Page{% endblock %}
{% block content %}
<div class="container" >
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
Expand Down
8 changes: 8 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
@@ -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')),
Expand All @@ -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)
51 changes: 37 additions & 14 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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})