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

Ads and scraper logging refactoring #6

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5cc4f53
add logging config to settings
diadzine Dec 30, 2015
b760db3
add logger and code refactoring
diadzine Dec 30, 2015
f3c95f3
add new dependancy on django-multiselectfield
diadzine Mar 6, 2016
cd5031c
add multiselectfield to installed apps and ADS_PLACEHOLDERS choices
diadzine Mar 6, 2016
76b23b8
add placeholders field to select where ad will appear
diadzine Mar 6, 2016
3b4c98f
add placeholders to AdsSerializer
diadzine Mar 6, 2016
1a2a87e
add null and blank param to placeholders field
diadzine Mar 6, 2016
f88381e
add ads model migrations
diadzine Mar 6, 2016
5aa7e0c
add rankings model migrations
diadzine Mar 6, 2016
5e7f800
add placeholder filter to AdsCreateReadView
diadzine Mar 6, 2016
4341b36
update AdsCreateReadView
diadzine Mar 6, 2016
fc2b71d
update settings
diadzine Mar 6, 2016
67f9f6f
add AdsManager to Ads model
diadzine Mar 7, 2016
7bb814b
change AdsCreateReadView to use AdsManager and placeholder with categ…
diadzine Mar 7, 2016
8d8ad16
cleaning
diadzine Mar 9, 2016
a924e02
add empty logs folder
diadzine Mar 9, 2016
5a944d1
add all log files to gitignore file
diadzine Mar 9, 2016
21fb9e9
change AdsCreateReadView to get ads only if placeholder is defined in…
diadzine Mar 9, 2016
9472e80
add ADS_PLACEHOLDERS list to enable/disable ads blocks in pages
diadzine Mar 9, 2016
ec7d23f
fix merge
diadzine Mar 10, 2016
6243c81
Merge branch 'master' of bitbucket.org:ab_tooskich/python_core
diadzine Mar 10, 2016
fd486bd
add placeholder class to Ads models
diadzine Mar 14, 2016
2860460
add AdsPlaceholders serializer
diadzine Mar 14, 2016
de92f61
add ListAPIView for AdsPlaceholders
diadzine Mar 14, 2016
5c14e88
add new url dispatch for AdsPlaceholder API view
diadzine Mar 14, 2016
70cd619
Merge branch 'master' of https://github.com/Tooskich/python_core
diadzine Mar 14, 2016
2671473
refactoring dir and path and add exception handling
diadzine Mar 17, 2016
0d0ee1c
clean spiders to remove deprecated imports
diadzine Mar 21, 2016
46ed1ce
add ads placeholders settings
diadzine Jul 28, 2016
754a880
add repr method for FisRaces item
diadzine Jul 28, 2016
55a1852
alter table ads for placeholders
diadzine Jul 28, 2016
713098a
fix missing __init__.py for rankings module
diadzine Aug 2, 2016
c8ec4be
add coding comment for utf-8 support
diadzine Aug 2, 2016
7201378
Remove Ads with placeholders null
diadzine Sep 9, 2016
40b1d65
Remove Ads by category if no Ad found by category and placeholder
diadzine Sep 9, 2016
5c671ea
fix issue with jump variable in races spider
diadzine Sep 20, 2016
d50c5cf
exclude Mag posts from News post list
diadzine Nov 21, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
python2.7
2.7
*.pyc
logs/*
Binary file removed ads/__init__.pyc
Binary file not shown.
20 changes: 20 additions & 0 deletions ads/migrations/0003_auto_20151029_1429.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('ads', '0002_ads_link'),
]

operations = [
migrations.AlterField(
model_name='ads',
name='link',
field=models.TextField(default=b'', null=True),
preserve_default=True,
),
]
21 changes: 21 additions & 0 deletions ads/migrations/0004_ads_placeholders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import multiselectfield.db.fields


class Migration(migrations.Migration):

dependencies = [
('ads', '0003_auto_20151029_1429'),
]

operations = [
migrations.AddField(
model_name='ads',
name='placeholders',
field=multiselectfield.db.fields.MultiSelectField(blank=True, max_length=165, null=True, choices=[(b'actu-top', b'Page Actu, haut de page'), (b'actu-side', b'Page Actu, colonne droite'), (b'actu-bottom', b'Page Actu, base de page'), (b'results-top', b'Pages R\xc3\xa9sultats, haut de page'), (b'results-side', b'Pages R\xc3\xa9sultats, colonne droite'), (b'results-bottom', b'Pages R\xc3\xa9sultats, base de page'), (b'blog-top', b'Pages Blog, haut de page'), (b'blog-side', b'Pages Blog, colonne droite'), (b'blog-bottom', b'Pages Blog, base de page'), (b'shop-top', b'Page Angulation, haut de page'), (b'shop-bottom', b'Page Angulation, base de page'), (b'sponsors-top', b'Page Mentors & Sponsors, haut de page'), (b'sponsors-side', b'Page Mentors & Sponsors, colonne droite'), (b'sponsors-bottom', b'Page Mentors & Sponsors, base de page')]),
preserve_default=True,
),
]
48 changes: 47 additions & 1 deletion ads/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
from django.db import models
from django.conf import settings
from multiselectfield import MultiSelectField

# Create your models here.

class AdsManager(models.Manager):
def get_queryset(self):
return super(AdsManager, self).get_queryset()

def by_placeholder(self, placeholder):
return super(AdsManager, self).get_queryset().filter(
placeholders__contains=placeholder
)

def by_category(self, category):

if category == 'square':
qs = super(AdsManager, self).get_queryset().filter(square=1)
elif category == 'horizontal':
qs = super(AdsManager, self).get_queryset().filter(horizontal=1)
elif category == 'vertical':
qs = super(AdsManager, self).get_queryset().filter(vertical=1)
else:
qs = super(AdsManager, self).get_queryset()

return qs

def by_placeholder_and_category(self, placeholder, category):
criteria = {
'placeholders__contains': placeholder,
category: 1,
}
qs = super(AdsManager, self).get_queryset().filter(**criteria)

return qs


class Ads(models.Model):
name = models.CharField(max_length=510)
url = models.TextField(null=True)
secureUrl = models.TextField(null=True)
link = models.TextField(null=True, default='')
placeholders = MultiSelectField(
choices=settings.ADS_PLACEHOLDERS,
null=True,
blank=True,
)
horizontal = models.PositiveSmallIntegerField(null=True)
vertical = models.PositiveSmallIntegerField(null=True)
square = models.PositiveSmallIntegerField(null=True)
Expand All @@ -16,5 +53,14 @@ class Ads(models.Model):
auto_now=False
)

objects = AdsManager()

def __unicode__(self):
return u'%s' % self.name


class Placeholder(object):

def __init__(self, code, label):
self.code = code
self.label = label
Binary file removed ads/models.pyc
Binary file not shown.
Binary file removed ads/urls.pyc
Binary file not shown.
7 changes: 6 additions & 1 deletion apiv1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ class AdsSerializer(serializers.ModelSerializer):

class Meta:
model = Ads
fields = ('id', 'name', 'link', 'url', 'secureUrl',
fields = ('id', 'name', 'link', 'url', 'secureUrl', 'placeholders',
'horizontal', 'vertical', 'square', 'date', )


class AdsPlaceholdersSerializer(serializers.Serializer):
code = serializers.CharField(max_length=30)
label = serializers.CharField(max_length=100)


class BloggersSerializer(serializers.ModelSerializer):

class Meta:
Expand Down
4 changes: 4 additions & 0 deletions apiv1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
view=apiv1.views.AdsCreateReadView.as_view(),
name='REST View'),

url(regex=r'^ads/placeholders/$',
view=apiv1.views.AdsPlaceholdersReadView.as_view(),
name='REST View'),

url(regex=r'^ads/(?P<pk>[-\w]+)/$',
view=apiv1.views.AdsReadUpdateDeleteView.as_view(),
name='REST View'),
Expand Down
49 changes: 36 additions & 13 deletions apiv1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
RetrieveUpdateDestroyAPIView,
)
from rest_framework.permissions import (
IsAuthenticated,
IsAuthenticatedOrReadOnly
)

from apiv1.serializers import (
NewsSerializer,
AdsSerializer,
AdsPlaceholdersSerializer,
BloggersSerializer,
BlogPostsSerializer,
SkiclubsSerializer,
Expand All @@ -22,9 +22,9 @@

from django.utils import timezone
from django.core.cache import cache

from django.conf import settings
from news.models import News
from ads.models import Ads
from ads.models import Ads, Placeholder
from skiclubs.models import Skiclubs
from pages.models import Pages
from rankings.models import Races
Expand All @@ -43,7 +43,7 @@ class NewsCreateReadView(ListCreateAPIView):

def get_queryset(self):
now = timezone.now()
return News.objects.filter(date__lte=now).order_by('date').reverse()
return News.objects.filter(date__lte=now).exclude(mag=1).order_by('date').reverse()


class MagCreateReadView(NewsCreateReadView):
Expand Down Expand Up @@ -87,15 +87,38 @@ class AdsCreateReadView(ListCreateAPIView):
permission_classes = (IsAuthenticatedOrReadOnly, )

def get_queryset(self):
if self.request.GET.get('category'):
cat = self.request.GET.get('category')
if cat == 'square':
return Ads.objects.filter(square=1)
if cat == 'horizontal':
return Ads.objects.filter(horizontal=1)
if cat == 'vertical':
return Ads.objects.filter(vertical=1)
return Ads.objects.all()
ads = []
placeholder = self.request.GET.get('placeholder', '')
ads_placeholders = getattr(settings, 'ADS_PLACEHOLDERS', None)

if placeholder == '' or placeholder in dict(ads_placeholders).keys():
category = self.request.GET.get('category', None)

if category is not None:
ads = Ads.objects.by_placeholder_and_category(
placeholder,
category
)
else:
ads = Ads.objects.all()

return ads


class AdsPlaceholdersReadView(ListAPIView):
serializer_class = AdsPlaceholdersSerializer
permission_classes = (IsAuthenticatedOrReadOnly, )

def get_queryset(self):
placeholders = getattr(settings, 'ADS_PLACEHOLDERS', None)

if placeholders is not None:
return [
Placeholder(code=code, label=label)
for code, label in placeholders
]
else:
return []


class AdsReadUpdateDeleteView(RetrieveUpdateDestroyAPIView):
Expand Down
Binary file removed blogs/__init__.pyc
Binary file not shown.
Binary file removed blogs/models.pyc
Binary file not shown.
Binary file removed blogs/urls.pyc
Binary file not shown.
Binary file removed blogs/views.pyc
Binary file not shown.
Binary file modified db.sqlite
Binary file not shown.
Empty file added logs/.keep
Empty file.
Binary file removed news/__init__.pyc
Binary file not shown.
Binary file removed news/models.pyc
Binary file not shown.
Binary file removed news/urls.pyc
Binary file not shown.
Binary file removed news/views.pyc
Binary file not shown.
Binary file removed pages/__init__.pyc
Binary file not shown.
Binary file removed pages/models.pyc
Binary file not shown.
Binary file removed pages/urls.pyc
Binary file not shown.
Binary file removed pages/views.pyc
Binary file not shown.
Binary file removed python_core/__init__.pyc
Binary file not shown.
64 changes: 64 additions & 0 deletions python_core/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Django settings for python_core project.

Expand Down Expand Up @@ -61,6 +62,7 @@
'skiclubs',
'widgets',
'angulation',
'multiselectfield',
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -142,3 +144,65 @@
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'spider': {
'format': '%(asctime)s - %(levelname)s - %(message)s'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'spider': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'spider'),
'when': 'midnight',
'formatter': 'spider',
'backupCount': 5,
},
},
'loggers': {
'dev': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
'spider': {
'handlers': ['console', 'spider'],
'level': 'DEBUG',
'propagate': True,
},
},
}


ADS_PLACEHOLDERS = (
('sides', "Toutes les pages, de chaque côté"),
('actu-top', "Page Actu, haut de page"),
('actu-side', "Page Actu, colonne droite"),
('actu-bottom', "Page Actu, base de page"),
('results-top', "Pages Résultats, haut de page"),
('results-side', "Pages Résultats, colonne droite"),
('results-bottom', "Pages Résultats, base de page"),
('blog-top', "Pages Blog, haut de page"),
('blog-side', "Pages Blog, colonne droite"),
('blog-bottom', "Pages Blog, base de page"),
('shop-top', "Page Angulation, haut de page"),
('shop-bottom', "Page Angulation, base de page"),
('sponsors-top', "Page Mentors & Sponsors, haut de page"),
('sponsors-side', "Page Mentors & Sponsors, colonne droite"),
('sponsors-bottom', "Page Mentors & Sponsors, base de page"),
)
Binary file removed python_core/settings.pyc
Binary file not shown.
53 changes: 53 additions & 0 deletions python_core/settings_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Note: Make sure that wsgi.py is referencing this file, in case of error 400.

Expand Down Expand Up @@ -144,3 +145,55 @@
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'spider': {
'format': '%(asctime)s - %(levelname)s - %(message)s'
}
},
'handlers': {
'spider': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'spider'),
'when': 'midnight',
'formatter': 'spider',
'backupCount': 15,
},
},
'loggers': {
'spider': {
'handlers': ['spider'],
'level': 'INFO',
'propagate': True,
},
},
}


ADS_PLACEHOLDERS = (
('sides', "Toutes les pages, de chaque côté"),
('actu-top', "Page Actu, haut de page"),
('actu-side', "Page Actu, colonne droite"),
('actu-bottom', "Page Actu, base de page"),
('results-top', "Pages Résultats, haut de page"),
('results-side', "Pages Résultats, colonne droite"),
('results-bottom', "Pages Résultats, base de page"),
('blog-top', "Pages Blog, haut de page"),
('blog-side', "Pages Blog, colonne droite"),
('blog-bottom', "Pages Blog, base de page"),
('shop-top', "Page Angulation, haut de page"),
('shop-bottom', "Page Angulation, base de page"),
('sponsors-top', "Page Mentors & Sponsors, haut de page"),
('sponsors-side', "Page Mentors & Sponsors, colonne droite"),
('sponsors-bottom', "Page Mentors & Sponsors, base de page"),
)
Binary file removed python_core/urls.pyc
Binary file not shown.
Binary file removed python_core/wsgi.pyc
Binary file not shown.
Binary file removed rankings/__init__.pyc
Binary file not shown.
Binary file removed rankings/fis/fis/__init__.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions rankings/fis/fis/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ class FisRaces(Item):
discipline = Field()
table = Field()

# def __repr__(self):
# """only print out few data after exiting the Pipeline"""
# return repr({
# "id": self["id"],
# "category": self["category"],
# "date": self["date"],
# "discipline": self["discipline"],
# "genre": self["genre"],
# "info": self["info"],
# "location": self["location"],
# "link": self["link"],
# })


class FisRanking(Item):
id = Field()
Expand Down
Binary file removed rankings/fis/fis/items.pyc
Binary file not shown.
Loading