def create_food_feed(data:dict,user:User)->dict: """ Method to create a new feed in food by user user should be a volunteer or own of feed run :para data: content info to put in feed :raise: ValueError :return: food_models.FeedFood """ if data.get("id_food_run") is not None: food_run = get_food_run(data.get("id_food_run")) else: raise ValueError(str(_("id_food_run is required"))) if data.get("image") is None: raise ValueError(str(_("image is required"))) if user != food_run.user: raise ValueError(str(_("You do not have permission to create a feed food"))) try: with transaction.atomic(): feed = food_models.FeedFood.objects.create( user = user, food = food_run, image = accounts_services.updateImage(data.get("image")) ) except Exception as e: print(e) raise ValueError(str(_("There was a error saving feed food run"))) if data.get("description") is not None: food_validations.validate_length("description",data.get("description"),0,100) feed.description = data.get("description") feed.save() return feed
def create_feed(data: dict, user: accounts_models.User) -> feed_models.Feed: """ Method to create a new post in feed :param data: contains data about post :type data: dict :param user: user that is creating post in feed :type user: accounts_models.User :raise: ValueError :return: feed_models.Feed """ if data.get("image") is None: raise ValueError(str(_("Image is required"))) if data.get("description") is not None: accounts_validations.validate_length("Description", data.get("description"), 0, 65000) else: raise ValueError(str(_("Description is required"))) with transaction.atomic(): try: feed = feed_models.Feed.objects.create( userRegisterer=user, image=accounts_services.updateImage(data.get("image")), description=data.get("description")) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the feed"))) return feed
def update_feed(data: dict, user: accounts_models.User) -> feed_models.Feed: """ Method to updte feed by user :param data: data to update in feed :type data: dict :param user: user who created the feed and now updates it :type user: accounts_models.User :raise: ValueError :return: feed_models.Feed """ if data.get("id") is not None: feed_validations.validate_id(data.get("id")) else: raise ValueError(str(_("id feed is required"))) try: feed = feed_models.Feed.objects.get(id=data.get("id"), userRegisterer=user) except feed_models.Feed.DoesNotExist as e: raise ValueError(str( _("You dont have permission to update this feed"))) if data.get("image") is not None: feed.image = accounts_services.updateImage(data.get("image")) if data.get("description") is not None: accounts_validations.validate_length("Description", data.get("description"), 0, 65000) feed.description = data.get("description") feed.save() return feed
def new_food_run(data:dict, user:User): """ Method to create a new food run by user :param data: info to put in food :raise: ValueError :return: FoodRun """ total: float = data.get("total", None) total_volunteers : int = data.get("total_volunteers",None) if data.get("name") is not None: food_validations.validate_length('Name',data.get("name"),5,100) else: raise ValueError(str(_("Name is required"))) if data.get("description") is not None: food_validations.validate_length('Decription',data.get("description"),5,300) else: raise ValueError(str(_("description is required"))) if data.get("image") is None: raise ValueError(str(_("image is required"))) if total is not None: if total <=0 or total > 9999999: raise ValueError(str(_("Min value in total is 0 and max is 9999999"))) else: raise ValueError(str(_("total is required"))) if total_volunteers is not None: if total_volunteers <=0 or total > 10000: raise ValueError(str(_("Min value in total is 0 and max is 10000"))) else: raise ValueError(str(_("total_volunteers is required"))) if data.get("execution_date") is not None: execution_date = food_validations.validate_excuted_date(data.get("execution_date")) else: raise ValueError(str(_("execution_date is required"))) with transaction.atomic(): try: food_run = food_models.FoodRun.objects.create( user = user, name = data.get("name"), description = data.get("description"), image = accounts_services.updateImage(data.get("image")), total = total, rest = total, total_volunteers = total_volunteers, rest_volunteers = total_volunteers, execution_date = execution_date, status = str('open') ) except Exception as e: print(e) raise ValueError(str(_("there was an error saving the food run"))) if data.get("invitation_message") is not None: food_validations.validate_length('Invitation Message',data.get("invitation_message"),10,100) food_run.invitation_message = data.get("invitation_message") food_run.save() """Calculations to perform the scheduled task""" create_at = datetime.now() execution_date = datetime.strptime(execution_date, "%Y-%m-%d %H:%M") # Time to add at timenow time_extra = execution_date - create_at # Time to rest at time_extra time_rest = (20*time_extra.total_seconds())/100 time_to_add = time_extra - timedelta(seconds=time_rest) time = time_to_add.total_seconds() # time = create_at + time_to_add print(time) food_task.notification_food.apply_async(([food_run.id]),countdown=int(time)) return food_run
def update_food_run(data:dict,user:User): """ Method to update a food run. Only the owner of the food run can update it :param data: content info to update in food run :param user: content user owner of the food run :raise: ValuerError :return: food_models.FoodRun """ total: float = data.get("total", None) total_volunteers : int = data.get("total_volunteers",None) if data.get("id_food_run") is not None: food_run = get_food_run(data.get("id_food_run")) if user != food_run.user: raise ValueError(str(_("This user have not permission to update this food run"))) else: raise ValueError(str(_("id_food_run is required"))) if data.get("name") is not None: food_validations.validate_length('Name',data.get("name"),5,100) food_run.name = data.get("name") if data.get("description") is not None: food_validations.validate_length('Decription',data.get("description"),5,300) food_run.description = data.get("description") if total is not None: if food_run.rest <=0: raise ValueError(str(_("Can not change total beacause the rest alredy is 0"))) if total != food_run.total: # print("Es diferente el valor del total") if total <=0 or total > 9999999: raise ValueError(str(_("Min value in total is 0 and max is 9999999"))) if total < food_run.rest: raise ValueError(str(_("rest value can not be mayor than total value"))) # print(total) food_run.total = total rest = float(food_run.rest) food_run.rest = total - rest if (food_run.rest_volunteers == 0) and (food_run.rest <=0): food_run.status = 'closed' # print("No es doferente") if total_volunteers is not None: if food_run.total_volunteers <=0: raise ValueError(str(_("Can not change total beacause the rest volunteer alredy is 0"))) if total_volunteers != food_run.total_volunteers: # print("Es diferente el valor del total voluntario") if total_volunteers <=0 or total > 10000: raise ValueError(str(_("Min value in total is 0 and max is 10000"))) if total_volunteers < food_run.rest_volunteers: raise ValueError(str(_("rest volunteers can no be mayor than total volunteer"))) # print(food_run.rest_volunteers,type(food_run.rest_volunteers)) food_run.total_volunteers = total_volunteers food_run.rest_volunteers = food_run.rest_volunteers - total_volunteers if (food_run.rest_volunteers == 0) and (food_run.rest <=0): food_run.status = 'closed' # print("Es diferente el valor del total voluntario") if (food_run.rest_volunteers > 0) or (food_run.rest >0): food_run.status = 'open' if data.get("image") is not None: food_run.image = accounts_services.updateImage(data.get("image")) try: with transaction.atomic(): food_run.save() except Exception as e: # print(e) raise ValueError(str(_("There was a error updating the food run"))) return food_run