Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
.DS_Store
7 changes: 7 additions & 0 deletions source/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
6 changes: 6 additions & 0 deletions source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## What needs to be done to run the project:

1. docker-compose build
2. docker-compose run web ./manage.py migrate
3. docker-compose run web ./manage.py createsuperuser
4. docker-compose up
Empty file added source/commits/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions source/commits/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions source/commits/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import unicode_literals

from django.apps import AppConfig


class ApigithubConfig(AppConfig):
name = 'apigithub'
30 changes: 30 additions & 0 deletions source/commits/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-03-15 09:40
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Commits',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sha', models.CharField(default='', max_length=200)),
('author', models.CharField(blank=True, default='', max_length=200)),
('pub_date', models.DateTimeField()),
('text', models.TextField()),
('read_status', models.BooleanField(default=False)),
],
options={
'db_table': 'commits',
},
),
]
Empty file.
16 changes: 16 additions & 0 deletions source/commits/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Commits(models.Model):
class Meta():
db_table = 'commits'


sha = models.CharField(max_length=200, default='')
author = models.CharField(max_length=200, blank=True, default='')
pub_date = models.DateTimeField()
text = models.TextField()
read_status = models.BooleanField(default=False)

37 changes: 37 additions & 0 deletions source/commits/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from celery.utils.log import get_task_logger

from django.utils import timezone

from .models import Commits
from urllib2 import urlopen
from dateutil.parser import parse

import json

logger = get_task_logger(__name__)


@periodic_task(ignore_result=True, run_every=(crontab(hour="*", minute=timezone.now().minute+1, day_of_week="*")))
def get_latest_commit(owner='nodejs', repo='node'):
logger.info("Start task")
url = 'https://api.github.com/repos/{owner}/{repo}/commits'.format(owner=owner, repo=repo)
response = urlopen(url).read()
data = json.loads(response.decode('UTF-8'))
list = data[:25]
for el in list:
sha = el['sha']
name = el['commit']['author']['name']
msg = el['commit']['message'].encode('ascii','ignore')
date = parse(el['commit']['author']['date'])
try:
if not Commits.objects.filter(sha=sha):
comment = Commits(author=name, text=msg, pub_date=date, read_status=False, sha=sha)
comment.save()
logger.info("Get!!!")
except Exception as ex:
print('ex is: {}'.format(ex))
logger.info("End task success!!!")
return list

34 changes: 34 additions & 0 deletions source/commits/templates/commits.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends 'index.html' %}

{% block commits %}
<div>
<h1>Commits!</h1>
{% for commit in commits %}
<p><strong>{{ commit.author }}</strong> - {{ commit.pub_date }}</p>
<p>{{ commit.text }}</p>
Read - <a href="/readed/{{ commit.id }}/{{ commits.number }}/">{{ commit.read_status }}</a>
<hr>
{% endfor %}
</div>
<div align="center">
{% if commits.has_previous %}
<a style="width: 15px;" href="/page/{{ commits.previous_page_number }}/"><span> < </span></a>
{% else %}
<a href=""><</a>
{% endif %}
{% for page in commits.paginator.page_range %}
{% if page == commits.number %}
<a href="/page/{{ page }}/">{{ page }}</a>
{% else %}
<a href="/page/{{ page }}/">{{ page }}</a>
{% endif %}
{% endfor %}
{% if commits.has_next %}
<a href="/page/{{ commits.next_page_number }}/">></a>
{% else %}
<a href="">></a>
{% endif %}

</div>
{% endblock %}

25 changes: 25 additions & 0 deletions source/commits/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from urllib2 import urlopen
import json

from django.test import TestCase

from .tasks import get_latest_commit
from .models import Commits


class TasksMethodTests(TestCase):

def test_get_latest_commit(self):
"""
test_get_latest_commit - checking get_latest_commit saved only new commits
"""
error = ''
list = get_latest_commit()
for el in list:
sha = el['sha']
try:
if not Commits.objects.get(sha=sha):
error = 'object not exist'
except Exception as ex:
error = 'there are duplicates'
self.assertEqual(error, '')
24 changes: 24 additions & 0 deletions source/commits/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""composeexample URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from views import commits, readed


urlpatterns = [
url(r'^page/(\d+)/$', commits),
url(r'^readed/(?P<commit_id>\d+)/(?P<page_number>\d+)/$', readed),
url(r'^$', commits),
]
30 changes: 30 additions & 0 deletions source/commits/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.shortcuts import render_to_response, redirect
from django.http.response import Http404
from django.core.paginator import Paginator
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone

from .models import Commits

# Create your views here.

def commits(request, page_number=1):

all_commits = Commits.objects.order_by('-pub_date')
current_page = Paginator(all_commits, 5)
print(timezone.now().minute)
return render_to_response('commits.html', {'commits': current_page.page(page_number)})

def readed(request, commit_id, page_number):
try:
commits = Commits.objects.get(id=commit_id)
if commits.read_status:
commits.read_status = False
else:
commits.read_status = True
commits.save()
response = redirect('/page/'+page_number+"/")
return response
except ObjectDoesNotExist:
raise Http404
return redirect('/page/'+page_number+'/')
Binary file added source/db.sqlite3
Binary file not shown.
31 changes: 31 additions & 0 deletions source/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: '2'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
# RabbitMQ
rabbit:
hostname: rabbit
image: rabbitmq:3.6.0
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=mypass
ports:
- "5672:5672" # we forward this port because it's useful for debugging
- "15672:15672" # here, we can access rabbitmq management plugin
celery:
build: .
environment:
- C_FORCE_ROOT=true
command: python manage.py celeryd -l INFO -B
volumes:
- .:/code

Empty file added source/github/__init__.py
Empty file.
Loading