Skip to content
Merged
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
23 changes: 17 additions & 6 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datetime import datetime
from django.utils import timezone
from datetime import datetime, timedelta, date
from django.conf import settings
from django.shortcuts import render, redirect, get_object_or_404, reverse
from django.core.mail import EmailMessage
Expand All @@ -19,7 +22,7 @@
from .tokens import account_activation_token
from .forms import SignUpForm, CreateStaffForm, UpdateStaffForm
from .models import Profile
from membership.models import Business, BusinessTeamMember, Subscription
from membership.models import Business, BusinessTeamMember, Subscription, Plan


User = get_user_model()
Expand All @@ -35,9 +38,9 @@ def get_success_url(self):
if url:
return url
elif self.request.user.is_admin:
return reverse('/')
return reverse('membership')
elif self.request.user.is_manager or self.request.user.is_staff:
return reverse('dashboard')
return reverse('membership')
else:
return f'/admin/'

Expand All @@ -60,9 +63,17 @@ def signup(request):

#create BusinessTeamMember
business_team = BusinessTeamMember.objects.get_or_create(business=business, user=user)
#business_team = BusinessTeamMember(user=user)
print('business_team:', business_team)
#business_team.save()

#create free subscription
ends_time = timezone.now() + timedelta(days=14)

subscription = Subscription.objects.get_or_create(
plan_id= 1,
business=business,
start_time = timezone.now(),
ends_time = ends_time,
is_active = True,
)

# send confirmation email
token = account_activation_token.make_token(user)
Expand Down
92 changes: 63 additions & 29 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 3.0.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
from decouple import config
from unipath import Path
from dj_database_url import parse as db_url

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -20,12 +11,19 @@
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'wsfsfgfdsf'
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = config('DEBUG', cast=bool)

ALLOWED_HOSTS = []
if not DEBUG:
ALLOWED_HOSTS = []
SECURE_SSL_REDIRECT=True
SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https')
SESSION_COOKIE_SECURE=True
SESSION_EXPIRE_AT_BROWSER_CLOSE=True
else:
ALLOWED_HOST = []


# Application definition
Expand Down Expand Up @@ -79,12 +77,29 @@
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
if not DEBUG:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.mysql',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT', cast=int),
# 'OPTIONS': {
# 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
# },
}
}

else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
}


# Password validation
Expand Down Expand Up @@ -120,10 +135,18 @@
USE_TZ = True


#Email Configurations For Development
EMAIL_HOST_USER = 'noreply@example.com'
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'sent_mails')
#Email Configurations
if not DEBUG:
EMAIL_BACKEND = config('EMAIL_BACKEND')
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_PORT = config('PORT', cast=int)
EMAIL_USE_SSL = config('PORT', cast=bool)
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
else:
EMAIL_HOST_USER = 'noreply@example.com'
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'sent_mails')


AUTH_USER_MODEL = 'accounts.CustomUser'
Expand All @@ -139,10 +162,21 @@
STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
if not DEBUG:
STATICFILES_DIRS = [
BASE_DIR+"/static",]

#M-PESA PAYMENT CONFIGURATIONS
PUBLIC_KEY ='MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArv9yxA69XQKBo24BaF/D+fvlqmGdYjqLQ5WtNBb5tquqGvAvG3WMFETVUSow/LizQalxj2ElMVrUmzu5mGGkxK08bWEXF7a1DEvtVJs6nppIlFJc2SnrU14AOrIrB28ogm58JjAl5BOQawOXD5dfSk7MaAA82pVHoIqEu0FxA8BOKU+RGTihRU+ptw1j4bsAJYiPbSX6i71gfPvwHPYamM0bfI4CmlsUUR3KvCG24rB6FNPcRBhM3jDuv8ae2kC33w9hEq8qNB55uw51vK7hyXoAa+U7IqP1y6nBdlN25gkxEA8yrsl1678cspeXr+3ciRyqoRgj9RD/ONbJhhxFvt1cLBh+qwK2eqISfBb06eRnNeC71oBokDm3zyCnkOtMDGl7IvnMfZfEPFCfg5QgJVk1msPpRvQxmEsrX9MQRyFVzgy2CWNIb7c+jPapyrNwoUbANlN8adU1m6yOuoX7F49x+OjiG2se0EJ6nafeKUXw/+hiJZvELUYgzKUtMAZVTNZfT8jjb58j8GVtuS+6TM2AutbejaCV84ZK58E2CRJqhmjQibEUO6KPdD7oTlEkFy52Y1uOOBXgYpqMzufNPmfdqqqSM4dU70PO8ogyKGiLAIxCetMjjm6FCMEA3Kc8K0Ig7/XtFm9By6VxTJK1Mg36TlHaZKP6VzVLXMtesJECAwEAAQ=='
MEDIA_ROOT = ''
STATIC_ROOT = ''
else:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

API_KEY = 'e0dlIJMW6qrit7c9XpP1olfhqgHQ05sw'

#M-PESA PAYMENT CONFIGURATIONS
if not DEBUG:
PUBLIC_KEY = config('PUBLIC_KEY')
API_KEY = config('API_KEY')
else:
PUBLIC_KEY = config('PUBLIC_KEY')
API_KEY = config('API_KEY')
25 changes: 25 additions & 0 deletions env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
DEBUG=True

SECRET_KEY=aaaaa

PUBLIC_KEY=

API_KEY=


# Credentials for database

DB_NAME=aaa
DB_USER=aaa
DB_HOST=localhost
DB_PASSWORD=aaaa
DB_PORT=5432

#Email Credentials

EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=aaaa
EMAIL_HOST_USER=aaaa
EMAIL_PORT=465
EMAIL_USE_SSL=True
EMAIL_HOST_PASSWORD=aaaa
18 changes: 18 additions & 0 deletions membership/migrations/0007_auto_20201017_0947.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1 on 2020-10-17 06:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('membership', '0006_auto_20201015_2336'),
]

operations = [
migrations.RenameField(
model_name='payment',
old_name='subscription',
new_name='subscription_id',
),
]
23 changes: 23 additions & 0 deletions membership/migrations/0008_auto_20201017_1020.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1 on 2020-10-17 07:20

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('membership', '0007_auto_20201017_0947'),
]

operations = [
migrations.RemoveField(
model_name='payment',
name='subscription_id',
),
migrations.AddField(
model_name='payment',
name='subscription',
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='membership.subscription'),
),
]
25 changes: 25 additions & 0 deletions membership/migrations/0009_auto_20201017_1046.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.1 on 2020-10-17 07:46

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('membership', '0008_auto_20201017_1020'),
]

operations = [
migrations.RemoveField(
model_name='payment',
name='subscription',
),
migrations.AddField(
model_name='payment',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
]
21 changes: 21 additions & 0 deletions membership/migrations/0010_auto_20201017_1046.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.1 on 2020-10-17 07:46

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('membership', '0009_auto_20201017_1046'),
]

operations = [
migrations.AlterField(
model_name='payment',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='accounts.customuser'),
),
]
21 changes: 21 additions & 0 deletions membership/migrations/0011_auto_20201017_1047.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.1 on 2020-10-17 07:47

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('membership', '0010_auto_20201017_1046'),
]

operations = [
migrations.AlterField(
model_name='payment',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
]
22 changes: 22 additions & 0 deletions membership/migrations/0012_auto_20201017_1253.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1 on 2020-10-17 09:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('membership', '0011_auto_20201017_1047'),
]

operations = [
migrations.RemoveField(
model_name='business',
name='reference_no',
),
migrations.AddField(
model_name='payment',
name='reference_no',
field=models.CharField(blank=True, max_length=150, null=True),
),
]
6 changes: 3 additions & 3 deletions membership/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Business(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
reference_no = models.CharField(max_length=150, blank=True, null=True) #payment gateway

def __str__(self):
return self.user.email
Expand Down Expand Up @@ -56,12 +55,13 @@ def __str__(self):
class Payment(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
subscription = models.OneToOneField(Subscription, on_delete=models.PROTECT)
user = models.ForeignKey(User, on_delete=models.PROTECT, blank=True, null=True)
phone = models.CharField(max_length=12, verbose_name='Enter your M-PESA mobile number', help_text='example. 255700000000')
transactionID = models.CharField(max_length=100, blank=True, null=True)
conversationID = models.CharField(max_length=100, blank=True, null=True)
reference_no = models.CharField(max_length=150, blank=True, null=True) #payment gateway

def __str__(self):
return self.subscription.plan.price
return self.user.business.plan


18 changes: 9 additions & 9 deletions membership/signals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Business
# from django.db.models.signals import post_save
# from django.dispatch import receiver
# from .models import Business


@receiver(post_save, sender=Business)
def business_reference_no(sender, instance, created, **kwargs):
if created:
reference_no = str(instance.id) + str(instance.created_at.hour) + str(instance.created_at.minute) + str(instance.created_at.year)[-2:]
# @receiver(post_save, sender=Business)
# def business_reference_no(sender, instance, created, **kwargs):
# if created:
# reference_no = str(instance.id) + str(instance.created_at.hour) + str(instance.created_at.minute) + str(instance.created_at.year)[-2:]

Business.objects.filter(pk=instance.pk).update(reference_no=reference_no)
print("reference_no:", reference_no)
# Business.objects.filter(pk=instance.pk).update(reference_no=reference_no)
# print("reference_no:", reference_no)
2 changes: 1 addition & 1 deletion membership/templates/membership/payment.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<div class="mb-3 mt-3 form-group">
<button class="btn btn-danger btn-block btn-lg" id="make-payment">Pay Tshs. {{plans.price|floatformat:2}}/=</button>
<p class="mb-2 mt-2 text-secondary">Your Order ID: {{subscription.id}}</p>
<p class="mb-2 mt-2 text-secondary">Ref no: {{reference_no}}</p>
</div>
<div class="mb-2">
<small>Please keep your phone in hand. Once you click "Pay" a request to comfirm your M-PESA PIN sent to your phone.</small>
Expand Down
Loading