From a1d87a8ac7f03566f44a988b3ac6cce39913aa79 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Fri, 19 Jan 2024 18:14:41 +0330 Subject: [PATCH 1/3] Fix Allowlist constraint --- core/constraints.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/core/constraints.py b/core/constraints.py index 0f67cf40..50af91d5 100644 --- a/core/constraints.py +++ b/core/constraints.py @@ -18,7 +18,7 @@ class ConstraintParam(Enum): USERNAME = "username" FROM_DATE = "from_date" TO_DATE = "to_date" - WALLETS_CSV_FILE = "wallets_csv_file" + CSV_FILE = "csv_file" COLLECTION_ADDRESS = "collection_address" MINIMUM = "minimum" @@ -26,11 +26,6 @@ class ConstraintParam(Enum): def choices(cls): return [(key.value, key.name) for key in cls] - @classmethod - def is_valid_wallets_csv_file(cls): - # TODO: fixme - pass - class ConstraintVerification(ABC): _param_keys = [] @@ -159,19 +154,19 @@ def is_observed(self, *args, **kwargs): # return token_count >= int(minimum) -class AllowList(ConstraintVerification): - _param_keys = [ConstraintParam.WALLETS_CSV_FILE] +class AllowListVerification(ConstraintVerification): + _param_keys = [ConstraintParam.CSV_FILE] - def __init__(self, user_profile, response: str = None) -> None: - super().__init__(user_profile, response) + def __init__(self, user_profile) -> None: + super().__init__(user_profile) def is_observed(self, *args, **kwargs): - file_path = self._param_values[ConstraintParam.WALLETS_CSV_FILE] + file_path = self._param_values[ConstraintParam.CSV_FILE.name] self.allow_list = [] with open(file_path, newline="") as f: reader = csv.reader(f) - self.allow_list = list(reader) - self.allow_list = [a.lower() for a in self.allow_list] + data = list(reader) + self.allow_list = [a[0].lower() for a in data] user_wallets = self.user_profile.wallets.values_list( Lower("address"), flat=True ) From 7ee04aea13ed2333c173b29db69e2bbd61be9438 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Fri, 19 Jan 2024 18:16:39 +0330 Subject: [PATCH 2/3] Add AllowList constraint to models --- core/models.py | 2 ++ .../migrations/0052_alter_constraint_name.py | 18 ++++++++++++++++++ .../migrations/0032_alter_constraint_name.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 prizetap/migrations/0052_alter_constraint_name.py create mode 100644 tokenTap/migrations/0032_alter_constraint_name.py diff --git a/core/models.py b/core/models.py index 872e872a..3f75a574 100644 --- a/core/models.py +++ b/core/models.py @@ -12,6 +12,7 @@ from faucet.faucet_manager.lnpay_client import LNPayClient from .constraints import ( + AllowListVerification, BrightIDAuraVerification, BrightIDMeetVerification, HasNFTVerification, @@ -70,6 +71,7 @@ class Type(models.TextChoices): BrightIDMeetVerification, BrightIDAuraVerification, HasNFTVerification, + AllowListVerification, ] name = models.CharField( diff --git a/prizetap/migrations/0052_alter_constraint_name.py b/prizetap/migrations/0052_alter_constraint_name.py new file mode 100644 index 00000000..a26ec7f3 --- /dev/null +++ b/prizetap/migrations/0052_alter_constraint_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2024-01-17 10:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('prizetap', '0051_alter_raffle_twitter_url'), + ] + + operations = [ + migrations.AlterField( + model_name='constraint', + name='name', + field=models.CharField(choices=[('core.BrightIDMeetVerification', 'BrightIDMeetVerification'), ('core.BrightIDAuraVerification', 'BrightIDAuraVerification'), ('core.HasNFTVerification', 'HasNFTVerification'), ('core.AllowListVerification', 'AllowListVerification'), ('prizetap.HaveUnitapPass', 'HaveUnitapPass'), ('prizetap.NotHaveUnitapPass', 'NotHaveUnitapPass'), ('faucet.OptimismDonationConstraint', 'OptimismDonationConstraint'), ('faucet.OptimismClaimingGasConstraint', 'OptimismClaimingGasConstraint')], max_length=255, unique=True), + ), + ] diff --git a/tokenTap/migrations/0032_alter_constraint_name.py b/tokenTap/migrations/0032_alter_constraint_name.py new file mode 100644 index 00000000..dc47f543 --- /dev/null +++ b/tokenTap/migrations/0032_alter_constraint_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2024-01-19 14:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tokenTap', '0031_tokendistribution_constraint_params_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='constraint', + name='name', + field=models.CharField(choices=[('core.BrightIDMeetVerification', 'BrightIDMeetVerification'), ('core.BrightIDAuraVerification', 'BrightIDAuraVerification'), ('core.HasNFTVerification', 'HasNFTVerification'), ('core.AllowListVerification', 'AllowListVerification'), ('tokenTap.OncePerMonthVerification', 'OncePerMonthVerification'), ('tokenTap.OnceInALifeTimeVerification', 'OnceInALifeTimeVerification'), ('faucet.OptimismHasClaimedGasInThisRound', 'OptimismHasClaimedGasInThisRound')], max_length=255, unique=True), + ), + ] From eff346964891ea47f7ed6447da48c39559d66387 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Fri, 19 Jan 2024 18:23:10 +0330 Subject: [PATCH 3/3] File upload controller --- core/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/utils.py b/core/utils.py index 399f8afa..43507a0c 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,11 +1,15 @@ import datetime import logging +import os import time +import uuid from contextlib import contextmanager import pytz import web3.exceptions from django.core.cache import cache +from django.core.files.storage import default_storage +from django.core.files.uploadedfile import UploadedFile from eth_account.messages import encode_defunct from solana.rpc.api import Client from web3 import Account, Web3 @@ -14,6 +18,7 @@ from web3.middleware import geth_poa_middleware from web3.types import TxParams, Type +from brightIDfaucet.settings import MEDIA_ROOT from core.constants import ERC721_READ_METHODS @@ -243,3 +248,20 @@ def get_number_of_tokens(self, address: str): def to_checksum_address(self, address: str): return self.web3_utils.w3.to_checksum_address(address) + + +class UploadFileStorage: + BASE_PATH = time.strftime("%Y%m%d") + "/" + + def __init__(self, upload_to=None) -> None: + self.upload_to = upload_to or self.BASE_PATH + + def save(self, file: UploadedFile): + _, file_extension = os.path.splitext(file.name) + milliseconds = round(time.time() * 1000) + time_str = time.strftime("%H%M%S") + "-" + str(milliseconds) + file_name = time_str + "-" + str(uuid.uuid4()) + path = default_storage.save( + str(self.upload_to or "") + file_name + file_extension, file + ) + return MEDIA_ROOT + "/" + path