tum 5 anni fa
parent
commit
c0d54dd937
5 ha cambiato i file con 131 aggiunte e 4 eliminazioni
  1. BIN
      __pycache__/app.cpython-36.pyc
  2. BIN
      __pycache__/fpl_lib.cpython-36.pyc
  3. 34 3
      app.py
  4. 46 1
      fpl_lib.py
  5. 51 0
      templates/lgs.html

BIN
__pycache__/app.cpython-36.pyc


BIN
__pycache__/fpl_lib.cpython-36.pyc


+ 34 - 3
app.py

@@ -1,5 +1,5 @@
1 1
 from flask import Flask, render_template, request, jsonify, redirect, url_for
2
-from fpl_lib import SFL, Team
2
+from fpl_lib import SFL, Team, Leauge
3 3
 from flask_pymongo import PyMongo
4 4
 from bson.objectid import ObjectId
5 5
 
@@ -18,6 +18,37 @@ def search_player(name):
18 18
     results = sfp.search_player(name)
19 19
     return jsonify(output=results)
20 20
 
21
+@app.route('/lgs', methods=["GET", "POST"])
22
+def lgs():
23
+    lgs = mongo.db.lgs.find()
24
+
25
+
26
+    teams = mongo.db.teams.find()
27
+    if request.method == "POST":
28
+        if 'createLg' in request.form:
29
+            name = request.form.get('title', None)
30
+
31
+            team = Leauge(name, mongo)
32
+            team.create()
33
+            return redirect(url_for(".lgs"))
34
+
35
+
36
+
37
+
38
+    lg = None
39
+    if 'lg_id' in request.args:
40
+        #lg = mongo.db.lgs.find_one({'_id': ObjectId(request.args.get('lg_id'))})
41
+        lg = Leauge.from_id(request.args.get('lg_id'), mongo)
42
+
43
+        if 'selectTeam' in request.form:
44
+            team_values = request.form.getlist('teams')
45
+            print(f"teams = {team_values}")
46
+            for t in team_values:
47
+                lg.add_team(t)
48
+            lg.save()
49
+
50
+    return render_template('lgs.html', lgs = lgs, lg=lg, teams = teams)
51
+
21 52
 @app.route('/team/<team_id>', methods=["GET", "POST"])
22 53
 def team(team_id):
23 54
     team = mongo.db.teams.find_one({"_id": ObjectId(team_id)})
@@ -79,8 +110,8 @@ def index():
79 110
             selectPlayers = [int(x) for x in selectPlayers]
80 111
             mongo.db.teams.update_one({"_id": ObjectId(selectTeam)}, {"$set": {"players": selectPlayers}})
81 112
 
82
-        print("select players")
83
-        print(selectPlayers)
113
+            print("select players")
114
+            print(selectPlayers)
84 115
 
85 116
         if teamCreate:
86 117
             print("Team Create")

+ 46 - 1
fpl_lib.py

@@ -2,6 +2,7 @@ from fpl import FPL
2 2
 from fpl.utils import get_current_gameweek
3 3
 import aiohttp
4 4
 import asyncio
5
+from bson.objectid import ObjectId
5 6
 
6 7
 class Team:
7 8
     def __init__(self, name):
@@ -18,9 +19,53 @@ class Team:
18 19
         total = sum(p.goals_scored for p in self.players)
19 20
         print(f"Total Score => {total}")
20 21
 
22
+
23
+class Leauge:
24
+    def __init__(self, title, mongo=None):
25
+        self.title = title
26
+        self.mongo = mongo
27
+
28
+
29
+    @classmethod
30
+    def from_id(cls, oid, mongo=None):
31
+        lg = mongo.db.lgs.find_one({"_id": ObjectId(oid)})
32
+        print(f"lg = {lg}")
33
+        c = cls(None, mongo)
34
+        c.init_db(lg)
35
+        return c
36
+
37
+    def init_db(self,  col):
38
+        self.title = col.get('title', None)
39
+        self._teams = col.get('teams', [])
40
+        self._id = col.get('_id',None)
41
+
42
+    @property
43
+    def teams(self):
44
+        return self.mongo.db.teams.find({"_id": {"$in": self._teams}})
45
+
46
+    def save(self):
47
+
48
+        self.mongo.db.lgs.update_one({"_id": self._id}, {
49
+            '$set': {'title': self.title, 'teams': self._teams}
50
+        })
51
+
52
+    def create(self):
53
+
54
+        return  self.mongo.db.lgs.insert_one({
55
+            'title': self.title, 'teams': []
56
+        })
57
+
58
+    def add_team(self, team_id):
59
+        self._teams.append(ObjectId(team_id))
60
+
61
+    def remove_team(self, team_id):
62
+        self._teams.remove(ObjectId(team_id))
63
+
64
+
21 65
 class SFL:
22
-    def __init__(self, name):
66
+    def __init__(self, name,mongo=None):
23 67
         self.name = name
68
+        self.mongo = mongo
24 69
 
25 70
     async def _setup(self):
26 71
         async with aiohttp.ClientSession() as session:

+ 51 - 0
templates/lgs.html

@@ -0,0 +1,51 @@
1
+{% extends "base.html" %}
2
+{% block sidemenu %}
3
+<form  method="post">
4
+    Title:
5
+    <input type="text" name="title"><br>
6
+    <input type="submit" name="createLg" id="" value="Create Lg">
7
+</form>
8
+<table class='table table-bordered table-striped'>
9
+    <thead>
10
+        <tr>
11
+            <th>Id</th>
12
+            <th>Name</th>
13
+            <th>Top</th>
14
+        </tr>
15
+    </thead>
16
+    <tbody>
17
+{% for l in lgs %}
18
+        <tr>
19
+            <td><a href="{{ url_for("lgs")  }}?lg_id={{ l._id }}">{{ l._id }}</a></td>
20
+            <td>{{ l.title }}</td>
21
+            <td>{{ l.top_score }}</td>
22
+        </tr>
23
+{% endfor %}
24
+    </tbody>
25
+</table>
26
+{% endblock %}
27
+{% block content %}
28
+<h1>Leauges</h1>
29
+{{ lg.title }}
30
+
31
+<form  method="post">
32
+    Select Name 
33
+    <select name="teams" multiple>
34
+        
35
+        {% for t in teams %}
36
+        <option value="{{ t._id }}">{{ t['title'] }}</option>
37
+        {% endfor %}
38
+    </select>
39
+    <input type="submit" name="selectTeam" value="Select  Team">
40
+</form>
41
+{% if lg %}
42
+
43
+    <h2>Team in this Leauge</h2>
44
+     <ol>
45
+         
46
+    {% for team in lg.teams %}
47
+    <li><a href="{{ url_for(".team", team_id=team._id) }}">{{ team['title'] }}</a></li>
48
+    {% endfor %}
49
+     </ol>
50
+{% endif %}
51
+{% endblock %}