Skip to content

Commit

Permalink
Merge pull request #255 from DEKHTIARJonathan/dev
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
Jonathan DEKHTIAR authored Sep 11, 2018
2 parents 9fcef2d + 7941287 commit e26552c
Show file tree
Hide file tree
Showing 48 changed files with 306 additions and 211 deletions.
6 changes: 5 additions & 1 deletion application/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')


def load_env():
"Get the path to the .env file and load it."
project_dir = os.path.dirname(os.path.dirname(__file__))
dotenv.read_dotenv(os.path.join(project_dir, '.env'))


if not any(x in os.environ for x in platforms):
load_env()

Expand Down Expand Up @@ -54,7 +56,7 @@ def load_env():
app.conf.worker_concurrency = settings.CELERY_CONCURRENCY

# Results settings
#app.conf.result_backend = settings.CELERY_RESULT_BACKEND
# app.conf.result_backend = settings.CELERY_RESULT_BACKEND
app.conf.result_serializer = settings.CELERY_RESULT_SERIALIZER
app.conf.result_expires = settings.CELERY_TASK_RESULT_EXPIRES

Expand Down Expand Up @@ -82,10 +84,12 @@ def load_env():
app.conf.monitors_expire_error = settings.CELERY_MONITORS_EXPIRE_ERROR
app.conf.monitors_expire_pending = settings.CELERY_MONITORS_EXPIRE_PENDING


class CeleryConfig(AppConfig):
name = 'application'
verbose_name = 'Celery Config'


@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
2 changes: 2 additions & 0 deletions application/custom_storages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage


class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION


class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
8 changes: 6 additions & 2 deletions application/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os, sys, dotenv
import os
import dotenv

from django.core.wsgi import get_wsgi_application

platforms = ["TRAVIS", "HEROKU", "BLUEMIX"]


def load_env():
"Get the path to the .env file and load it."
"""Get the path to the .env file and load it."""
project_dir = os.path.dirname(os.path.dirname(__file__))
dotenv.read_dotenv(os.path.join(project_dir, '.env'))


if not any(x in os.environ for x in platforms):
load_env()

Expand Down
19 changes: 11 additions & 8 deletions feedcrunch/management/commands/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from feedcrunch.models import *
from application.settings import *

import os
import csv


def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)

for row in csv_reader:
yield [unicode(cell, 'utf-8') for cell in row]
yield [str(cell) for cell in row]


class Command(BaseCommand):
help = 'Load Data from continents.csv and countries.csv'
Expand All @@ -21,22 +24,22 @@ def handle(self, *args, **options):

## Load Continents to DATABASE

print ("Saving Continents ...")
print("Saving Continents ...")
with open(os.path.join(BASE_DIR, 'feedcrunch/data/continents.csv'), 'rb') as f:
reader = csv.reader(f)
next(reader, None) #skip the headers
next(reader, None) # skip the headers
for row in reader:
cntnt = Continent(code=row[0], name=row[1])
cntnt.save()
print ("Continents Saved !")
print("Continents Saved !")

## Load Countries to DATABASE

print ("Saving Countries ...")
print("Saving Countries ...")
with open(os.path.join(BASE_DIR, 'feedcrunch/data/countries.csv'), 'rb') as f:
reader = unicode_csv_reader(f)
next(reader, None) #skip the headers
next(reader, None) # skip the headers
for row in reader:
cntry = Country(continent=Continent.objects.get(name=row[2]), code=row[1], name=row[0])
cntry.save()
print ("Countries Saved !")
print("Countries Saved !")
22 changes: 13 additions & 9 deletions feedcrunch/model_files/models_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

from __future__ import unicode_literals

import datetime

from io import StringIO

import string
import pickle
import random
import uuid
import urllib

# original based on sci-kit hashing function
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import InMemoryUploadedFile

import datetime

from io import StringIO

import os, string, pickle, random, uuid, urllib

def id_generator(size=20, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))


def get_upload_path():

while True:
Expand All @@ -31,6 +35,7 @@ def get_upload_path():

return settings.USER_ESTIMATOR_PATH + filename


def get_upload_path_instance(instance, filename):
if instance.object_file:

Expand All @@ -45,7 +50,6 @@ def get_upload_path_instance(instance, filename):
return get_upload_path()



class Estimator(models.Model):

"""This class creates estimator objects that persists predictive models
Expand All @@ -55,7 +59,7 @@ class Estimator(models.Model):
:description:
:estimator:
>>> from estimators.models import Estimator
>>> from feedcrunch.model_files.models_estimators import Estimator
>>> est = Estimator()
>>> est.estimator = object
>>> est.description = "k-means with 5 clusters"
Expand Down
3 changes: 3 additions & 0 deletions feedcrunch/model_files/models_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.db import models

############################# Localisation #####################################


class Continent(models.Model):
name = models.CharField(primary_key=True, max_length=60)
code = models.CharField(max_length=2)

def __str__(self):
return self.name


class Country(models.Model):
name = models.CharField(primary_key=True, max_length=60)
code = models.CharField(max_length=2)
Expand Down
11 changes: 9 additions & 2 deletions feedcrunch/model_files/models_interest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import random
import urllib
import string
import uuid

from django.db import models
from django.conf import settings

from .models_rssfeed import RSSFeed
from feedcrunch.model_files.models_rssfeed import RSSFeed

import random, urllib, string, uuid

############################## Interest MODEL ###################################

def id_generator(size=20, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))


def get_photo_path(instance, filename):
ext = filename.split('.')[-1]

Expand All @@ -27,6 +33,7 @@ def get_photo_path(instance, filename):

return settings.INTEREST_PHOTO_PATH + filename


class Interest(models.Model):
name = models.CharField(max_length=255, primary_key=True)
rssfeeds = models.ManyToManyField(RSSFeed, blank=True, related_name='rel_interests')
Expand Down
4 changes: 3 additions & 1 deletion feedcrunch/model_files/models_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.db import models

from encrypted_model_fields.fields import EncryptedCharField


############################## Option MODEL ###################################

class Option(models.Model):
parameter = models.CharField(max_length=255, primary_key=True)
value = EncryptedCharField(max_length=255, default='')
value = EncryptedCharField(max_length=255, default='') # TODO Check This

def __str__(self):
return self.parameter
Expand Down
16 changes: 8 additions & 8 deletions feedcrunch/model_files/models_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User, UserManager

import re, uuid, datetime, random, string
from feedcrunch.model_files.models_user import *
from feedcrunch.model_files.models_tag import *

from .models_geo import *
from .models_user import *
from .models_tag import *
from functions.get_domain import get_domain

from get_domain import get_domain

def create_key(size=8):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(size))
return ''.join(
random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)
for _ in range(size)
)


############################## Post MODEL ###################################

Expand Down
14 changes: 7 additions & 7 deletions feedcrunch/model_files/models_rss_assocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from __future__ import unicode_literals
from django.db import models

import datetime, string, re, uuid
from feedcrunch.model_files.models_user import FeedUser
from feedcrunch.model_files.models_rssfeed import RSSFeed
from feedcrunch.model_files.models_rssarticle import RSSArticle

from .models_user import FeedUser
from .models_rssfeed import RSSFeed
from .models_rssarticle import RSSArticle
from functions.clean_html import clean_html

from clean_html import clean_html

def shorten_string(string, max_size):
if max_size < 7:
Expand All @@ -20,6 +19,7 @@ def shorten_string(string, max_size):
else:
return string


class RSSFeed_SubManager(models.Manager):
def create(self, *args, **kwargs):

Expand All @@ -33,6 +33,7 @@ def create(self, *args, **kwargs):

return super(RSSFeed_SubManager, self).create(*args, **kwargs)


class RSSFeed_Sub(models.Model):
objects = RSSFeed_SubManager()

Expand Down Expand Up @@ -72,8 +73,6 @@ def short_title(self):
return shorten_string(self.title, 75)


###################################################################################################################################

class RSSArticle_AssocManager(models.Manager):
def create(self, *args, **kwargs):

Expand All @@ -82,6 +81,7 @@ def create(self, *args, **kwargs):

return super(RSSArticle_AssocManager, self).create(*args, **kwargs)


class RSSArticle_Assoc(models.Model):
objects = RSSArticle_AssocManager()

Expand Down
13 changes: 8 additions & 5 deletions feedcrunch/model_files/models_rss_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.core.validators import MaxValueValidator, MinValueValidator

import datetime

from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.db import models

from .models_user import FeedUser
from feedcrunch.model_files.models_user import FeedUser

from djchoices import DjangoChoices, ChoiceItem

from ipware.ip import get_real_ip, get_ip

import datetime
from ipware.ip import get_real_ip
from ipware.ip import get_ip

######################################## Subscription ################################################

Expand Down
11 changes: 7 additions & 4 deletions feedcrunch/model_files/models_rssarticle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import uuid

from django.db import models

import datetime, string, re, uuid
from feedcrunch.model_files.models_rssfeed import RSSFeed

from .models_rssfeed import RSSFeed
from functions.get_domain import get_domain
from functions.clean_html import clean_html

from get_domain import get_domain
from clean_html import clean_html

class RSSArticleManager(models.Manager):
def create(self, *args, **kwargs):
Expand All @@ -28,6 +30,7 @@ def create(self, *args, **kwargs):

return super(RSSArticleManager, self).create(*args, **kwargs)


class RSSArticle(models.Model):
objects = RSSArticleManager()

Expand Down
Loading

0 comments on commit e26552c

Please sign in to comment.