def new_project(request): if request.method == 'POST': form = ZmenProjekt(request.POST) if form.is_valid(): data = form.cleaned_data autori = [] try: autori.append(User.objects.get(username=data['autor1'])) autori.append(User.objects.get(username=data['autor2'])) autori.append(User.objects.get(username=data['autor3'])) except User.DoesNotExist: pass projekt = Project() projekt.save() projekt.title = data['nazov'] projekt.consultant = User.objects.get(username=data['konzultant']) projekt.description = data['popis'] projekt.authors = autori projekt.save() return HttpResponseRedirect('/database/projekty/' + str(projekt.id)) else: form = ZmenProjekt(initial={'autor1': request.user.username}) return render(request, 'database/zmenit_projekt.html', { 'form': form, 'nazov': 'Vytvoriť projekt', 'tlacidlo': 'Vytvor!' })
def create_project(): """ Description: Create a new project from the UI, indicating 'name', 'manager_id', 'country', 'city', 'address', 'category' and 'description'. All the data needs to be in filled in order to create a new question. """ # Get the data from the UI user_data = request.get_json() user_name = user_data.get("name") user_manager_id = user_data.get("manager_id") user_country = user_data.get("country") user_city = user_data.get("city") user_address = user_data.get("address") user_category = user_data.get("category") user_description = user_data.get("description") # All the information needs to be provided if ( user_name and user_manager_id and user_country and user_city and user_address and user_category and user_description ): try: new_project = Project( name=user_name, manager_id=user_manager_id, country=user_country, city=user_city, address=user_address, category=user_category, description=user_description, ) new_project.insert() # Return success in JSON format return ( jsonify( { "success": True, "message": "Project successfully added to the database!", "project": new_project.format(), } ), 200, ) except BaseException: abort(400) # Some info is missing else: abort(400)
def projectTestData(): newProject1 = Project(customer_id = 1, status_name = 'Not Reached', address = 'Bear St', end_date = None , note = 'A fun fencing project', project_name = "Kat's house fence", company_name = 'Fence', layout_selected = None, appearance_selected = None, project_id = 1 ) newProject2 = Project(customer_id = 1, status_name = 'Not Reached', address = 'Grand Ave', end_date = None, note = 'Dog lives here', project_name = "Kat's second house fence", company_name = 'Fence',layout_selected = None, appearance_selected = None, project_id = 2 ) #newProject3 = Project(customer_id = 3, status_name = 'Complete', address = 'Park St', end_date = None, note = 'Concrete fence', project_name = "Jason's fence for company building", company_name = 'Fence', project_id = 3, layout_selected = 3, appearance_selected = 1) dbSession.add(newProject1) dbSession.add(newProject2) #dbSession.add(newProject3) dbSession.commit()
def create_project(payload): try: member = Member.query.filter( Member.auth0_id == payload.get('sub', '')).one_or_none() except: abort(403) if not member: abort(403) data = request.get_json() project_data = data['project'] if not project_data: abort(400) project = Project(member.id) project.set_data(project_data) project.insert() pm = ProjectMember(project.id, member.id) pm.insert() for m in project_data.get('members', []): pm = ProjectMember(project.id, m.get('id')) pm.insert() return jsonify({'success': True, 'project': project.long()})
def createProject(customerId, statusName, address, companyName, project_name): """Access mysql and add in project""" newProject = Project(customer_id=customerId, address=address, status_name=statusName, end_date=None, note='', project_name=project_name, company_name=companyName, finalize=False, layout_selected=None, appearance_selected=None) dbSession.add(newProject) dbSession.commit() newAppearance = Appearances.createAppearance(newProject.project_id) newProject.appearance_selected = newAppearance.appearance_id newLayout = Layouts.createLayout(newProject.project_id) newProject.layout_selected = newLayout.layout_id dbSession.commit() return newProject.project_id
def post(self): try: user_id = get_jwt_identity() body = request.get_json() user = User.objects.get(id=user_id) project = Project(**body, created_by=user).save() user.update(push__projects=project) user.save() return {'id': str(project.id)}, 200 except (FieldDoesNotExist, ValidationError): raise SchemaValidationError except NotUniqueError: raise ProjectAlreadyExistsError except Exception: raise InternalServerError
def update_api_counts(): """Periodically updates the api counts to the db. Should be called from a unique thread.""" while True: global api_count_cache if api_count_cache: try: with db_session: for project_id in api_count_cache: proj = Project.get(name=project_id) if not proj: continue count = api_count_cache[project_id] if not count or not isinstance(count, int): count = 0 proj.used_api_tokens += count if proj.used_api_tokens >= proj.api_token_count: proj.active = False logging.info('updating {} with {}'.format( proj.name, count)) api_count_cache = dict() # clear cache on success except Exception as e: logging.error(e) time.sleep(5)
def get(self): projects = Project.objects() dump_data = projects_schema.dumps(projects) return Response(dump_data, mimetype="application/json", status=200)
def create_project(): logging.info('Creating new pending project') # fetch eth tier1_expected_amount = get_eth_amount(min_payment_amount_tier1) tier2_expected_amount = get_eth_amount(min_payment_amount_tier2) tier1_expected_amount_ablock = get_ablock_amount(min_payment_amount_tier1 * discount_ablock) tier2_expected_amount_ablock = get_ablock_amount(min_payment_amount_tier2 * discount_ablock) tier1_expected_amount_aablock = get_aablock_amount( min_payment_amount_tier1 * discount_aablock) tier2_expected_amount_aablock = get_aablock_amount( min_payment_amount_tier2 * discount_aablock) amounts = { "eth_tier1": tier1_expected_amount, "eth_tier2": tier2_expected_amount, "ablock_tier1": tier1_expected_amount_ablock, "ablock_tier2": tier2_expected_amount_ablock, "aablock_tier1": tier1_expected_amount_aablock, "aablock_tier2": tier2_expected_amount_aablock } if list(amounts.values()).count(None) >= 5: context = { 'error': 'Internal Server Error: Failed to get at least 2 amounts, please try again', 'amounts': amounts } return Response(response=json.dumps(context)) eth_token, eth_address, eth_privkey = web3_helper.get_eth_address() avax_token, avax_address, avax_privkey = web3_helper.get_avax_address() project_name = str(uuid.uuid4()) start_time = datetime.datetime.now() payment_expires = start_time + datetime.timedelta(hours=3, minutes=30) api_key = secrets.token_urlsafe(32) logging.info( f'Creating project {project_name} with payment amounts: {amounts}') if tier1_expected_amount is None and tier2_expected_amount is None and tier1_expected_amount_ablock is None and tier2_expected_amount_ablock is None: eth_address = None try: if eth_address is None and avax_address is None: context = { 'error': 'Internal Server Error: Failed to get at least 1 payment address, please try again' } return Response(response=json.dumps(context)) with db_session: project = Project(name=project_name, api_key=api_key, api_token_count=6000000, used_api_tokens=0, active=False) payment = Payment( pending=True, eth_token=eth_token if eth_token != None else '', eth_address=eth_address if eth_address != None else '', eth_privkey=eth_privkey if eth_privkey != None else '', avax_token=avax_token if avax_token != None else '', avax_address=avax_address if avax_address != None else '', avax_privkey=avax_privkey if avax_privkey != None else '', start_time=start_time, project=project, tier1_expected_amount=tier1_expected_amount if tier1_expected_amount != None else -1, tier2_expected_amount=tier2_expected_amount if tier2_expected_amount != None else -1, tier1_expected_amount_ablock=tier1_expected_amount_ablock if tier1_expected_amount_ablock != None else -1, tier2_expected_amount_ablock=tier2_expected_amount_ablock if tier2_expected_amount_ablock != None else -1, tier1_expected_amount_aablock=tier1_expected_amount_aablock if tier1_expected_amount_aablock != None else -1, tier2_expected_amount_aablock=tier2_expected_amount_aablock if tier2_expected_amount_aablock != None else -1, amount_aablock=0, amount_ablock=0) commit() except Exception as e: logging.critical('Exception while creating Project or Payment', exc_info=True) context = {'error': 'Exception while creating Project or Payment'} return Response(response=json.dumps(context)) context = { 'result': { 'project_id': project_name, 'api_key': api_key, 'payment_eth_address': eth_address, 'payment_avax_address': avax_address, 'payment_amount_tier1_eth': tier1_expected_amount, 'payment_amount_tier2_eth': tier2_expected_amount, 'payment_amount_tier1_ablock': tier1_expected_amount_ablock, 'payment_amount_tier2_ablock': tier2_expected_amount_ablock, 'payment_amount_tier1_aablock': tier1_expected_amount_aablock, 'payment_amount_tier2_aablock': tier2_expected_amount_aablock, 'expiry_time': payment_expires.strftime("%Y-%m-%d %H:%M:%S EST") } } logging.info('Successfully created new pending project') return Response(response=json.dumps(context))