-
-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GITC-478: Caching github user object. (#9804)
* GITC-478: Caching github user object. Have created model to store the serialized github entities. Have also reworked code, mainly in the `git/utils.py` file to make use of this cache. * GITC-478: remove logs for sensitive data * GITC-478: Adding tests
- Loading branch information
Showing
9 changed files
with
295 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Define the Grant admin layout. | ||
Copyright (C) 2021 Gitcoin Core | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
from django.contrib import admin | ||
from git.models import GitCache | ||
|
||
class GitCacheAdmin(admin.ModelAdmin): | ||
list_display = ['pk', 'category', 'handle'] | ||
search_fields = [ | ||
'id', 'handle' | ||
] | ||
|
||
admin.site.register(GitCache, GitCacheAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Generated by Django 2.2.24 on 2021-12-01 11:50 | ||
|
||
from django.db import migrations, models | ||
import economy.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='GitCache', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)), | ||
('modified_on', models.DateTimeField(default=economy.models.get_time)), | ||
('handle', models.CharField(blank=True, max_length=200)), | ||
('category', models.CharField(blank=True, choices=[('user', 'User'), ('repo', 'Repository')], max_length=20)), | ||
('data', models.BinaryField()), | ||
], | ||
options={ | ||
'unique_together': {('handle', 'category')}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
|
||
# -*- coding: utf-8 -*- | ||
"""Define models. | ||
Copyright (C) 2021 Gitcoin Core | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
import logging | ||
|
||
from django.db import models | ||
|
||
from economy.models import SuperModel | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GitCache(SuperModel): | ||
"""Model used for storing serialized pygithub objects. | ||
Attributes: | ||
handle (str): The unique (within a category) handle for the data | ||
category (str): the category of the data (user, repo, ...). | ||
data (binary): The serialized object data. | ||
""" | ||
|
||
class Category: | ||
USER = "user" | ||
REPO = "repo" | ||
|
||
CATEGORY_CHOICES = [ | ||
(Category.USER, 'User'), | ||
(Category.REPO, 'Repository'), | ||
] | ||
|
||
handle = models.CharField(max_length=200, null=False, blank=True) | ||
category = models.CharField(max_length=20, null=False, blank=True, choices=CATEGORY_CHOICES) | ||
data = models.BinaryField() | ||
|
||
class Meta: | ||
unique_together = ["handle", "category"] | ||
|
||
def __str__(self): | ||
"""Return the string representation of a model.""" | ||
return f"[{self.category}] {self.handle}" | ||
|
||
@classmethod | ||
def get_user(self, handle): | ||
"""Utility function to retreive a user object""" | ||
try: | ||
return self.objects.get(category=GitCache.Category.USER, handle=handle) | ||
except self.DoesNotExist: | ||
raise | ||
|
||
def update_data(self, data): | ||
"""Update the data field if it has changed.""" | ||
if self.data != data: | ||
self.data = data | ||
self.save() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import factory | ||
import pytest | ||
from git.models import GitCache | ||
|
||
|
||
@pytest.mark.django_db | ||
class GitCacheFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = GitCache | ||
|
||
# Unique user handle | ||
handle = factory.Sequence(lambda n: f"user_handle_{n}") | ||
|
||
# Cycle through the choices and select one | ||
category = factory.Sequence(lambda n: GitCache.CATEGORY_CHOICES[n % len(GitCache.CATEGORY_CHOICES)][0]) | ||
|
||
# Generate binary data depending on n | ||
data = factory.Sequence(lambda n: ("{n}" * 100).encode("utf-8")) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from git.tests.factories.git_cache_factory import GitCacheFactory | ||
import pytest | ||
from git.models import GitCache | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestGitCache: | ||
"""Test CLRMatch model.""" | ||
|
||
def test_creation(self): | ||
"""Test GitCache returned by factory is valid.""" | ||
|
||
git_cache = GitCacheFactory() | ||
|
||
assert isinstance(git_cache, GitCache) | ||
|
||
def test_get_user(self): | ||
"""Test get_user helper function.""" | ||
|
||
git_cache = GitCacheFactory() | ||
git_cache.category = GitCache.Category.USER | ||
handle = git_cache.handle | ||
git_cache.save() | ||
|
||
saved = GitCache.get_user(handle) | ||
assert git_cache.id == saved.id | ||
|
||
def test_update_data(self): | ||
"""Test update_data helper function.""" | ||
|
||
git_cache = GitCacheFactory() | ||
git_cache.category = GitCache.Category.USER | ||
handle = git_cache.handle | ||
git_cache.save() | ||
|
||
new_data = "This is updated data".encode("utf-8") | ||
git_cache.update_data(new_data) | ||
|
||
saved = GitCache.get_user(handle) | ||
assert new_data == saved.data.tobytes() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters