def add_user_to_draw(request): """Add an user to a draw and sends a mail to inform him""" # DEPRECATE draw_id = request.GET.get('draw_id') users_to_add = request.GET.get('emails', "") if draw_id is None: return HttpResponseBadRequest() LOG.info("Adding {0} to draw {1}".format(users_to_add, draw_id)) bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # Raises 500 new_users = users_to_add.replace(',', ' ').split() try: for email in new_users: validate_email(email) # Raises a ValidationError except ValidationError: LOG.info("One or more emails are not correct") return HttpResponseBadRequest() bom_draw.users += new_users MONGO.save_draw(bom_draw) invite_user(new_users, draw_id, request.user.email) LOG.info("{0} users added to draw {1}".format(len(new_users), draw_id)) return HttpResponse()
def toss_draw(request): """generates a result and returns it""" # DEPRECATE draw_id = request.GET.get("draw_id") if draw_id is None: return HttpResponseBadRequest() bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # raises 500 result = bom_draw.toss() MONGO.save_draw(bom_draw) ga_track_draw(bom_draw, "toss") return JsonResponse({ "result": result })
def schedule_toss_draw(request): """generates a result and returns it""" # DEPRECATE draw_id = request.GET.get("draw_id") schedule = request.GET.get("schedule") if draw_id is None or schedule is None: return HttpResponseBadRequest() schedule = dateutil.parser.parse(schedule) bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # raises 500 result = bom_draw.timed_toss(schedule) MONGO.save_draw(bom_draw) return JsonResponse({ "result": result })
def update_share_settings(request): """Updates the shared settings. input POST {draw_id, enable_chat} """ # DEPRECATE draw_id = request.GET.get('draw_id') enable_chat = request.GET.get('enable_chat') == "true" if draw_id is None: LOG.warning("Empty draw_id") return HttpResponseBadRequest() bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # raises 500 bom_draw.enable_chat = enable_chat MONGO.save_draw(bom_draw) LOG.info("Draw {0} updated".format(bom_draw.pk)) return HttpResponse()
def remove_user_from_draw(request): """Remove an user from a draw""" # DEPRECATE draw_id = request.GET.get('draw_id') users = request.GET.get('emails', "") if draw_id is None: return HttpResponseBadRequest() LOG.info("Removing {0} from draw {1}".format(users, draw_id)) bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # Raises 500 remove_users = users.replace(',', ' ').split() bom_draw.users = [email for email in bom_draw.users if email not in remove_users] MONGO.save_draw(bom_draw) return HttpResponse()
def update_draw(request, draw_id): """Serves the update of a draw @draw_id: pk of the draw to update Given the draw details through the POST data, updates the draw. If success, redirects to display the view, otherwise, returns the form with the errors. It always create a new version of the draw. Use ws to update parts of the draw without creating a new version """ prev_bom_draw = MONGO.retrieve_draw(draw_id) draw_type = draw_factory.get_draw_name(prev_bom_draw.draw_type) user_can_write_draw(request.user, prev_bom_draw) LOG.debug("Received post data: {0}".format(request.POST)) draw_form = draw_factory.create_form(draw_type, request.POST) if not draw_form.is_valid(): LOG.info("Form not valid: {0}".format(draw_form.errors)) return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": prev_bom_draw}) else: bom_draw = prev_bom_draw raw_draw = draw_form.cleaned_data LOG.debug("Form cleaned data: {0}".format(raw_draw)) # update the draw with the data coming from the POST for key, value in raw_draw.items(): if key not in ("_id", "pk") and value != "": setattr(bom_draw, key, value) if not bom_draw.is_feasible(): LOG.info("Draw {0} is not feasible".format(bom_draw)) draw_form.add_error(None, _("Draw not feasible")) draw_form = draw_factory.create_form(draw_type, bom_draw.__dict__.copy()) return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": bom_draw}) else: bom_draw.add_audit("DRAW_PARAMETERS") # generate a result if a private draw if not bom_draw.is_shared: bom_draw.toss() MONGO.save_draw(bom_draw) LOG.info("Updated draw: {0}".format(bom_draw)) return redirect('retrieve_draw', draw_id=bom_draw.pk)
def remove_favorite(request): """removes a draw from the list of favourites""" # DEPRECATE draw_id = request.GET.get('draw_id') if draw_id is None: return HttpResponseBadRequest() bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # raises 500 user = MONGO.retrieve_user(request.user.pk) if draw_id not in user.favourites: LOG.info("Draw {0} is not favorite for user {1}".format( draw_id, request.user.pk)) return HttpResponse() user.favourites.remove(draw_id) MONGO.save_user(user) LOG.info("Draw {0} removed as favorite for user {1}".format( draw_id, request.user.pk)) return HttpResponse()
def add_favorite(request): """Add a draw to the list of favourites of an user""" # DEPRECATE draw_id = request.GET.get('draw_id') if draw_id is None: return HttpResponseBadRequest() bom_draw = MONGO.retrieve_draw(draw_id) user_can_write_draw(request.user, bom_draw) # raises 500 user = MONGO.retrieve_user(request.user.pk) if draw_id in user.favourites: LOG.info("Draw {0} is favorite for user {1}".format( draw_id, request.user.pk)) return HttpResponse() user.favourites.append(draw_id) MONGO.save_user(user) LOG.info("Draw {0} added as favorite for user {1}".format( draw_id, request.user.pk)) return HttpResponse()