def test_update_membership(self): u, m = self._create_user_with_membership() m.update(points=100, credits=10) self.assertEqual(Member.get_by_id(m.id).points, 100) self.assertEqual(m.points, Member.get_by_id(m.id).points) self.assertEqual(Member.get_by_id(m.id).credits, 10) self.assertEqual(m.credits, 10)
def update_plan(ptype,CourseId,Yardid,Coach_id,BeginDate,EndDate,WorkTime,OffTime,WeekSchedule_list_or_str,updater): session = database.get_session() updatedict = {'BeginDate':BeginDate,'EndDate':EndDate,'Modifier':updater,'LastUpdateDate':datetime.now()} if CourseId: c = Course() c.CourseId = CourseId updatedict.update({'Course':[c]}) if Yardid: y = Yard() y.Yardid = Yardid updatedict.update({'Yard':[y]}) if Coach_id: m = Member() m.UserId = Coach_id updatedict.update({'Coach':[m]}) if WorkTime: updatedict.update({'WorkTime':WorkTime}) if OffTime: updatedict.update({'OffTime':OffTime}) if WeekSchedule_list_or_str: if ptype=='a': s = '' for e in WeekSchedule_list_or_str: s+= '&'.join(e)+'^' updatedict.update({'WeekSchedule':s.strip('^')}) elif ptype=='b': updatedict.update({'WeekSchedule':WeekSchedule_list_or_str}) else: pass session.query(Course).filter(Course.CourseId == CourseId).update(updatedict) session.commit() session.close()
def update_course(CourseId,Name,Duration,Amount,Coach_id,HourFee,Comment,Description,updater): session = database.get_session() updatedict = {'Name':Name.strip(),'Duration':int(Duration),'Amount':int(Amount),'HourFee':int(HourFee), 'Comment':Comment.strip(),'Description':Description.strip(),'Modifier':updater, 'LastUpdateDate':datetime.now()} if Coach_id: m = Member() m.UserId = Coach_id updatedict.update({'Coach':[m]}) session.query(Course).filter(Course.CourseId == CourseId).update(updatedict) session.commit() session.close()
def _create_user_with_membership(self): """ Create a user and a member """ u = User.create(account_id=self.account_id, secret_key=self.secret_key) m = Member.create(account_id=u.account_id, email=self.email) return [u, m]
def register_user(): if request.method == 'POST': data = request.get_json() print(data, "GHUJKL:HGJHKJ") userEmail = data['email'] userPassword = data['password'] userName = data['name'] user = Member.query.filter_by(email=userEmail).first() if user: return jsonify({'message': 'Email already exist!'}) else: new_user = Member(email=userEmail, name=userName) new_user.generate_password(userPassword) db.session.add(new_user) db.session.commit() return jsonify({'message': 'You has sign up, please login!'})
def test_delete_purchase(self): """ Purchase deleted -> Member NOT deleted -> User NOT deleted """ p, m, u = self._create_purchase_with_member_with_user() p.delete() self.assertEqual(Purchase.query.count(), 0) assert not Purchase.get_by_id(p.id) and Member.get_by_id(m.id) and User.get_by_id(u.id) self.assertEqual(len(m.purchases), 0)
def create_course(CourseType,Name,Duration,Amount,HourFee,Comment,Description,creator,Coach_id=None): session = database.get_session() p = Course() p.CType = CourseType.strip() p.Name = Name.strip() p.Duration = int(Duration) p.Amount = int(Amount) if Coach_id: m = Member() m.UserId = Coach_id p.Coach.append(m) p.HourFee = int(HourFee) p.Comment = Comment.strip() p.Description = Description.strip() p.Creator = creator p.CreateDate = datetime.now() session.add(p) session.commit() session.close()
def test_points_to_credits(self): """ Convert points to credits """ u, m = self._create_user_with_membership() m.add_points(100) self.assertEqual(Member.points_needed_for(10), 100) m.convert_points_to_these_credits(10) self.assertEqual(m.points, 0) self.assertEqual(m.credits, 10) self.assertEqual(Member.get_by_id(m.id).points, 0) self.assertEqual(Member.get_by_id(m.id).credits, 10) m.add_points(100) m.convert_points_to_these_credits(9999) # should not be allowed self.assertEqual(m.points, 100) self.assertEqual(m.credits, 10) self.assertEqual(Member.get_by_id(m.id).points, 100) self.assertEqual(Member.get_by_id(m.id).credits, 10)
def test_delete_membership(self): """ Member deleted -> Purchases by that member deleted -> User NOT deleted """ p, m, u = self._create_purchase_with_member_with_user() m.delete() self.assertIsNone(Purchase.get_by_id(p.id)) self.assertIsNone(Member.get_by_id(m.id)) self.assertIsNotNone(User.get_by_id(u.id)) self.assertEqual(u.members, [])
def test_delete_user_before_membership(self): """ if user is deleted then user's membership should be deleted """ u, m = self._create_user_with_membership() self.assertEqual(u.members, [m]) self.assertIsNotNone(m) u.delete() self.assertIsNone(User.get_by_id(u.id)) self.assertIsNone(Member.get_by_id(m.id))
def test_delete_membership_before_user(self): """ if user's membership is deleted then user should still exist """ u, m = self._create_user_with_membership() self.assertIsNotNone(m) self.assertEqual(User.get_by_id(u.id).members, [m]) m.delete() self.assertIsNone(Member.get_by_id(m.id)) self.assertIsNotNone(User.get_by_id(u.id)) self.assertEqual(User.get_by_id(u.id).members, [])
def facebook_logged_in(blueprint, token): if not token: flash("Failed to log in.", category="error") return False resp = blueprint.session.get("/me") if not resp.ok: msg = "Failed to fetch user info." flash(msg, category="error") return False info = resp.json() user_id = info["id"] print('info', info) # Find this OAuth token in the database, or create it query = OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id) try: oauth = query.one() except NoResultFound: oauth = OAuth(provider=blueprint.name, provider_user_id=user_id, token=token) if oauth.user: login_user(oauth.user) flash("Successfully signed in.") else: # Create a new local user account for this user user = Member(name=info["name"]) # Associate the new local user account with the OAuth token oauth.user = user # Save and commit our database models db.session.add_all([user, oauth]) db.session.commit() # Log in the new local user account login_user(user) flash("Successfully signed in.") # Disable Flask-Dance's default behavior for saving the OAuth token token_query = Token.query.filter_by(user_id=current_user.id) try: token = token_query.one() except NoResultFound: token = Token(user_id=current_user.id, uuid=str(uuid.uuid4().hex)) db.session.add(token) db.session.commit() return redirect("http://localhost:3000/?api_key={}".format(token.uuid))
def _create_purchase_with_member_with_user(self): u = User.create(account_id=self.account_id, secret_key=self.secret_key) m = Member.create(email=self.email, account_id=u.account_id) p = Purchase.create(title='TEST', price=100, member_id=m.id) return [p, m, u]