├── myproject
├── __init__.py
├── core
│ ├── __init__.py
│ ├── tests
│ │ ├── __init__.py
│ │ └── test_models.py
│ ├── migrations
│ │ ├── __init__.py
│ │ ├── 0002_auto_20190512_1938.py
│ │ └── 0001_initial.py
│ ├── static
│ │ ├── css
│ │ │ └── style.css
│ │ └── js
│ │ │ └── main.js
│ ├── apps.py
│ ├── templates
│ │ ├── core
│ │ │ ├── index.html
│ │ │ └── repos_list.html
│ │ ├── includes
│ │ │ └── nav.html
│ │ └── base.html
│ ├── admin.py
│ ├── urls.py
│ ├── models.py
│ └── views.py
├── urls.py
├── wsgi.py
└── settings.py
├── runtime.txt
├── Procfile
├── requirements.txt
├── manage.py
├── contrib
└── env_gen.py
├── README.md
└── .gitignore
/myproject/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/myproject/core/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/runtime.txt:
--------------------------------------------------------------------------------
1 | python-3.6.8
2 |
--------------------------------------------------------------------------------
/myproject/core/tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/myproject/core/migrations/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Procfile:
--------------------------------------------------------------------------------
1 | web: gunicorn myproject.wsgi --log-file -
2 |
--------------------------------------------------------------------------------
/myproject/core/static/css/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin-top: 60px;
3 | }
4 |
--------------------------------------------------------------------------------
/myproject/core/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class CoreConfig(AppConfig):
5 | name = 'core'
6 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | dj-database-url==0.5.0
2 | dj-static==0.0.6
3 | django-extensions==2.1.6
4 | Django==2.2.1
5 | gunicorn==19.9.0
6 | psycopg2==2.8.2
7 | python-decouple==3.1
--------------------------------------------------------------------------------
/myproject/urls.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from django.urls import include, path
3 |
4 | urlpatterns = [
5 | path('', include('myproject.core.urls')),
6 | path('admin/', admin.site.urls),
7 | ]
8 |
--------------------------------------------------------------------------------
/myproject/core/templates/core/index.html:
--------------------------------------------------------------------------------
1 | {% extends "base.html" %}
2 |
3 | {% block content %}
4 |
5 |
13 |
14 | {% endblock content %}
--------------------------------------------------------------------------------
/myproject/core/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from .models import Repo
3 |
4 |
5 | @admin.register(Repo)
6 | class RepoAdmin(admin.ModelAdmin):
7 | list_display = (
8 | 'slug',
9 | 'name',
10 | 'full_name',
11 | 'html_url',
12 | 'stargazers_count',
13 | )
14 | search_fields = ('name', 'full_name')
15 |
--------------------------------------------------------------------------------
/myproject/core/urls.py:
--------------------------------------------------------------------------------
1 | from django.urls import path
2 | from myproject.core import views as v
3 |
4 | app_name = 'core'
5 |
6 |
7 | urlpatterns = [
8 | path('', v.index, name="index"),
9 | path('repo/', v.repo_list, name="repo_list"),
10 | path('repo/json/', v.repo_json, name="repo_json"),
11 | path('repo/add/', v.repo_add, name="repo_add"),
12 | ]
13 |
--------------------------------------------------------------------------------
/myproject/core/migrations/0002_auto_20190512_1938.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 2.2.1 on 2019-05-12 22:38
2 |
3 | from django.db import migrations
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('core', '0001_initial'),
10 | ]
11 |
12 | operations = [
13 | migrations.RenameField(
14 | model_name='repo',
15 | old_name='htm_url',
16 | new_name='html_url',
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/myproject/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for myproject project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 | from dj_static import Cling
12 |
13 | from django.core.wsgi import get_wsgi_application
14 |
15 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
16 |
17 | application = Cling(get_wsgi_application())
18 |
--------------------------------------------------------------------------------
/myproject/core/tests/test_models.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 | from myproject.core.models import Repo
3 |
4 |
5 | class ContractTest(TestCase):
6 |
7 | def test_should_return_attributes(self):
8 | fields = (
9 | 'slug',
10 | 'name',
11 | 'full_name',
12 | 'htm_url',
13 | 'stargazers_count',
14 | )
15 |
16 | for field in fields:
17 | with self.subTest():
18 | self.assertTrue(hasattr(Repo, field))
19 |
--------------------------------------------------------------------------------
/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | """Django's command-line utility for administrative tasks."""
3 | import os
4 | import sys
5 |
6 |
7 | def main():
8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
9 | try:
10 | from django.core.management import execute_from_command_line
11 | except ImportError as exc:
12 | raise ImportError(
13 | "Couldn't import Django. Are you sure it's installed and "
14 | "available on your PYTHONPATH environment variable? Did you "
15 | "forget to activate a virtual environment?"
16 | ) from exc
17 | execute_from_command_line(sys.argv)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/contrib/env_gen.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | Django SECRET_KEY generator.
5 | """
6 | from django.utils.crypto import get_random_string
7 |
8 |
9 | chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
10 |
11 | CONFIG_STRING = """
12 | DEBUG=True
13 | SECRET_KEY=%s
14 | ALLOWED_HOSTS=127.0.0.1, .localhost
15 | #DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/NAME
16 | #DEFAULT_FROM_EMAIL=
17 | #EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
18 | #EMAIL_HOST=
19 | #EMAIL_PORT=
20 | #EMAIL_USE_TLS=
21 | #EMAIL_HOST_USER=
22 | #EMAIL_HOST_PASSWORD=
23 | """.strip() % get_random_string(50, chars)
24 |
25 | # Writing our configuration file to '.env'
26 | with open('.env', 'w') as configfile:
27 | configfile.write(CONFIG_STRING)
28 |
--------------------------------------------------------------------------------
/myproject/core/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 |
4 | class Repo(models.Model):
5 | slug = models.BigIntegerField(
6 | unique=True,
7 | help_text='slug is id of github api'
8 | )
9 | name = models.TextField()
10 | full_name = models.TextField()
11 | html_url = models.URLField()
12 | stargazers_count = models.PositiveIntegerField()
13 |
14 | class Meta:
15 | ordering = ('-stargazers_count',)
16 |
17 | def __str__(self):
18 | return self.name
19 |
20 | def to_dict_json(self):
21 | return {
22 | 'slug': self.slug,
23 | 'name': self.name,
24 | 'full_name': self.full_name,
25 | 'html_url': self.html_url,
26 | 'stargazers_count': self.stargazers_count,
27 | }
28 |
--------------------------------------------------------------------------------
/myproject/core/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 2.2.1 on 2019-05-12 20:58
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | initial = True
9 |
10 | dependencies = [
11 | ]
12 |
13 | operations = [
14 | migrations.CreateModel(
15 | name='Repo',
16 | fields=[
17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18 | ('slug', models.BigIntegerField(help_text='slug is id of github api', unique=True)),
19 | ('name', models.TextField()),
20 | ('full_name', models.TextField()),
21 | ('htm_url', models.URLField()),
22 | ('stargazers_count', models.PositiveIntegerField()),
23 | ],
24 | options={
25 | 'ordering': ('stargazers_count',),
26 | },
27 | ),
28 | ]
29 |
--------------------------------------------------------------------------------
/myproject/core/templates/includes/nav.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/myproject/core/static/js/main.js:
--------------------------------------------------------------------------------
1 | axios.defaults.xsrfHeaderName = 'X-CSRFToken'
2 | axios.defaults.xsrfCookieName = 'csrftoken'
3 | var app = new Vue({
4 | el: '#app',
5 | delimiters: ['${', '}'],
6 | data: {
7 | search: '',
8 | headers: [
9 | {title: 'id'},
10 | {title: 'name'},
11 | {title: 'full_name'},
12 | {title: 'html_url'},
13 | {title: 'stargazers_count'},
14 | ],
15 | username: 'rg3915',
16 | items: []
17 | },
18 | methods: {
19 | getReposJson () {
20 | axios.get('/repo/json/')
21 | .then(result => {
22 | this.items = result.data.data
23 | })
24 | },
25 | getRepos (username) {
26 | const url = 'https://api.github.com/users/' + username + '/starred'
27 | axios.get(url)
28 | .then(result => {
29 | this.items = result.data
30 | })
31 | },
32 | saveRepos() {
33 | let bodyFormData = new FormData()
34 | bodyFormData.append('item', JSON.stringify(this.items))
35 | axios.post('/repo/add/', bodyFormData)
36 | .then(response => {
37 | this.items = response.data.data
38 | })
39 | }
40 | },
41 | mounted () {
42 | this.getReposJson()
43 | },
44 | computed: {
45 | filteredItems () {
46 | return this.items.filter((item) => {
47 | return item.name.toLowerCase().indexOf(this.search.toLowerCase())>=0;
48 | });
49 | }
50 | }
51 | });
--------------------------------------------------------------------------------
/myproject/core/views.py:
--------------------------------------------------------------------------------
1 | import json
2 | from django.http import JsonResponse
3 | from django.shortcuts import render
4 | from django.views.decorators.csrf import csrf_exempt
5 | from .models import Repo
6 |
7 |
8 | def index(request):
9 | template_name = 'core/index.html'
10 | return render(request, template_name)
11 |
12 |
13 | def repo_list(request):
14 | template_name = 'core/repos_list.html'
15 | return render(request, template_name)
16 |
17 |
18 | def repo_json(request):
19 | ''' Retorna um JSON dos repos. '''
20 | repos = Repo.objects.all()
21 | data = [item.to_dict_json() for item in repos]
22 | return JsonResponse({'data': data})
23 |
24 |
25 | @csrf_exempt
26 | def repo_add(request):
27 | if request.method == 'POST':
28 | Repo.objects.all().delete()
29 | response = request.POST
30 | items = json.loads(response['item'])
31 | aux = []
32 | for item in items:
33 | obj = Repo(
34 | slug=item['id'],
35 | name=item['name'],
36 | full_name=item['full_name'],
37 | html_url=item['html_url'],
38 | stargazers_count=item['stargazers_count'],
39 | )
40 | aux.append(obj)
41 | Repo.objects.bulk_create(aux)
42 | repos = Repo.objects.all()
43 | data = [item.to_dict_json() for item in repos]
44 | return JsonResponse({'data': data})
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # aw-challenge
2 |
3 | AW Challenge
4 |
5 | ## Objetivo
6 |
7 | Construir uma nova aplicação, utilizando o framework de sua preferência, a qual deverá conectar na API do GitHub e disponibilizar as seguintes funcionalidades:
8 |
9 | - Botão para buscar e armazenar os repositórios destaques de 5 linguagens à sua escolha;
10 | - Listar os repositórios encontrados;
11 | - Visualizar os detalhes de cada repositório.
12 |
13 | Alguns requisitos:
14 |
15 | - Deve ser uma aplicação totalmente nova;
16 | - A solução deve estar em um repositório público do GitHub;
17 | - A aplicação deve armazenar as informações encontradas;
18 | - Utilizar PostgreSQL, MySQL ou SQL Server;
19 | - O deploy deve ser realizado, preferencialmente, no Heroku ou no Azure;
20 | - A aplicação precisa ter testes automatizados.
21 |
22 | ## Clonando e rodando a api localmente
23 |
24 | * Clone esse repositório.
25 | * Crie um virtualenv com Python 3.
26 | * Ative o virtualenv.
27 | * Instale as dependências.
28 |
29 | ```
30 | git clone https://github.com/rg3915/aw-challenge.git
31 | cd aw-challenge
32 | python3 -m venv .venv
33 | source .venv/bin/activate
34 | pip install -r requirements.txt
35 | python contrib/env_gen.py
36 | python manage.py test
37 | python manage.py runserver
38 | ```
39 |
40 | ## App
41 |
42 | https://aw-challenge-regis.herokuapp.com/
43 |
44 | ## O que foi usado
45 |
46 | * Python 3.6.6
47 | * Django 2.2.1
48 | * VueJS 2.6.10
49 | * Bootstrap 4.0
50 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
--------------------------------------------------------------------------------
/myproject/core/templates/base.html:
--------------------------------------------------------------------------------
1 |
2 | {% load static %}
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | API Github
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | {% block css %}{% endblock css %}
20 |
21 |
22 |
23 |
24 | {% include "includes/nav.html" %}
25 |
26 |
27 | {% block content %}{% endblock content %}
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | {% block js %}{% endblock js %}
42 |
43 |
--------------------------------------------------------------------------------
/myproject/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for myproject project.
3 |
4 | Generated by 'django-admin startproject' using Django 2.2.1.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.2/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/2.2/ref/settings/
11 | """
12 |
13 | import os
14 | from decouple import config, Csv
15 | from dj_database_url import parse as dburl
16 |
17 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
18 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19 |
20 |
21 | # Quick-start development settings - unsuitable for production
22 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
23 |
24 | # SECURITY WARNING: keep the secret key used in production secret!
25 | SECRET_KEY = config('SECRET_KEY')
26 |
27 | # SECURITY WARNING: don't run with debug turned on in production!
28 | DEBUG = config('DEBUG', default=False, cast=bool)
29 |
30 | ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())
31 |
32 |
33 | # Application definition
34 |
35 | INSTALLED_APPS = [
36 | 'django.contrib.admin',
37 | 'django.contrib.auth',
38 | 'django.contrib.contenttypes',
39 | 'django.contrib.sessions',
40 | 'django.contrib.messages',
41 | 'django.contrib.staticfiles',
42 | 'django_extensions',
43 | 'myproject.core',
44 | ]
45 |
46 | MIDDLEWARE = [
47 | 'django.middleware.security.SecurityMiddleware',
48 | 'django.contrib.sessions.middleware.SessionMiddleware',
49 | 'django.middleware.common.CommonMiddleware',
50 | 'django.middleware.csrf.CsrfViewMiddleware',
51 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
52 | 'django.contrib.messages.middleware.MessageMiddleware',
53 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
54 | ]
55 |
56 | ROOT_URLCONF = 'myproject.urls'
57 |
58 | TEMPLATES = [
59 | {
60 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
61 | 'DIRS': [],
62 | 'APP_DIRS': True,
63 | 'OPTIONS': {
64 | 'context_processors': [
65 | 'django.template.context_processors.debug',
66 | 'django.template.context_processors.request',
67 | 'django.contrib.auth.context_processors.auth',
68 | 'django.contrib.messages.context_processors.messages',
69 | ],
70 | },
71 | },
72 | ]
73 |
74 | WSGI_APPLICATION = 'myproject.wsgi.application'
75 |
76 |
77 | # Database
78 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
79 |
80 | default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
81 | DATABASES = {
82 | 'default': config('DATABASE_URL', default=default_dburl, cast=dburl),
83 | }
84 |
85 |
86 | # Password validation
87 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
88 |
89 | AUTH_PASSWORD_VALIDATORS = [
90 | {
91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92 | },
93 | {
94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95 | },
96 | {
97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98 | },
99 | {
100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101 | },
102 | ]
103 |
104 |
105 | # Internationalization
106 | # https://docs.djangoproject.com/en/2.2/topics/i18n/
107 |
108 | LANGUAGE_CODE = 'pt-br'
109 |
110 | TIME_ZONE = 'America/Sao_Paulo'
111 |
112 | USE_I18N = True
113 |
114 | USE_L10N = True
115 |
116 | USE_TZ = True
117 |
118 |
119 | # Static files (CSS, JavaScript, Images)
120 | # https://docs.djangoproject.com/en/2.2/howto/static-files/
121 |
122 | STATIC_URL = '/static/'
123 | STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
124 |
--------------------------------------------------------------------------------
/myproject/core/templates/core/repos_list.html:
--------------------------------------------------------------------------------
1 | {% extends "base.html" %}
2 | {% load static %}
3 |
4 | {% block css %}
5 |
6 |
11 |
12 | {% endblock css %}
13 |
14 | {% block content %}
15 |
16 |
17 |
18 |
19 |
Do lado esquerdo você pode digitar um username do Github e clicar em listar que ele vai listar os repositórios na tela.
20 |
Do lado direito ele busca pelos repositórios salvos no banco de dados.
21 |
O botão 'Salvar todos' salva todos os repositórios no banco de dados.
22 |
Clique no id para ver os detalhes.
23 |
24 |
25 |
26 |
31 |
32 |
33 |
37 |
38 |
39 |
40 |
107 |
108 |
109 | {% endblock content %}
110 |
111 | {% block js %}
112 |
113 |
114 |
115 | {% endblock js %}
--------------------------------------------------------------------------------