Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__pycache__
volumes/
.secrets/
static/
!.static/150x150.png
.DS_Store
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#FROM mysql/mysql-server:latest
FROM python:3.9.7-slim

WORKDIR /python


RUN apt-get update -y && \
apt-get install -y \
python \
default-mysql-client \
python3-dev \
default-libmysqlclient-dev \
build-essential
RUN pip install \
flask \
flask-sqlalchemy \
mysqlclient \
boto3

ADD ./ ./
25 changes: 16 additions & 9 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import mysql.connector
import os
from flask_sqlalchemy import SQLAlchemy

class Database:
def __init__(self):
self.db = mysql.connector.connect(
host = 'localhost',
user = 'root',
password = '',
database = 'twitter_development'
)
_instance = None
app = None
database = None

self.cursor = self.db.cursor()
def __new__(klass, flask_app):
if klass._instance is None:
klass.app = flask_app
klass._instance = super(Database, klass).__new__(klass)
klass.__initialize(klass)
return klass._instance

def __initialize(klass):
klass.app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
klass.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
klass.database = SQLAlchemy(klass.app)
46 changes: 25 additions & 21 deletions follower.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
from flask import json
from user import Users
from db import Database

class Follower:
def __init__(self):
db = Database()
self.db = db.db
self.cursor = db.cursor
db = Database.database

def follow(self, user_id, follow_id):
sql = 'SELECT COUNT(id) FROM followers WHERE user_id = %s AND follow_id = %s' % (user_id, follow_id)
self.cursor.execute(sql)
total = dict(zip(self.cursor.column_names, self.cursor.fetchone()))
if total['COUNT(id)'] > 0:
return False
else:
next_sql = 'INSERT INTO followers (user_id, follow_id) VALUES (%s, %s)' % (user_id, follow_id)
class Followers(db.Model):
id = db.Column(db.Integer, primary_key = True)
user_id = db.Column(db.Integer, db.ForeignKey(Users.id), nullable = True)
follow_id = db.Column(db.Integer, db.ForeignKey(Users.id), nullable = True)

self.cursor.execute(next_sql)
self.db.commit()
return True
def __init__(self, data):
self.user_id = data['user_id']
self.follow_id = data['follow_id']

def unfollow(self, user_id, follow_id):
sql = 'DELETE FROM followers WHERE user_id = %s AND follow_id = %s' % (user_id, follow_id)
self.cursor.execute(sql)
self.db.commit()
@classmethod
def search(self, data):
return Followers.query.filter_by(user_id = data['user_id'], follow_id = data['follow_id']).first()

return True
def create(self):
db.session.add(self)
db.session.commit()
return self

def delete(self):
db.session.delete(self)
db.session.commit()
return self

def to_json(self):
return json.dumps(self, default = lambda o: '', sort_keys = True, indent = 4)
6 changes: 6 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build:
docker:
web: Dockerfile

run:
web: python3 main.py
38 changes: 15 additions & 23 deletions login.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
from db import Database
import hashlib
from user_info import UserInfos
from user import Users

class Login:
def __init__(self):
db = Database()
self.db = db.db
self.cursor = db.cursor
pass

def signup(self, email, password):
self.cursor.execute('SELECT COUNT(*) FROM users WHERE email = "%s" AND password = "%s"' % (email, self.__to_sha256(password)))
(total,) = self.cursor.fetchone()
if total <= 0:
self.cursor.execute('INSERT INTO users (email, password) VALUES ("%s", "%s")' % (email, self.__to_sha256(password)))
self.db.commit()
self.cursor.execute('SELECT * FROM users WHERE email = "%s"' % email)
(id, email, _) = self.cursor.fetchone()
return (id, email)
exists_user = Users.search({ 'email': email, 'password': password})
if exists_user:
return exists_user
else:
return (None, None)

u = Users({ 'email': email, 'password': password })
created_user = u.create()
UserInfos.initial_to_create(created_user.id)
return created_user

def login(self, email, password):
self.cursor.execute('SELECT * FROM users LEFT JOIN user_infos ON users.id = user_infos.user_id WHERE email = "%s" AND password = "%s"' % (email, self.__to_sha256(password)))
(id, email, _, _, _, display_name, user_name, interests, _, _) = self.cursor.fetchone()
if id:
return (id, email, display_name, user_name, interests)
exists_user = Users.search({ 'email': email, 'password': password})
if exists_user:
return exists_user
else:
return False

def __to_sha256(self, password):
return hashlib.sha256(f'{password}'.encode()).hexdigest()
return None
Loading