Skip to content

Commit

Permalink
Merge pull request #277 from Normal-OJ/AddIPRecord
Browse files Browse the repository at this point in the history
feat: add IP record
  • Loading branch information
Bogay authored Nov 6, 2023
2 parents 7f2df61 + 0fa2522 commit 3a9a353
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 635 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- uses: actions/checkout@v3
- name: Install Poetry
run: pipx install poetry
- name: Setup Python 3.8
- name: Setup Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.8"
python-version: "3.11"
cache: "poetry"
- name: Install Dependency
run: poetry install
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/generate-coverage-badge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- uses: actions/checkout@v3
- name: Install Poetry
run: pipx install poetry
- name: Setup Python 3.8
- name: Setup Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.8"
python-version: "3.11"
cache: "poetry"
- name: Install Dependency
run: poetry install
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ref: https://github.com/adamwojt/ur_l/blob/c748fff814f8cffcc979020008aab460a0de9d50/python_docker/Dockerfile
FROM python:3.8-slim as python-base
FROM python:3.11-slim as python-base
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
Expand All @@ -17,7 +17,7 @@ FROM python-base as builder-base
RUN apt-get update && apt-get install --no-install-recommends -y curl

# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
ENV POETRY_VERSION=1.3.2
ENV POETRY_VERSION=1.6.1
RUN curl -sSL https://install.python-poetry.org | python -

# We copy our Python requirements here to cache them
Expand Down
53 changes: 30 additions & 23 deletions model/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ def create_submission(user, language_type, problem_id):
user.save()
# insert submission to DB
try:
submission = Submission.add(
problem_id=problem_id,
username=user.username,
lang=language_type,
timestamp=now,
)
submission = Submission.add(problem_id=problem_id,
username=user.username,
lang=language_type,
timestamp=now,
ip_addr=request.remote_addr)
except ValidationError:
return HTTPError('invalid data!', 400)
except engine.DoesNotExist as e:
Expand All @@ -126,17 +125,8 @@ def create_submission(user, language_type, problem_id):

@submission_api.route('/', methods=['GET'])
@login_required
@Request.args(
'offset',
'count',
'problem_id',
'username',
'status',
'language_type',
'course',
'before',
'after',
)
@Request.args('offset', 'count', 'problem_id', 'username', 'status',
'language_type', 'course', 'before', 'after', 'ip_addr')
def get_submission_list(
user,
offset,
Expand All @@ -148,6 +138,7 @@ def get_submission_list(
before,
after,
language_type,
ip_addr,
):
'''
get the list of submission data
Expand All @@ -161,6 +152,22 @@ def parse_int(val: Optional[int], name: str):
except ValueError:
raise ValueError(f'can not convert {name} to integer')

def parse_str(val: Optional[str], name: str):
if val is None:
return None
try:
return str(val)
except ValueError:
raise ValueError(f'can not convert {name} to string')

def parse_timestamp(val: Optional[int], name: str):
if val is None:
return None
try:
return datetime.fromtimestamp(val)
except ValueError:
raise ValueError(f'can not convert {name} to timestamp')

cache_key = (
'SUBMISSION_LIST_API',
user,
Expand All @@ -174,6 +181,7 @@ def parse_int(val: Optional[int], name: str):
before,
after,
)

cache_key = '_'.join(map(str, cache_key))
cache = RedisCache()
# check cache
Expand All @@ -187,12 +195,10 @@ def parse_int(val: Optional[int], name: str):
count = parse_int(count, 'count')
problem_id = parse_int(problem_id, 'problemId')
status = parse_int(status, 'status')
before = parse_int(before, 'before')
if before is not None:
before = datetime.fromtimestamp(before)
after = parse_int(after, 'after')
if after is not None:
after = datetime.fromtimestamp(after)
before = parse_timestamp(before, 'before')
after = parse_timestamp(after, 'after')
ip_addr = parse_str(ip_addr, 'ip_addr')

if language_type is not None:
try:
language_type = list(map(int, language_type.split(',')))
Expand All @@ -216,6 +222,7 @@ def parse_int(val: Optional[int], name: str):
'course': course,
'before': before,
'after': after,
'ip_addr': ip_addr
})
submissions, submission_count = Submission.filter(
**params,
Expand Down
1 change: 1 addition & 0 deletions mongo/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ class Submission(Document):
code = ZipField(required=True, null=True, max_size=10**7)
last_send = DateTimeField(db_field='lastSend', default=datetime.now)
comment = FileField(default=None, null=True)
ip_addr = StringField(default=None, null=True)


@escape_markdown.apply
Expand Down
16 changes: 10 additions & 6 deletions mongo/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ def filter(
after: Optional[datetime] = None,
sort_by: Optional[str] = None,
with_count: bool = False,
ip_addr: Optional[str] = None,
):
if before is not None and after is not None:
if after > before:
Expand Down Expand Up @@ -581,6 +582,7 @@ def filter(
'status': status,
'language__in': language_type,
'user': q_user,
'ip_addr': ip_addr,
'timestamp__lte': before,
'timestamp__gte': after,
}
Expand All @@ -606,6 +608,7 @@ def add(
username: str,
lang: int,
timestamp: Optional[date] = None,
ip_addr: Optional[str] = None,
) -> 'Submission':
'''
Insert a new submission into db
Expand All @@ -625,12 +628,11 @@ def add(
if timestamp is None:
timestamp = datetime.now()
# create a new submission
submission = engine.Submission(
problem=problem.obj,
user=user.obj,
language=lang,
timestamp=timestamp,
)
submission = engine.Submission(problem=problem.obj,
user=user.obj,
language=lang,
timestamp=timestamp,
ip_addr=ip_addr)
submission.save()
return cls(submission.id)

Expand Down Expand Up @@ -671,13 +673,15 @@ def _to_dict(self) -> SON:
'submissionId': str(self.id),
'timestamp': self.timestamp.timestamp(),
'lastSend': self.last_send.timestamp(),
'ipAddr': self.ip_addr,
}
old = [
'_id',
'problem',
'code',
'comment',
'tasks',
'ip_addr',
]
# delete old keys
for o in old:
Expand Down
Loading

0 comments on commit 3a9a353

Please sign in to comment.