Skip to content

Add SQLite support for saveing wlans adapters.#13

Open
muchosun wants to merge 5 commits intoDistributedSky:masterfrom
muchosun:drone-employee-connect
Open

Add SQLite support for saveing wlans adapters.#13
muchosun wants to merge 5 commits intoDistributedSky:masterfrom
muchosun:drone-employee-connect

Conversation

@muchosun
Copy link
Copy Markdown
Contributor

Add SQLite support for saveing wlans adapters.

Copy link
Copy Markdown
Member

@akru akru left a comment

Choose a reason for hiding this comment

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

Внести исправления в логику работы с БД.

Comment thread db.py Outdated
def check_wlan(name,wlan,ssid,password):
session = Session()
new_wlan=Wlans(name,wlan,ssid,password)
if new_wlan.wlan in repr(session.query(Wlans.wlan).order_by(Wlans.id).all()):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Почему бы не использовать фильтр и проверить len?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

def check_wlan(name,wlan,ssid,password):
    session = Session()
    new_wlan=Wlans(name,wlan,ssid,password)
    if len(new_wlan.wlan)>=8 and session.query(Wlans.wlan).filter(Wlans.wlan == new_wlan.wlan).all()):
       session.flush()
       update_wlan(name,wlan,ssid,password)
    elif len(new_wlan.wlan)>=8:
       session.flush()
       add_wlan(name, wlan, ssid, password)

Comment thread db.py
new_wlan=Wlans(name,wlan,ssid,password)
if new_wlan.wlan in repr(session.query(Wlans.wlan).order_by(Wlans.id).all()):
session.flush()
update_wlan(name,wlan,ssid,password)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Не за чем плодить дополнительный вызов, это породит еще одну сессию, лучше обновить на месте.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

А вот тут я подвис, не могу понянть как лучше это поправить

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if len(new_wlan.wlan)>=8 and session.query(Wlans.wlan).filter(Wlans.wlan == new_wlan.wlan).all()

vs

if session.query(Wlans).filter(Wlans.wlan == wlan).all():

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

А ты в этом плане)

Comment thread db.py Outdated
__tablename__ = 'wlanstb'
id = Column(Integer, primary_key=True)
name = Column(String)
wlan = Column(String)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Добавить модификатор, что поле уникальное.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

имеешь ввиду __id?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wlan

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Например Column('name', String(30), nullable=False, unique=True)

Comment thread db.py Outdated
def add_wlan(name, wlan, ssid, password):
session = Session()
new_wlan=Wlans(name,wlan,ssid,password)
session.add(new_wlan)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Здесь может быть exception, если добавляется повторно.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Если добавлять уникальность поля wlan? то данная проблема по идеи уходит.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Нет, она как раз возникает

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

except ValueError:
...         print "Oops!  That was no valid number.  Try again..."

Типа такого???

Copy link
Copy Markdown
Contributor Author

@muchosun muchosun Sep 13, 2017

Choose a reason for hiding this comment

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

def add_wlan(name, wlan, ssid, password):
    session = Session()
    new_wlan=Wlans(name,wlan,ssid,password)
    try:
       session.add(new_wlan)
       session.commit()
       session.flush()
    except:
       print("Oops!  That was no valid wlan.  Try again...")

Comment thread db.py
session.commit()
session.flush()
except:
print("Oops! That was no valid wlan. Try again...")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Тут, по-хорошему, можно поймать исключение, связанное с дубликатом уникального поля и делать update.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Интересный стиль. Вместо того что использовать if будем провоцировать ошибки)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@akru
Copy link
Copy Markdown
Member

akru commented Oct 19, 2017

Hey, @muchosun, do you can fix all commented places?

@muchosun
Copy link
Copy Markdown
Contributor Author

Yes I can. Today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants