def merchant(django_db_setup, django_db_blocker): ''' create dummy merchants ''' with django_db_blocker.unblock(): print('FIXTURE CREATED') for i in range(2): merchant = Merchant(name=f"name{i+1}", email=f"merc{i+1}@gmail.com", mobile="9999999999") merchant.save() merchant = Merchant.objects.all() # merchant = mixer.blend('app.Merchant') yield merchant # deleting the merchant after test suite run completed, # deleting merchant would also delete all other entries as it ondelete.CASCADE in other tables with django_db_blocker.unblock(): print('deleting') merchant.delete()
def merchant(action): """ Manage merchants endpoint. action = any(list-all, new, save, delete, note, delete-note) """ data = request.get_json() if action == 'list-all': merchants, count = Merchant.admin_list_all(data.get('offset', 0), data.get('limit', 25), data.get('search', '')) return jsonify({'merchants': merchants, 'count': count}) elif action == 'new': return jsonify({'merchant': Merchant.new().as_dict()}) elif action == 'save': Merchant.save(data.get('merchant')) return jsonify({'success': True}) elif action == 'delete': Merchant.delete(data.get('merchant')) return jsonify({'success': True}) elif action == 'note': if data.get('merchant_id') is not None: note = MerchantNote(note=data.get('note'), merchant_id=data['merchant_id']) db.session.add(note) db.session.commit() return jsonify({'note': note.as_dict()}) elif action == 'delete-note': if data.get('note') is not None and data['note'].get('id') is not None: note = MerchantNote.query.get(data['note']['id']) db.session.delete(note) db.session.commit() return jsonify({'success': True}) elif action == 'location-note': if data.get('location_id') is not None: note = LocationNote(note=data.get('note'), location_id=data['location_id']) db.session.add(note) db.session.commit() return jsonify({'note': note.as_dict()}) elif action == 'location-delete-note': if data.get('note') is not None and data['note'].get('id') is not None: note = LocationNote.query.get(data['note']['id']) db.session.delete(note) db.session.commit() return jsonify({'success': True}) elif action == 'offer-new': offer = Offer.new(data.get('merchant_id')) if offer is not None: return jsonify({'offer': offer.as_dict()}) elif action == 'offer-save': offer = Offer.save(data.get('offer')) if offer is not None: return jsonify({'offer': offer.as_dict()}) elif action == 'offer-delete': offer_id = data.get('offer', {}).get('id') if offer_id is not None: offer = Offer.query.get(offer_id) db.session.delete(offer) db.session.commit() return jsonify({'success': True}) elif action == 'location-new': location = Location.new(data.get('merchant_id')) if location is not None: return jsonify({'location': location.as_dict()}) elif action == 'location-save': location = Location.save(data.get('location')) if location is not None: return jsonify({'location': location.as_dict()}) elif action == 'location-delete': location_id = data.get('location', {}).get('id') if location_id is not None: location = Location.query.get(location_id) db.session.delete(location) db.session.commit() return jsonify({'success': True}) elif action == 'contact-new': contact = MerchantContact.new(data.get('merchant_id')) if contact is not None: return jsonify({'contact': contact.as_dict()}) elif action == 'contact-save': contact = MerchantContact.save(data.get('contact')) if contact is not None: return jsonify({'contact': contact.as_dict()}) elif action == 'contact-delete': contact_id = data.get('contact', {}).get('id') if contact_id is not None: contact = MerchantContact.query.get(contact_id) db.session.delete(contact) db.session.commit() return jsonify({'success': True}) return jsonify({}), 400