Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: allow to update team #16716

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

kraktus
Copy link
Member

@kraktus kraktus commented Jan 3, 2025

close #11380 but is more general, all fields but flairs are supported

testing code

#!/usr/local/bin/python3
#coding: utf-8
# Licence: GNU AGPLv3

""""""

from __future__ import annotations

import argparse
import json
import logging
import logging.handlers
import os
import requests
import sys

from dataclasses import dataclass
from datetime import datetime
from collections import deque
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from pathlib import Path
from typing import Optional, List, Union, Tuple

#############
# Constants #
#############

TOKEN = "lip_FZiM3V5ydJN3BrMXtqgk" # localhost, sorry!

LOG_PATH = f"{__file__}.log"
RETRY_STRAT = Retry(
    total=5,
    backoff_factor=1,
    status_forcelist=[429, 500, 502, 503, 504],
    allowed_methods=["GET"]
)
ADAPTER = HTTPAdapter(max_retries=RETRY_STRAT)

########
# Logs #
########

log = logging.getLogger(__file__)
log.setLevel(logging.DEBUG)
format_string = "%(asctime)s | %(levelname)-8s | %(message)s"

# 125000000 bytes = 12.5Mb
handler = logging.handlers.RotatingFileHandler(LOG_PATH, maxBytes=12500000, backupCount=3, encoding="utf8")
handler.setFormatter(logging.Formatter(format_string))
handler.setLevel(logging.DEBUG)
log.addHandler(handler)

handler_2 = logging.StreamHandler(sys.stdout)
handler_2.setFormatter(logging.Formatter(format_string))
handler_2.setLevel(logging.INFO)
if __debug__:
    handler_2.setLevel(logging.DEBUG)
log.addHandler(handler_2)

###########
# Classes #
###########

class Req:

    def __init__(self) -> None:
        http = requests.Session()
        http.mount("https://", ADAPTER)
        http.mount("http://", ADAPTER)
        self.http = http

        print("editing team private desc")
        self.edit_field("descPrivate", "This is API edited!")
        print("editing password")
        self.edit_field("password", "secure")
        print("removing password")
        self.edit_field("password", "")
        print("unknown key")
        self.edit_field("foo barz!!", "")

    def edit_field(self, field: str, value: Any) -> None:
        r = self.http.post(f"http://localhost:9663/api/team/entry-code-team/update/{field}", 
                            headers = {"Authorization": f"Bearer {TOKEN}"},data={field: value})
        print(r)

def main() -> None:
    Req()

########
# Main #
########

if __name__ == "__main__":
    print('#'*80)
    main()

given fieldsInstance: TeamForm.Fields.type = TeamForm.Fields
private def changing[A] = lila.common.Form.SingleChange.changing[Team, TeamForm.Fields.type, A]

val changes: Map[String, Change[?]] = List[Change[?]](
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flair not added, because it requires the flair api, and was not sure how to integrate it. Simply adding TeamSingleChange inside TeamForm would work

Comment on lines 386 to 393
object SingleChange:
case class Change[Model, A](field: String, mapping: Mapping[A], update: A => Model => Model):
def form: PlayForm[A] = PlayForm(single(field -> mapping))

def changing[Model, FieldsType, A](field: FieldsType => (String, Mapping[A]))(
f: A => Model => Model
)(using fieldsType: FieldsType): Change[Model, A] =
Change(field(fieldsType)._1, field(fieldsType)._2, f)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole partial update thing feels more convoluted and restrictive than it needs to be I think.

I've no concrete plan though, only thinking something using filled form etc would allow multiple changes at the same time, but no concrete plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API for editing a team page
1 participant