Esempio n. 1
0
File: views.py Progetto: Vedolin/api
def create_nonprofit(request, format=None):
    obj = json.loads(request.DATA["nonprofit"])
    email = obj["user"]["email"]

    obja = obj["address"]
    address = Address()
    address.zipcode = obja["zipcode"][0:9]
    address.addressline = obja["addressline"]
    address.addressline2 = obja.get("addressline2")
    address.addressnumber = obja["addressnumber"][0:9]
    address.neighborhood = obja["neighborhood"]
    address.city = City.objects.get(id=obja["city"]["id"])
    address.save()

    try:
        user = User.objects.get(email=email)
    except User.DoesNotExist:
        password = obj["user"]["password"]
        user = User.objects.create_user(email, password, slug=obj["user"]["slug"])
        user.name = obj["user"]["name"]
        user.hidden_address = obj["hidden_address"]
        user.phone = obj["phone"]
        user.address = address
        user.save()

    if Nonprofit.objects.filter(user=user):
        return Response({"detail": "Nonprofit already exists."}, status.HTTP_404_NOT_FOUND)

    FACEBOOK_KEY = "facebook_page"
    GOOGLE_KEY = "google_page"
    TWITTER_KEY = "twitter_handle"

    nonprofit = Nonprofit(user=user)
    nonprofit.name = obj["name"]
    nonprofit.details = obj["details"]
    nonprofit.description = obj["description"]
    nonprofit.save()

    causes = obj["causes"]
    for c in causes:
        nonprofit.causes.add(Cause.objects.get(name=c["name"]))

    if FACEBOOK_KEY in obj:
        nonprofit.facebook_page = obj[FACEBOOK_KEY]
    if GOOGLE_KEY in obj:
        nonprofit.google_page = obj[GOOGLE_KEY]
    if TWITTER_KEY in obj:
        nonprofit.twitter_handle = obj[TWITTER_KEY]

    nonprofit.image = request.FILES.get("image")
    nonprofit.image_small = request.FILES.get("image")
    nonprofit.image_medium = request.FILES.get("image")
    nonprofit.image_large = request.FILES.get("image")
    nonprofit.cover = request.FILES.get("cover")
    nonprofit.save()

    # Sending welcome email on nonprofit signup
    plaintext = get_template("email/nonprofitSignup.txt")
    htmly = get_template("email/nonprofitSignup.html")
    d = Context()
    subject, from_email, to = "Cadastro no Atados enviado com sucesso!", "*****@*****.**", nonprofit.user.email
    text_content = plaintext.render(d)
    html_content = htmly.render(d)
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

    return Response({"detail": "Nonprofit succesfully created."}, status.HTTP_200_OK)
Esempio n. 2
0
def create_nonprofit(request, format=None):
  obj = json.loads(request.DATA['nonprofit'])
  email = obj['user']['email']

  obja = obj['address']
  address = Address()
  address.zipcode = obja['zipcode'][0:9]
  address.addressline = obja['addressline']
  address.addressline2 = obja.get('addressline2')
  address.addressnumber = obja['addressnumber'][0:9]
  address.neighborhood = obja['neighborhood']
  address.city = City.objects.get(id=obja['city']['id'])
  address.save()

  try:
   user = User.objects.get(email=email)
  except User.DoesNotExist:
   password = obj['user']['password']
   user = User.objects.create_user(email, password, slug=obj['user']['slug'])
   user.name = obj['user']['name']
   user.hidden_address = obj['hidden_address']
   user.phone = obj['phone']
   user.address = address
   user.save()

  if Nonprofit.objects.filter(user=user):
   return Response({'detail': 'Nonprofit already exists.'}, status.HTTP_404_NOT_FOUND)


  FACEBOOK_KEY = 'facebook_page'
  GOOGLE_KEY = 'google_page'
  TWITTER_KEY = 'twitter_handle'

  nonprofit = Nonprofit(user=user)
  nonprofit.name = obj['name']
  nonprofit.details = obj['details']
  nonprofit.description = obj['description']
  nonprofit.save()

  causes = obj['causes']
  for c in causes:
    nonprofit.causes.add(Cause.objects.get(name=c['name']))

  if FACEBOOK_KEY in obj:
   nonprofit.facebook_page = obj[FACEBOOK_KEY]
  if GOOGLE_KEY in obj:
   nonprofit.google_page = obj[GOOGLE_KEY]
  if TWITTER_KEY in obj:
   nonprofit.twitter_handle = obj[TWITTER_KEY]

  nonprofit.image = request.FILES.get('image')
  nonprofit.cover = request.FILES.get('cover')
  nonprofit.save()

  # Sending welcome email on nonprofit signup
  plaintext = get_template('email/nonprofitSignup.txt')
  htmly     = get_template('email/nonprofitSignup.html')
  d = Context()
  subject, from_email, to = 'Cadastro no Atados enviado com sucesso!', '*****@*****.**', nonprofit.user.email
  text_content = plaintext.render(d)
  html_content = htmly.render(d)
  msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  msg.attach_alternative(html_content, "text/html")
  msg.send()

  return Response({'detail': 'Nonprofit succesfully created.'}, status.HTTP_200_OK)