def create(self, request): permission_classes = [AllowAny] print(request.data['title']) print(request.data['content']) if not request.data['title'] or not request.data['content']: return Response({'message': 'title or content cannot be null'}, status.HTTP_400_BAD_REQUEST) newJournal = Journal(title=request.data['title'], content=request.data['content']) newJournal.save() serializer = JournalSerializer(newJournal) return Response(serializer.data, status.HTTP_201_CREATED) # @api_view(['GET']) # def getAllJournals(request): # print(request.user) # journals = Journal.objects.all() # serializer = JournalSerializer(journals) # if len(journals) == 0: # return Response([]) # else: # return Response(serializer.data) # @api_view(['POST']) # def addJournal(request): # print(request.title) # print(request.content) # return Response([])
def initialize_journal(request): f = open(os.path.join(settings.EFS_ROOT, 'log.txt'), 'w') #DELETE THIS sub = request.user.subscriber if sub.total_entries == 0: try: met = Metrics.objects.get(current=True) met.log_journal_entry() except: pass #never fail user request because of a metrics error try: creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists(os.path.join(settings.EFS_ROOT, str(sub.id) + 'token.pickle')): with open(os.path.join(settings.EFS_ROOT, str(sub.id) + 'token.pickle'), 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( os.path.join(settings.EFS_ROOT, 'credentials.json'), ['https://www.googleapis.com/auth/documents']) flow.redirect_uri = flow._OOB_REDIRECT_URI flow.fetch_token(code=request.POST["auth_code"]) creds = flow.credentials # Save the credentials for the next run with open(os.path.join(settings.EFS_ROOT, str(sub.id) + 'token.pickle'), 'wb') as token: pickle.dump(creds, token) service = build('docs', 'v1', credentials=creds) except: return render(request, 'initialize_journal_prompt.html', {"googleError": True}) names = ["SMSJournal"] for name in names: try: journal = sub.journal_set.get(name=process_journal_name(name)) write_to_gdoc(journal.google_docs_id, "Welcome to SMSJournal! You can make journal entries to this file by sending messages to the SMSJournal phone number, (970)-507-7992. To send an entry to a different journal, just add a tag like @ideas and we'll find or create the journal \"ideas\" in your Google Drive.", service) except: doc = service.documents().create(body={"title": name}).execute() write_to_gdoc(doc["documentId"], "Welcome to SMSJournal! You can make journal entries to this file by sending messages to the SMSJournal phone number, (970)-507-7992. To send an entry to a different journal, just add a tag like @ideas and we'll find or create the journal \"ideas\" in your Google Drive.", service) journal = Journal(subscriber=sub, name=process_journal_name(name), google_docs_id=doc["documentId"]) journal.save() sub.last_entry = timezone.now() sub.total_entries = sub.total_entries + 1 sub.save() client = boto3.client('pinpoint', 'us-east-1') pinpoint_id = settings.AWS_PINPOINT_PROJECT_ID client.send_messages(ApplicationId=pinpoint_id, MessageRequest={'Context': {}, 'Addresses': {sub.phone: {"ChannelType": "SMS"}}, 'MessageConfiguration': { 'SMSMessage': {'Body': 'Welcome to SMSJournal! This is the phone number where you can send your journal entries. Please save this number to your contacts as SMSJournal for easy access.', 'OriginationNumber': "+19705077992", 'MessageType': 'TRANSACTIONAL'}}}) return HttpResponseRedirect('/account/')
def test_save(self): journal = Journal(name="Science Journal") journal.save() self.assertIsNotNone(journal.id) self.assertEquals("science-journal", journal.slug) journal = Journal(name="Random Math Journal") journal.save() self.assertIsNotNone(journal.id) self.assertEquals("random-math-journal", journal.slug)
def __make_journal(self, title="title", content=None, writer=None, game_attendance=None, edit_date=timezone.now(), is_downtime=False, is_valid=False, is_deleted=False, contains_spoilers=True): journal = Journal(title=title, content=content, writer=writer, game_attendance=game_attendance, edit_date=edit_date, is_downtime=is_downtime, is_valid=is_valid, is_deleted=is_deleted, contains_spoilers=contains_spoilers) journal.save() return journal