Explorar el Código

backend for creating art (minting) is ready

control hace 3 años
padre
commit
834da54dca
Se han modificado 10 ficheros con 102 adiciones y 20 borrados
  1. 15 3
      app/dashboards.py
  2. BIN
      app/database-clean.db
  3. BIN
      app/database-mints.db
  4. BIN
      app/database.db
  5. 57 0
      app/dispatch.py
  6. 4 6
      app/hasher.py
  7. 4 3
      app/models.py
  8. 1 1
      app/templates/base.html
  9. 8 7
      app/templates/create_art.html
  10. 13 0
      app/tools.py

+ 15 - 3
app/dashboards.py

@@ -5,6 +5,8 @@ from .models import User
 from . import db
 
 from . import clean_file as cf
+from . import tools
+from . import dispatch
 
 dashboards = Blueprint('dashboards', __name__)
 
@@ -35,22 +37,32 @@ def profile():
     return render_template('profile.html', user=current_user)
 
 @dashboards.route('/create_art', methods=['GET', 'POST'])
-#login_required
+@login_required
 def create():
     # check POST req
     if request.method == "POST":
         new_art = request.files.get('art_img')
+        art_name = request.form.get('art_name')
+        art_desc = request.form.get('description')
+        min_price = request.form.get('minimum_price')
+        buyout_price = request.form.get('buyout_price')
+        close_date = request.form.get('closing_date')
 
-        if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
-            new_art.save(f'{cf.UPLOAD_FOLDER}/{cf.sanitize(new_art.filename)}')
+        if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
+            if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
+                designated_fn = cf.sanitize(new_art.filename)
+                new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
+                dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
 
     return render_template('create_art.html', user=current_user)
 	
 # Pop Ups
 @dashboards.route('/modal_home')
+@login_required
 def modal_home():
     return render_template('detail_art_for_home.html', user=current_user)
 
 @dashboards.route('/modal_profile')
+@login_required
 def modal_profile():
     return render_template('detail_art_for_profile.html', user=current_user)

BIN
app/database-clean.db


BIN
app/database-mints.db


BIN
app/database.db


+ 57 - 0
app/dispatch.py

@@ -0,0 +1,57 @@
+# Dispatches data to hasher and adds to database tables
+
+import sqlalchemy
+from datetime import datetime
+from flask_login import current_user
+
+from .models import Hashchain, Art, List, TX 
+from . import db
+
+from . import clean_file as cf
+from . import hasher as hsh
+
+def mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date):
+    # gen filehash
+    filehash = hsh.hashfile(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
+    # gen txhash (if bought... or sold, or minted)
+    ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
+    dt = sqlalchemy.func.now()
+    txstring = f'[{filehash}, {ddt}, {current_user.id}, {current_user.id}, 0.0]' # minting
+    txhash = hsh.gen_txhash(txstring)
+    # fetch prev block hash OR GEN HASH
+    try:
+        prev_bhash = db.session.query(Hashchain).order_by(Hashchain.id.desc()).first().blockhash
+        if prev_bhash is None:
+            prev_bhash = hsh.GENESIS_HASH
+    except:
+        prev_bhash = hsh.GENESIS_HASH
+    # gen block hash add to hashchain
+    new_bhash = hsh.append_tx(txhash, prev_bhash)
+    new_block = Hashchain(blockhash = new_bhash)
+    db.session.add(new_block)
+    # move the file into external secure storage + another local dir (NOT DONE)
+    # add to TX table (mint)
+    new_tx = TX(
+            filehash = filehash,
+            datetime = dt,
+            source_id = current_user.id,
+            destination_id = current_user.id,
+            price = 0.0,
+            txhash = txhash
+            )
+    db.session.add(new_tx)
+    # add to Art table
+    new_art = Art(name = art_name, description = art_desc, filehash = filehash)
+    db.session.add(new_art)
+    # add to List table
+    new_listing = List(
+            filehash = filehash,
+            seller = current_user.id,
+            min_price = min_price,
+            out_price = buyout_price,
+            timeout = close_date,
+            )
+    db.session.add(new_listing)
+
+    db.session.commit() 
+

+ 4 - 6
app/hasher.py

@@ -1,16 +1,14 @@
 import sys
 import hashlib
-
+ 
 # Constants
 GENESIS_STR = 'This is the genesis hash'
 
 # Specify buffer size
 BUF_SIZE = 65536 # 64kB chunks!
 
-# Init objects
-sha256 = hashlib.sha256()
-
 def hashfile(filename):
+    sha256 = hashlib.sha256()
     with open(filename, 'rb') as f:
         while True:
             data = f.read(BUF_SIZE)
@@ -18,12 +16,12 @@ def hashfile(filename):
                 break
             sha256.update(data)
 
-    return sha256.hexdigest
+    return sha256.hexdigest()
 
 def hashdata(data):
     sha256 = hashlib.sha256(data.encode())
 
-    return sha1.hexdigest
+    return sha256.hexdigest()
 
 # Constant!
 GENESIS_HASH = hashdata(GENESIS_STR)

+ 4 - 3
app/models.py

@@ -10,10 +10,10 @@ class User(db.Model, UserMixin): # User Database
    username = db.Column(db.String(150))
    profile_image = db.Column(db.String(150))
 
-class Transaction(db.Model):
+class TX(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     filehash = db.Column(db.String(64)) # sha256 hash
-    datetime = db.Column(db.DateTime(timezone=True), default=func.now())
+    datetime = db.Column(db.DateTime(timezone=True))
     source_id = db.Column(db.Integer)
     destination_id = db.Column(db.Integer)
     price = db.Column(db.Float)
@@ -35,7 +35,8 @@ class List(db.Model):
     seller = db.Column(db.Integer)
     min_price = db.Column(db.Float)
     out_price = db.Column(db.Float)
-    timeout = db.Column(db.DateTime(timezone=True))
+    timeout = db.Column(db.String(20))
+    list_date = db.Column(db.DateTime(timezone=True), default=func.now()) 
 
 class Bids(db.Model):
     id = db.Column(db.Integer, primary_key=True)

+ 1 - 1
app/templates/base.html

@@ -109,7 +109,7 @@ body {
         {% endfor %}
     {% endif %}
     {% endwith %}
-    <div class="container">
+    <div class="container" style="position:relative; top:200px;"> <!-- added the style here for backend testing! -->
         {% block content %}
         {% endblock %}
     </div>

+ 8 - 7
app/templates/create_art.html

@@ -28,13 +28,13 @@
                 <!--Input Starting Price-->
                 <div>
                     <input 
-                    type="starting_price" 
+                    type="minimum_price" 
                     class="form-control" 
-                    id="starting_price"
-                    name="starting_price"
-                    placeholder="Enter Starting Price "
+                    id="minimum_price"
+                    name="minimum_price"
+                    placeholder="Enter Minimum Price "
                     />
-                    <label for="starting_price"></label>
+                    <label for="minimum_price"></label>
                 </div> 
                 <!--Input Buyout Price-->
                 <div>
@@ -55,9 +55,10 @@
                 </select>
                 <br>
                 <!--Choose Time for Auction ends (I will check how I can limit the user 14days)-->
-                <label for="Auction_ends_time">Choose a time for your meeting:</label>
-                <input id="Auction_ends_time" type="datetime-local" name="Auction_ends_time">
+                <label for="closing_date">Auction end date:</label>
+                <input id="closing_date" type="date" name="closing_date">
                 <small>The maximum Auction time is 14 days</small>
+                <small>The exact time of closing is 0000 hrs of that date</small>
 
                 <br>
                 <p>***Backend also save 1.Creator name, 2.creater the item date and time for showing on detail art page</p>

+ 13 - 0
app/tools.py

@@ -0,0 +1,13 @@
+# Tools
+
+# takes list of variables and checks if empty
+def check_fields(var_list):
+    counter = 0
+    for i in var_list:
+        if not i:
+            return False
+        else:
+            counter += 1
+    if counter == len(var_list):
+        return True
+