Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Entrega do Desafio #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
5 changes: 5 additions & 0 deletions Ateliware_djangoProject/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
1 change: 1 addition & 0 deletions Ateliware_djangoProject/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn django_project.wsgi
1 change: 1 addition & 0 deletions Ateliware_djangoProject/Procfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python manage.py runserver 0.0.0.0:5000
7 changes: 7 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin

# I´m Registering our models here.
from .models import TbLanguages, TbGitRepository

admin.site.register(TbLanguages)
admin.site.register(TbGitRepository)
5 changes: 5 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AteliwareConfig(AppConfig):
name = 'ateliware_git_app'
76 changes: 76 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/ateliware_git_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Imports necessary tables and Github API class
from .models import TbLanguages, TbGitRepository
from github import Github
from datetime import datetime as dt
from os import environ
import pytz
# PyGitHub documentation: https://pygithub.readthedocs.io/en/latest/index.html


class GitAPI:

def __init__(self, lang_list):
"""
Initializes the class with local (self) variables
Input:
lang_list: list
languages list input by user
Outputs: class
(Initialization)
"""
# Temporarily disabled
# self.token = environ['GITHUB_API_TOKEN']
# self.git_api = Github(self.token) # Init Object Github API with token

self.git_api = Github() # Init Object Github API
self.tb_repo = TbGitRepository # TbGitRepository object
self.tb_lang = TbLanguages # TbLanguages object
# Creates a dictionary only with registered (allowed) languages
self.dict_lang = {lang[0]: lang[1] for lang in self.tb_lang.objects.all().values_list()}
self.allowed_lang = [val for val in self.dict_lang.values()] # All allowed languages
self.languages = [lang for lang in lang_list if lang in self.allowed_lang] # Languages chosen by user

def cad_or_up_repo(self):
"""
This method finds five repositories (top three by language)
"""
search_repos = list() # Creates a list to store found objects from Github API
for lang in self.languages: # For each language in allowed languages list
# Searches by repositories and sorts by number of stars to get most highlight ones
repositories = self.git_api.search_repositories(lang, sort='stars')

for repository in repositories: # For each repository found by API

# Creates a dictionary for repository main atributes except name
dict_repo = {'id_fk_lang': self.tb_lang.objects.filter(language=lang).first(),
'repo_url': repository.html_url,
'repo_stars': repository.stargazers_count,
'repo_commits': repository.get_commits().totalCount,
'repo_watchers': repository.watchers_count,
'repo_branches': repository.get_branches().totalCount,
'repo_forks': repository.get_forks().totalCount,
'repo_issues': repository.open_issues_count,
'repo_up_at': pytz.utc.localize(repository.updated_at)}

# Verifies if current repository is registered on database and update or create (keeps DB updated)
obj, created = self.tb_repo.objects.update_or_create(repo_name=repository.name, defaults=dict_repo)

if created: # If it was created, otherwise it updates existent register
# appends dict_repo to search list
search_repos.append([obj.repo_name, self.dict_lang[obj.id_fk_lang_id], obj.repo_url, obj.repo_stars,
obj.repo_commits, obj.repo_watchers, obj.repo_branches, obj.repo_forks,
obj.repo_issues, dt.strftime(obj.repo_up_at, '%d/%m/%Y - %Hh%M')])
break # Breaks the loop after finding a new repository by language

# Returns all five new registered repositories main data
return search_repos

def get_all_repo(self):
"""
A method that returns all registered repositories
Input: None
Output: list
list containing all repositories and their metadata
"""
return [[repo[2], self.dict_lang[repo[1]], repo[3], repo[4], repo[5], repo[6], repo[7], repo[8], repo[9],
dt.strftime(repo[10], '%d/%m/%Y - %Hh%M')] for repo in self.tb_repo.objects.all().values_list()]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.3 on 2021-05-30 20:35

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='TbLanguages',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('language', models.CharField(max_length=10)),
],
),
migrations.CreateModel(
name='TbGitRepository',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('repo_name', models.CharField(max_length=100, unique=True)),
('repo_url', models.CharField(max_length=250)),
('repo_stars', models.IntegerField()),
('repo_commits', models.IntegerField()),
('repo_watchers', models.IntegerField()),
('repo_branches', models.IntegerField()),
('repo_forks', models.IntegerField()),
('repo_issues', models.IntegerField()),
('repo_up_at', models.DateTimeField()),
('id_fk_lang', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='ateliware_git_app.tblanguages')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.3 on 2021-06-01 03:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ateliware_git_app', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='tblanguages',
name='language',
field=models.CharField(max_length=25),
),
]
36 changes: 36 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.db import models


class TbLanguages(models.Model):
"""
This class table stores all five languages selected for challenge:
['CSS', 'HTML', 'JavaScript', 'Python', 'Flutter']
Instead storing multiple times each language as char on TbGitRepository.
"""
language = models.CharField(max_length=25)


class TbGitRepository(models.Model):
"""
Such class table will store most important data about each git repository found:
id_fk_lang: Language id from TbLanguage, on delete does not delete lang ref.
repo_name: Git API attr = name ** MUST BE UNIQUE
repo_url: Git API attr = url
repo_stars: Git API attr = stargazers_count
repo_commits: Git API attr = get_commits().totalCount
repo_watchers: Git API attr = watchers_count
repo_branches: Git API attr = get_branches().totalCount
repo_forks: Git API attr = forks_count
repo_issues: Git API attr = open_issues_count
repo_up_at: Git API attr = updated_at
"""
id_fk_lang = models.ForeignKey(TbLanguages, on_delete=models.PROTECT)
repo_name = models.CharField(max_length=100, unique=True)
repo_url = models.CharField(max_length=250)
repo_stars = models.IntegerField(null=False)
repo_commits = models.IntegerField(null=False)
repo_watchers = models.IntegerField(null=False)
repo_branches = models.IntegerField(null=False)
repo_forks = models.IntegerField(null=False)
repo_issues = models.IntegerField(null=False)
repo_up_at = models.DateTimeField(null=False)
39 changes: 39 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.test import TestCase
from selenium import webdriver
from .ateliware_git_api import GitAPI
# Automatic tests for this awesome project!


class FunctionalTestCases(TestCase):
"""
Such class makes functional testes for front-end functionalities, for example pressing search repositories button
"""
def setUp(self):
self.browser = webdriver.Chrome()

def test_home_page(self):
"""
This test homepage oppening process
"""
self.browser.get('http://127.0.0.1:8000')
self.assertIn('Encontrar Repositórios Destaques em: CSS, HTML, Python, JS, Flutter', self.browser.page_source)


class UnitTestCases(TestCase):
"""
UnitTestCases tests back-end functionalities to assure front-end operations, for exemple:
Database tests;

"""

def test_github_class_template(self):
"""
Makes a test to validate template
"""
self.assertTrue(GitAPI([]).git_api)

def test_database(self):
"""
Tests database connection by selecting all values from a table
"""
self.assertTrue(GitAPI([]).tb_repo.objects.all().values_list())
17 changes: 17 additions & 0 deletions Ateliware_djangoProject/ateliware_git_app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .models import TbLanguages


def register_languages():
"""
This function registers some allowed linguages (chosen by author) into TbLanguages,
if they aren´t yet.
"""
languages = ['Python', 'Java', 'JavaScript', 'C#', 'C', 'C++', 'PHP', 'R', 'Objective-C', 'Swift', 'TypeScript',
'MATLAB', 'Kotlin', 'Go (Golang)', 'VBA', 'Ruby', 'Scala', 'Visual Basic', 'Rust', 'Dart', 'Ada',
'Lua', 'Abap', 'Groovy', 'Perl', 'Cobol', 'Julia', 'Haskell', 'Delphi', 'Elm', 'PowerShell', 'SQL',
'Clojure', 'Elixir', 'Pascal', 'LISP', 'Ballerina', 'FORTRAN', 'BASIC', 'Alice', 'COBOL', 'Speakeasy',
'Simula', 'Smalltalk', 'Prolog', 'Erlang', 'Eiffel', 'Rebol', 'Scratch', 'MicroPython', 'Flutter']
for lang in languages:
if not(TbLanguages.objects.filter(language=lang)):
TbLanguages(language=lang).save()

Loading